spidermon icon indicating copy to clipboard operation
spidermon copied to clipboard

Support jsonschema CustomValidator

Open manycoding opened this issue 5 years ago • 4 comments

As reported in https://github.com/scrapinghub/spidermon/issues/51#issuecomment-480250235 (I cp message here, since the original is too long)

We need to support other non natively jsonifiable types or force specific Python types with jsonschema with code like:


import arrow
import jsonschema
import jsonschema.validators


def is_arrow(checker, instance):
    return isinstance(instance, arrow.Arrow)


type_checker = jsonschema.Draft4Validator.TYPE_CHECKER.redefine(
    "datetime", is_arrow)

CustomValidator = jsonschema.validators.extend(
    jsonschema.Draft4Validator, type_checker=type_checker)

print(CustomValidator(
    schema={
        'type': 'object',
        'properties': {
            'v': {
                'type': 'datetime',
            },
        },
    },
).validate(
    instance={'v': arrow.now()},
))
 

manycoding avatar Jun 05 '19 00:06 manycoding

Is this issue still relevant? That is one of the reasons why I am not using spidermon's validator for now. Should I give it a try?

thamenato avatar Jul 22 '19 18:07 thamenato

Yep, and you are welcome to submit a pr.

manycoding avatar Jul 23 '19 07:07 manycoding

@thamenato We have some custom validators here: https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/validation/jsonschema/formats.py Maybe we can use the same logic for user-defined JSONSchema validators?

rennerocha avatar Jul 31 '19 14:07 rennerocha

Oh that's great @rennerocha, I haven't had much free time yet to work on this issue but that helps me with a starting place. It seems like we could use that logic for user-defined.

thamenato avatar Jul 31 '19 14:07 thamenato