customize validator to reject NaN, Infinity #1010
-
I have a Draft7Validator I use to validate some messages. I have seen primary_schema_object = decode_schema(json_schema_text)
...
self.validator = Draft7Validator(primary_schema_object)
self.validator.resolver.store = reference_schema_objects def is_my_int(self, checker, instance):
# example - change this
return instance == 4 When I attempt to use validators.extend() and validate it, I get an error as shown below: type_checker = (
Draft7Validator.TYPE_CHECKER.redefine("number", self.is_my_int),
)
self.validator = validators.extend(Draft7Validator, type_checker=type_checker)
self.validator(primary_schema_object)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
There may be more than one problem, but there's certainly at least one simple one, which is you did:
probably using the parentheses to break up the line, but that's a 1-element tuple, because you have a comma there. So you've passed a tuple in as a type checker, when the right type for that argument is an instance of You want just Beyond that it'd be helpful to see a bit more of a fully functioning example to offer advice, because yeah you're in the right spot, if you want to reject those you should redefine |
Beta Was this translation helpful? Give feedback.
There may be more than one problem, but there's certainly at least one simple one, which is you did:
probably using the parentheses to break up the line, but that's a 1-element tuple, because you have a comma there.
So you've passed a tuple in as a type checker, when the right type for that argument is an instance of
TypeChecker
(whichredefine
returns).You want just
type_checker = Draft7Validator.TYPE_CHECKER.redefine("number", self.is_my_int)
or if you want it on multiple lines remove the comma.Beyond that it'd be helpful to see a bit more of a fully functioning example to offer ad…