-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make schema validator to prune properties that are not present in the…
… schema instead of failing validation.
- Loading branch information
Showing
3 changed files
with
24 additions
and
2 deletions.
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
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,22 @@ | ||
import jsonschema | ||
|
||
|
||
def add_pruning_to_validator(validator_class): | ||
validate_properties = validator_class.VALIDATORS["properties"] | ||
|
||
def remove_additional_properties(validator, properties, instance, schema): | ||
for prop in list(instance.keys()): | ||
if prop not in properties: | ||
del instance[prop] | ||
|
||
for error in validate_properties(validator, properties, instance, schema,): | ||
yield error | ||
|
||
return jsonschema.validators.extend(validator_class, {"properties" : remove_additional_properties}) | ||
|
||
|
||
PruningValidator = add_pruning_to_validator(jsonschema.validators.Draft7Validator) | ||
|
||
|
||
def validate(instance, schema): | ||
return PruningValidator(schema).validate(instance) |