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

Support additional (Python) types in more than the type validator #727

Open
charkins opened this issue Aug 21, 2020 · 1 comment
Open

Support additional (Python) types in more than the type validator #727

charkins opened this issue Aug 21, 2020 · 1 comment
Labels
Dialects v2 Issues which will likely be addressed as part of reworked dialect support Enhancement Some new desired functionality

Comments

@charkins
Copy link

charkins commented Aug 21, 2020

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.

@Julian
Copy link
Member

Julian commented Aug 30, 2020

(Will come back to this in more detail)

Thanks for filing -- yeah, I was somewhat aware of this at the time.

Really the only way to do this "well" is if jsonschema specified what protocol each validator expected and committed to that protocol, but folks who want to do things like this somewhat expect to see things work as-is, which as you say, isn't the way things work.

At the minute things do work for type (the validator), but if arbitrary validators are to work with random types then yeah something much bigger would be needed.

We can leave this open for now -- I'm not sure such a thing is in jsonschema's future but worth thinking through.

@Julian Julian changed the title NamedTuple not usable as object Support additional (Python) types in more than the type validator Aug 30, 2020
@Julian Julian added the Enhancement Some new desired functionality label Aug 30, 2020
@Julian Julian added the Dialects v2 Issues which will likely be addressed as part of reworked dialect support label Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dialects v2 Issues which will likely be addressed as part of reworked dialect support Enhancement Some new desired functionality
Projects
None yet
Development

No branches or pull requests

2 participants