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

Error in early release 3.0.0.a3 but not in 2.6.0 #509

Closed
timbouffard opened this issue Dec 30, 2018 · 6 comments
Closed

Error in early release 3.0.0.a3 but not in 2.6.0 #509

timbouffard opened this issue Dec 30, 2018 · 6 comments
Labels
Bug

Comments

@timbouffard
Copy link

@timbouffard timbouffard commented Dec 30, 2018

I'm sure I'm just doing something wrong but in version 2.6.0 this code works for both validation calls. In the newer version the second validation call fails.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import jsonschema
from jsonschema import validate, Draft4Validator
from jsonschema.exceptions import ValidationError


def main():
    # Define JSON schema
    schema = {
        'type': 'object',
        'properties': {
            'value': {
                'type': 'number'
            },
        }
    }

    all_validators = dict(Draft4Validator.VALIDATORS)

    MyValidator = jsonschema.validators.create(
        meta_schema=Draft4Validator.META_SCHEMA,
        validators=all_validators
    )

    print('my validator')
    # This call to validate 'works', I get a validation error
    try:
        print(validate({'value': 'a'}, schema))
    except ValidationError:
        print('validation error when calling validate')

    # this call to my_validator.is_valid fails...it doesn't return false, it just errors
    my_validator = MyValidator(schema)
    print(my_validator.is_valid({'value': 'a'}))


if __name__ == '__main__':
    main()

This is python 3.6 if that makes any difference. I tried it for 3.0.0.a3, a2 and a1 - same error.
Product is awesome btw. Very helpful with what I need to do.

@Julian

This comment has been minimized.

Copy link
Owner

@Julian Julian commented Jan 3, 2019

Hm, my response didn't seem to come through here, so typing it out again (fingers crossed GitHub):

I'd guess this likely has to do with the type checker interface changes.

Though if so, thanks for filing this, maybe it's a nudge to change the interface slightly more.

Though overall, unless you have a good reason for doing it in your real code, not sure why you're using create rather than extend.

And thanks! Glad it's helping you.

@timbouffard

This comment has been minimized.

Copy link
Author

@timbouffard timbouffard commented Jan 3, 2019

Ah okay. I'll try extend. I was basically following the example at this link because I wanted to have a custom check on a particular number. Then the example I included here was a stripped down example.

@Julian

This comment has been minimized.

Copy link
Owner

@Julian Julian commented Jan 3, 2019

Bit of a shame that link became a blog post instead of just a pull request adding some prose docs for those cases, would have been happy to merge something like it.

Who knows if it's correct :), will have to read through it, but yeah I suspect that you likely do want extend.

@timbouffard

This comment has been minimized.

Copy link
Author

@timbouffard timbouffard commented Jan 5, 2019

Using extend worked perfectly. From the above sample I changed MyValidator to:

    MyValidator = jsonschema.validators.extend(validator=Draft4Validator,
                                               validators=all_validators)

I tried it with 3.0.0.a3 and Draft4Validator, Draft6Validator and Draft7Validators. No problems.

The sample I provided didn't really what show what we were doing. We are adding a new validator to check a number against a regex pattern.

def custom_number_pattern_validator(validator, value, instance, schema):
    if isinstance(instance, Number):
        if re.match(value, str(instance)) is None:
            yield ValidationError(f'{instance} does not pass number pattern format check.')

# extend the validator to include our custom validation...
VALIDATOR = validators.extend(validator=Draft7Validator,
                              validators={'customNumberPattern': custom_number_pattern_validator})

To us this was a clear win because we have conditions where a number should be a certain amount of max decimal places, a certain max 'size' to the right and left of the decimal, stuff like that. We can express that 'rule' with a regex, return an error if it doesn't match and skip coding that logic everywhere it's needed. I suppose we could make the type 'string' and use 'pattern' but for some reason that just seems wrong when we really do have a number data type. :)

I'm going to stay with the 3.0.0.a3 and the Draft 7 validator while we develop.
Thanks again for your help.
Tim

@timbouffard

This comment has been minimized.

Copy link
Author

@timbouffard timbouffard commented Jan 10, 2019

@Julian

This comment has been minimized.

Copy link
Owner

@Julian Julian commented Jan 11, 2019

No worries!

@Julian Julian added this to the v3.0.0 milestone Jan 22, 2019
@Julian Julian closed this in 7df693c Jan 26, 2019
@Julian Julian added the Bug label Apr 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.