-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schema validation like in odr: Apply default values (#50)
- Loading branch information
1 parent
4c62981
commit 035fd1b
Showing
2 changed files
with
25 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from jsonschema import Draft7Validator, validators | ||
|
||
|
||
def extend_with_default(validator_class): | ||
validate_properties = validator_class.VALIDATORS["properties"] | ||
|
||
def set_defaults(validator, properties, instance, schema): | ||
for property, subschema in properties.items(): | ||
if "default" in subschema: | ||
instance.setdefault(property, subschema["default"]) | ||
|
||
yield from validate_properties( | ||
validator, properties, instance, schema, | ||
) | ||
|
||
return validators.extend( | ||
validator_class, {"properties": set_defaults}, | ||
) | ||
|
||
|
||
DefaultValidatingDraft7Validator = extend_with_default(Draft7Validator) |