JsonSchema improvements
I have added format support to JsonSchema through pydsl. Here is a code snippet from the tests:
schema = {
"type" : "object",
"required":["foo"],
"properties" : {
"foo" : {"enum" : [1, 3]},
"bar" : {"format": "number_three"} #Ignored by jsonschema
}
}
grammardef = JsonSchema(schema)
checker = JsonSchemaChecker(grammardef)
self.assertFalse(checker.check("a"))
pydsl provides the JsonSchema (a dictionary wrapper) and JsonSchemaChecker (a function). the default JsonSchema validate call ignores the format property.
However, by adding an extra parameter to the checker external checkers can be linked to formats:
number_three = checker_factory(String("3"))
fc = {"number_three":number_three}
grammardef = JsonSchema(schema)
checker = JsonSchemaChecker(grammardef, fc) # Adds a format checker
self.assertFalse(checker.check({"foo" : 1, "bar" : "123456"}))
Here is the helper function I used:
def formatchecker_factory(**checkerdict):
"""Converts a dictionary of strings:checkers into a formatchecker object"""
fc = FormatChecker()
for format_name, checker in checkerdict.items():
fc.checks(format_name)(checker)
return fc
note the awkward use of checks as a decorator.