Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to the source rootData from the custom keyword validator #567

Closed
VladimirShestakov opened this issue Sep 18, 2017 · 2 comments
Closed
Labels

Comments

@VladimirShestakov
Copy link

Can I get access to the source rootData (that not changed by rules) from the custom keyword validator?

addKeyword('myKey', {
      modifying: true,
      compile: (sch, parentSchema) => {
        return (data, dataPath, parentObject, propName, rootData) => {
             console.log(rootData); // <<--- rootData is already changed
             return true;
        };
      }
@epoberezkin
Copy link
Member

Ajv does not clone the original data in any way, see #549 for the reasons. The rootData is always passed by the reference.

If your custom keywords modifies the data then all the keywords that execute after will have access to the modified data.

You can work around by passing both the original and the clone as two properties of the same object and either re-define your schemas accordingly or just construct the modified schema in code.

Data:

{
  data: {/* your data */},
  original: {/* the deep-clone of your data */}
}

Schema:

{
  type: 'object',
  properties: {
    data: {/* your schema */}
    original: {}
  },
  additionalProperties: false
}

@VladimirShestakov
Copy link
Author

Nice idea, but I applied a closure. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants