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

SchemaError in 4.0.1 for schema working with 3.2 #852

Closed
rikardn opened this issue Oct 4, 2021 · 2 comments
Closed

SchemaError in 4.0.1 for schema working with 3.2 #852

rikardn opened this issue Oct 4, 2021 · 2 comments

Comments

@rikardn
Copy link

rikardn commented Oct 4, 2021

The schema in the example below can be used to validate the instance without problems in jsonschema 3.2, but when using 4.0.1 it gives a schema error.

import jsonschema
instance = ['A', 'B']
schema = {
  "type": "array",
  "items": [{"type": "string"}, {"type": "string"}]
}
jsonschema.validate(instance, schema)

Output:

  File "<stdin>", line 1, in <module>
  File "/home/debug/myvenv/lib/python3.8/site-packages/jsonschema/validators.py", line 963, in validate
    cls.check_schema(schema)
  File "/home/debug/myvenv/lib/python3.8/site-packages/jsonschema/validators.py", line 183, in check_schema
    raise exceptions.SchemaError.create_from(error)
jsonschema.exceptions.SchemaError: [{'type': 'string'}, {'type': 'string'}] is not of type 'object', 'boolean'

Failed validating 'type' in metaschema['allOf'][1]['properties']['items']['$dynamicRef']['allOf'][0]:
    {'$defs': {'anchorString': {'pattern': '^[A-Za-z_][-A-Za-z0-9._]*$',
                                'type': 'string'},
               'uriReferenceString': {'format': 'uri-reference',
                                      'type': 'string'},
               'uriString': {'format': 'uri', 'type': 'string'}},
     '$dynamicAnchor': 'meta',
     '$id': 'https://json-schema.org/draft/2020-12/meta/core',
     '$schema': 'https://json-schema.org/draft/2020-12/schema',
     '$vocabulary': {'https://json-schema.org/draft/2020-12/vocab/core': True},
     'properties': {'$anchor': {'$ref': '#/$defs/anchorString'},
                    '$comment': {'type': 'string'},
                    '$defs': {'additionalProperties': {'$dynamicRef': '#meta'},
                              'type': 'object'},
                    '$dynamicAnchor': {'$ref': '#/$defs/anchorString'},
                    '$dynamicRef': {'$ref': '#/$defs/uriReferenceString'},
                    '$id': {'$comment': 'Non-empty fragments not allowed.',
                            '$ref': '#/$defs/uriReferenceString',
                            'pattern': '^[^#]*#?$'},
                    '$ref': {'$ref': '#/$defs/uriReferenceString'},
                    '$schema': {'$ref': '#/$defs/uriString'},
                    '$vocabulary': {'additionalProperties': {'type': 'boolean'},
                                    'propertyNames': {'$ref': '#/$defs/uriString'},
                                    'type': 'object'}},
     'title': 'Core vocabulary meta-schema',
     'type': ['object', 'boolean']}

On schema['items']:
    [{'type': 'string'}, {'type': 'string'}]
@Julian
Copy link
Member

Julian commented Oct 4, 2021

Hi. Your schema doesn't use $schema to declare what version of the spec it's written for, so you're comparing behavior across different versions of the spec.

Indeed in draft 7 what you have is fine, but in newer drafts it isn't, as doing what you have there now uses prefixItems rather than items.

You should be setting $schema or at least if not, directly using a validator for the version you intend to use. And if that's draft2020-12, prefixItems.

@Julian Julian closed this as completed Oct 4, 2021
@rikardn
Copy link
Author

rikardn commented Oct 4, 2021

Thanks for the explanation @Julian !

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

No branches or pull requests

2 participants