Skip to content

Commit

Permalink
Make schema validator to prune properties that are not present in the…
Browse files Browse the repository at this point in the history
… schema instead of failing validation.
  • Loading branch information
smelamud committed Jun 5, 2024
1 parent 47693c3 commit 42693bb
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/moeralib/naming/naming.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from typing import Any, cast, Sequence, Tuple

import requests
from jsonschema import validate
from jsonschema.exceptions import ValidationError

from . import schemas, types
from ..pruning_validator import validate
from ..structure import structure_or_none, structure_list

MAIN_SERVER = 'https://naming.moera.org/moera-naming'
Expand Down
2 changes: 1 addition & 1 deletion src/moeralib/node/caller.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from typing import Any, IO, Mapping, Sequence

import requests
from jsonschema import validate
from jsonschema.exceptions import ValidationError

from . import schemas
from .types import Result, BodyFormat, SourceFormat, Body
from ..pruning_validator import validate
from ..structure import Structure


Expand Down
22 changes: 22 additions & 0 deletions src/moeralib/pruning_validator.py
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)

0 comments on commit 42693bb

Please sign in to comment.