Skip to content

jsonschema validator issue -- validate a wrong schema #512

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

Closed
fantamiracle opened this issue Jan 3, 2019 · 2 comments
Closed

jsonschema validator issue -- validate a wrong schema #512

fantamiracle opened this issue Jan 3, 2019 · 2 comments
Labels
Invalid Not a bug, PEBKAC, or an unsupported setup

Comments

@fantamiracle
Copy link

fantamiracle commented Jan 3, 2019

>>> import jsonschema
>>> jsonschema.__version__
'2.6.0'
>>> event = {
   "event": "entry.u",
   "timestamp": 1546467787.4796588,
   "data": {
     "orgId": 316,
     "requestId": 1128121473,
     "entryIds": [
       98
     ]
   }
 }
>>> t = {
   "properties": {
     "event": {
       "enum": [
         "un",
         "entry.u"
       ]
     },
     "data": {
       "required": ["entryIds"],
       "properties": {
         "entryIds": {
           "contains": {
             "enum": [
               980
             ]
           }
         }
       }
     }
   },
   "required": [
     "event"
   ]
 }
>>> jsonschema.validate(event, t)
>>>

The validation should fail as the entryIds are not matching. I tested this with some online validators and they all failed, but it passes using jsonschema.validate. Any clues? what am I missing here?

@fantamiracle fantamiracle changed the title jsonschema validator isssue jsonschema validator issue -- validate a wrong schema Jan 3, 2019
@omenlabs
Copy link

omenlabs commented Jan 3, 2019

It looks like contains was added post 2.6.0.

497dfec#diff-66289c46b2d9052f921a55730be5c8bd

However, you can extend one of the validators in 2.6.0 with contains:

#!/usr/bin/env python

import jsonschema

print(jsonschema.__version__)

event = {
   "event": "entry.u",
   "timestamp": 1546467787.4796588,
   "data": {
     "orgId": 316,
     "requestId": 1128121473,
     "entryIds": [
          98, 9
     ]
   }
 }

t = {
   "properties": {
     "event": {
       "enum": [
         "un",
         "entry.u"
       ]
     },
     "data": {
       "required": ["entryIds"],
       "properties": {
         "entryIds": {
             "type": "array",
             "contains": { "enum":[ 98] }
         }
       }
     }
   },
   "required": [
     "event"
   ]
 }

# Validator for contains from v3.0.0a3
def contains(validator, contains, instance, schema):
    if not validator.is_type(instance, "array"):
        return

    if not any(validator.is_valid(element, contains) for element in instance):
        yield jsonschema.ValidationError(
            "None of %r are valid under the given schema" % (instance,)
        )

myV = jsonschema.validators.extend(jsonschema.Draft4Validator, {u"contains": contains}, version='draft6ish')

validator = myV(t)

print("Next line should be None")
print(validator.validate(event))
print("Now we explode")
event["data"]["entryIds"] = [99]
print(validator.validate(event))

Output:

$ ./test.py 
2.6.0
Next line should be None
None
Now we explode
Traceback (most recent call last):
  File "./test.py", line 59, in <module>
    print(validator.validate(event))
  File "/home/jjh/.pyenv/py3/lib/python3.6/site-packages/jsonschema/validators.py", line 130, in validate
    raise error
jsonschema.exceptions.ValidationError: None of [99] are valid under the given schema

Failed validating 'contains' in schema['properties']['data']['properties']['entryIds']:
    {'contains': {'enum': [98]}, 'type': 'array'}

On instance['data']['entryIds']:
    [99]

@Julian
Copy link
Member

Julian commented Jan 3, 2019

contains is draft-6 only, yes (and it looks like your schema doesn't declare itself under a version, which it should with a $schema property).

You'd need to use one of the prereleases, or sure, extend the one you've got.

@Julian Julian closed this as completed Jan 3, 2019
@Julian Julian added the Invalid Not a bug, PEBKAC, or an unsupported setup label Apr 5, 2019
@python-jsonschema python-jsonschema deleted a comment from sunshare10 Dec 12, 2019
Julian added a commit that referenced this issue Oct 7, 2021
54440eab4 Merge pull request #516 from ChALkeR/chalker/ipv6
e7b22e1c6 Fix the sanity check by pinning.
8891d8107 Merge pull request #519 from json-schema-org/ether/custom-dialect
5f5fccda3 test the format-assertion vocabulary with a custom metaschema
3fcee3868 Merge pull request #512 from json-schema-org/ether/formats-and-non-strings
b349b8797 test that format-assertions are valid with non-string types
8e5b2f10d fix needless inconsistencies in format tests between drafts
02d7cb59a Correct "ref with sibling id" tests
1649470ba More ipv6 tests to increase coverage
7334b4c7e Merge pull request #505 from ChALkeR/chalker/fix-unicode
0fb2d2787 Consolidate optional/unicode into optional/ecmascript-regex
4f8c6d7bf unevaluatedProperties: deep dynamic + refs
9103f3b6f $ref wit id does not test what it is indented to do
f300dd15f Add test "same $anchor with different base uri"
d128f9d7f Add test to check that $id resolved against nearest parent, not just immediate parent
72e31dd20 Merge pull request #515 from json-schema-org/ether/fix-mandatory-format-tests
0173a0835 Revert "by default, "format" only annotates, not validates"
66e813a90 Merge pull request #506 from json-schema-org/ether/formats-non-ascii
9430972bc fix unicode tests in accordance to pattern/patternProperties spec

git-subtree-dir: json
git-subtree-split: 54440eab4d50b80a62cc9f9c561e306cdbb19591
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Invalid Not a bug, PEBKAC, or an unsupported setup
Projects
None yet
Development

No branches or pull requests

3 participants