korzio/djv

Fix additionalProperties on nested schemas

Opened this issue · 7 comments

When a schema is nested (not on a first level),
When the iteration of properties uses an expression

tpl(`if(%s.properties.hasOwnProperty(${property})) continue;`, tpl.schema);

Then a schema is taken from the context of the generated validation function,
Then it is a single start point schema.
When additoonalProperties is used on a nested level
Then it will fail to validate

Example @see utils/index:makeSchema.js

return {
    properties: required.reduce((memo, key) => (
      Object.assign({}, memo, {
        [key]: makeSchema(instance[key])
      })
    ), {}),
    required,
    // other properties should be valid by `false` schema, aka not exist at all
    // additionalProperties: false,
  };

uncomment additionalProperties -> error

@korzio would this issue be the cause of the following error?

Result from errorHandler:

[ { keyword: 'oneOf',
    dataPath: '.test[0]',
    schemaPath: '#/properties/test/items/oneOf' } ]

From schema:

{
  "id": "ofTest",
  "type": "object",
  "properties": {
    "test": {
      "type": "array",
      "items": {
        "type": "object",
        "oneOf": [
          { "properties": { "alpha": { "type": "string" }}, "additionalProperties": false },
          { "properties": { "beta": { "type": "string" }}, "additionalProperties": false }
        ]
      }
    }
  },
  "required": [
    "test"
  ]
};

With data:

{ "test": [{ "alpha": "a" }]}

The error handlers I have for both ajv and tv4 pass the test with no objects added to the error array.

Hi guys.

Are there any updates on a fix for this bug? Or a known workaround?

@andreypechkurov
I have to fix it indeed :(
A workaround probably could be some sort of not keyword usage

@korzio Could you elaborate a bit more on the work around?

I was trying djv but i will have to bring back ajv because i meet this issue on all my schema :-(

Same problem here.
Appreciate the work you've done so far, thank you!

Added a branch with failing tests - https://github.com/korzio/djv/tree/issues-46-nested-schemas
This one for example:

"description": "const with object",
        "schema": {"const": {"foo": "bar", "baz": "bax"}},
        "tests": [
            {
                "description": "same object is valid",
                "data": {"foo": "bar", "baz": "bax"},
                "valid": true
            },