Giter VIP home page Giter VIP logo

Comments (2)

epoberezkin avatar epoberezkin commented on May 12, 2024

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 ;)

from ajv.

epoberezkin avatar epoberezkin commented on May 12, 2024

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.

from ajv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.