Skip to content

Object with integer property causes TypeError when patternProperties are defined #285

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
jstewmon opened this issue May 24, 2016 · 3 comments

Comments

@jstewmon
Copy link

Example:

from jsonschema import validate

schema = {
    'type': 'object',
    'patternProperties': {
        '^[0-9]{3}$': {'type': 'string'}
    },
}

validate({200: 'hello'}, schema)

Result:

Traceback (most recent call last):
  File "/Users/jstewmon/Library/Application Support/IntelliJIdea2016.1/python/helpers/pydev/pydevd.py", line 1530, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Users/jstewmon/Library/Application Support/IntelliJIdea2016.1/python/helpers/pydev/pydevd.py", line 937, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/jstewmon/Library/Preferences/IntelliJIdea2016.1/scratches/scratch_158", line 11, in <module>
    validate({200: 'hello'}, schema)
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/validators.py", line 478, in validate
    cls(schema, *args, **kwargs).validate(instance)
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/validators.py", line 122, in validate
    for error in self.iter_errors(*args, **kwargs):
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/validators.py", line 98, in iter_errors
    for error in errors:
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/_validators.py", line 14, in patternProperties
    if re.search(pattern, k):
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/re.py", line 146, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

A similar, but discrete, error occurs when additionalProperties is False:

from jsonschema import validate

schema = {
    'type': 'object',
    'patternProperties': {
        '^[0-9]{3}$': {'type': 'string'}
    },
    'additionalProperties': False
}

validate({200: 'hello'}, schema)

Result:

Traceback (most recent call last):
  File "/Users/jstewmon/Library/Application Support/IntelliJIdea2016.1/python/helpers/pydev/pydevd.py", line 1530, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/Users/jstewmon/Library/Application Support/IntelliJIdea2016.1/python/helpers/pydev/pydevd.py", line 937, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/jstewmon/Library/Preferences/IntelliJIdea2016.1/scratches/scratch_158", line 11, in <module>
    validate({200: 'hello'}, schema)
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/validators.py", line 478, in validate
    cls(schema, *args, **kwargs).validate(instance)
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/validators.py", line 122, in validate
    for error in self.iter_errors(*args, **kwargs):
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/validators.py", line 98, in iter_errors
    for error in errors:
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/_validators.py", line 25, in additionalProperties
    extras = set(_utils.find_additional_properties(instance, schema))
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/site-packages/jsonschema/_utils.py", line 100, in find_additional_properties
    if patterns and re.search(patterns, property):
  File "/Users/jstewmon/.virtualenvs/pre-commit-swagger/lib/python2.7/re.py", line 146, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer
@joepvandijken
Copy link
Contributor

joepvandijken commented May 26, 2016

You are trying to validate an property name by giving an number instead of a string eg. test with

validate({'200': 'hello'}, schema)

notice the string instead of the number 200 vs '200'
If you actually want to verify a number as a property name, I don't thinks this is supported, but I might be mistaking.

@jstewmon
Copy link
Author

@joepvandijken , you're right, but I think the expected result should be a ValidationError. The actual result is a leaked TypeError.

#286 has further discussion.

@Julian
Copy link
Member

Julian commented May 29, 2016

Let's keep discussing on the PR, closing this out, but if anyone else wants to chime in, #286

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

Successfully merging a pull request may close this issue.

3 participants