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

Schema is not properly validating data #428

Closed
deep9 opened this issue Feb 27, 2017 · 2 comments
Closed

Schema is not properly validating data #428

deep9 opened this issue Feb 27, 2017 · 2 comments

Comments

@deep9
Copy link

deep9 commented Feb 27, 2017

What version of Ajv are you using? Does the issue happen if you use the latest version?
4.11.3, latest at the time

Ajv options object (see https://github.com/epoberezkin/ajv#options):

{allErrors: true, removeAdditional: 'all'}

JSON Schema (please make it as small as possible to reproduce the issue):

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "testDefinitions",
  "description": "Test definition schema",

  "type": "array",

  "items": {
    "type": "object",
    "anyOf": [
      {
        "allOf": [
          {
            "allOf": [
              {
                "properties": {
                  "type": {
                    "type": "string",
                    "minLength": 1
                  }
                }
              },
              {
                "required": [
                  "type"
                ]
              }
            ]
          },
          {
            "properties": {
              "type": {
                "enum": [
                  "openUrl"
                ]
              },
              "lineId": {
                "type": "string"
              },
              "url": {
                "type": "string",
                "format": "uri",
                "minLength": 1
              }
            },
            "required": [
              "url"
            ]
          }
        ]
      }
    ]
  }
}

Please note the schema is dereferenced, that is why there is a lot of allOf elements

Data (please make it as small as posssible to reproduce the issue):

[
	{
		"lineId": 1,
		"type": "openUrl",
		"url": "http://localhost"
	}
]

Your code (please use options, schema and data as variables):

const ajv = new Ajv(options);
return new Promise((resolve, reject) => {
        ajv.validate(schema, data);
        if (ajv.errors === null) {
            resolve(data);
        } else {
            reject(ajv.errors);
        }
    });

See: https://runkit.com/deep9/ajv-issue

Validation result, data AFTER validation, error messages:

[ { keyword: 'required',
    dataPath: '[0]',
    schemaPath: '#/items/anyOf/0/allOf/1/required',
    params: { missingProperty: 'url' },
    message: 'should have required property \'url\'' },
  { keyword: 'anyOf',
    dataPath: '[0]',
    schemaPath: '#/items/anyOf',
    params: {},
    message: 'should match some schema in anyOf' } ]

What results did you expect?
If you run the same schema in http://www.jsonschemavalidator.net, expected result of

Message:
JSON does not match any schemas from 'anyOf'.
Schema path:
testDefinitions#/items/0/anyOf
Message:
JSON does not match all schemas from 'allOf'. Invalid schema indexes: 1.
Schema path:
testDefinitions#/items/0/anyOf/0/allOf
Message:
Invalid type. Expected String but got Integer.
Schema path:
testDefinitions#/items/0/anyOf/0/allOf/1/properties/lineId/type

Are you going to resolve the issue?
Don't know how.

@epoberezkin
Copy link
Member

The reason is removeAdditional options, when the first subschema is validated everything but "type" gets removed.

see #129, https://github.com/epoberezkin/ajv/blob/master/FAQ.md#additional-properties-inside-compound-keywords-anyof-oneof-etc

@deep9
Copy link
Author

deep9 commented Feb 27, 2017

Now I've tested with https://pypi.python.org/pypi/jsonschema and the result is the same. As being new to json schema, how can I know which one is correct?

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

No branches or pull requests

2 participants