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

dataPath of additionalProperties is empty #11

Closed
shachr opened this issue Jun 16, 2015 · 2 comments
Closed

dataPath of additionalProperties is empty #11

shachr opened this issue Jun 16, 2015 · 2 comments
Labels

Comments

@shachr
Copy link

shachr commented Jun 16, 2015

i would like to remove all additional properties but dataPath is always empty when err type is additionalProperties!

@epoberezkin
Copy link
Member

I suspect I may have recently fixed the issue with incorrect dataPath reporting in version 0.4.9 (it was issue #9).

Please try using the latest version (0.4.10 - there is another important fix). If you are still seeing the issue please post your schema, data and results (errors) here.

Thanks for helping me making it better ;)

@epoberezkin
Copy link
Member

Ok, I've figured what it was about.
The choice I've made was to put the object path in dataPath rather than the property, because it is the object that is being validated rather than its property.
So if you had additionalProperty keyword in some sub-object, you would have it's path in dataPath.
Although this is more consistent with the rest of the validation keywords, I agree that it is useful to have the actual additional property in error.dataPath.

So I added it in 0.4.15.

var schema = {
  properties: {
    foo: {},
    bar: {}
  },
  additionalProperties: false
};

var data = { foo: 1, bar: 2, baz: 3, quux: 4 };

var ajv = Ajv({allErrors: true});
var valid = ajv.validate(schema, data);
console.log(ajv.errors);

There will be two errors in the array, with dataPaths "['baz']" and "['.quux']". (It would have been easy to replace them with ".baz" and ".quux", but as it's not a compile time replacement, I decided to keep it faster - you can map/slice your properties out of these errors.

So if you want to remove additional properties you could do smth like this:

ajv.errors
.filter(function(err) { return err.keyword == 'additionalProperties'; })
.map(function(err) { return err.dataPath.slice(2,-2); })
.forEach(function(p) { delete data[p]; });

It'll be just a bit more complex for properties in sub-objects.

This was referenced Oct 29, 2015
epoberezkin added a commit that referenced this issue Nov 21, 2015
 - now dataPath points to the object that is validated and not to the additional property; old (<=1.4.10 ) error reporting of dataPath for "additionalProperties" keyword is available with option errorDataPath == "property"
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