spidermon
spidermon copied to clipboard
Support jsonschema CustomValidator
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()},
))
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?
Yep, and you are welcome to submit a pr.
@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?
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.