python-jsonschema / python-jsonschema/jsonschema
Support additional (Python) types in more than the `type` validator
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5k
- Forks
- 671
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 10
Description
In issue #364 and the related PR the type checker was expanded to allow redefining how jsonschema identifies/validates a type, with the intention of allowing a named tuple to be used as a jsonschema "object" type. In practice, this does not appear to be enough, as other portions of jsonschema use the python "in" expression to test for or iterate over properties in an object. In a NamedTuple, this will actually test the values of the properties. Two specific examples (required and additionalProperties) highlight this:
from typing import NamedTuple
from jsonschema import Draft7Validator
from jsonschema.validators import extend
from jsonschema.exceptions import ValidationError
# dict or tuple w/ _fields is object (allowed NamedTuple as object)
def is_object_or_namedtuple(checker, instance):
return isinstance(instance, dict) or (
isinstance(instance, tuple) and hasattr(instance, '_fields'))
# redefine object to allow namedtuple
type_checker = Draft7Validator.TYPE_CHECKER.redefine(
'object', is_object_or_namedtuple)
# extend validator with new type checker
Validator = extend(Draft7Validator, type_checker=type_checker)
# validator with foo as required property
validator_foo_required = Validator(schema={
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"required": ["foo"]
})
# validator with foo as optional property, no additional
validator_foo_optional_no_additional = Validator(schema={
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": False
})
class Example(NamedTuple):
foo: str
example = Example(foo="bar")
try:
validator_foo_required.validate(example)
except ValidationError as e:
print(f"Error: {e.message}")
try:
validator_foo_optional_no_additional.validate(example)
except ValidationError as e:
print(f"Error: {e.message}")
# the problem is that 'foo' is not "in" example..
print('Is foo in example? ', ('foo' in example))
# ... but 'bar' is
print('Is bar in example? ', ('bar' in example))
This is more of an FYI, as #364 was targeted at supporting NamedTuple, but it looks like there would be some bigger changes required to facilitate redefining how properties are identified/accessed in the NamedTuple.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading issue #364 and its related pull request, then reproduce the NamedTuple examples in this issue with the required and additionalProperties validators. Define how named-tuple properties should be identified and accessed consistently, and consider the work complete when both examples validate the intended properties without treating their values as property names.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100