Skip to content

Commit

Permalink
fix(jsonschema): sets default values defined in the schema
Browse files Browse the repository at this point in the history
When we moved to validate `jsonschemas` on the fly instead of
using the pre-compiled validation code, we regressed the existing
implementation by not realizing that the validation isn't setting
defaults anymore. In order to solve the issue, we referred to the
following link,

https://python-jsonschema.readthedocs.io/en/stable/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance

Since we are using the draft07 of JSONSchema, we referred to the
article and updated the code to define a default validator by
extending the Draft7Validator class to set defaults.

We now use this default validator instead of the `jsonschema.validate` method.
  • Loading branch information
pallabpain committed May 5, 2023
1 parent c5dcf8a commit 76b403a
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions riocli/jsonschema/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
from pathlib import Path

import yaml

import jsonschema
from jsonschema import validators, Draft7Validator


@lru_cache(maxsize=None)
Expand All @@ -30,8 +29,33 @@ def load_schema(resource: str) -> dict:
return yaml.safe_load(f)


def extend_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]

def set_defaults(validator, properties, instance, schema):
for p, sub_schema in properties.items():
if "default" in sub_schema:
if isinstance(instance, dict):
instance.setdefault(p, sub_schema["default"])
if isinstance(instance, list):
for i in instance:
i.setdefault(p, sub_schema["default"])

for error in validate_properties(
validator, properties, instance, schema,
):
yield error

return validators.extend(
validator_class, {"properties": set_defaults},
)


DefaultValidator = extend_with_default(Draft7Validator)


def validate_manifest(instance, schema) -> None:
"""
Validates a manifest against a JSON schema.
"""
jsonschema.validate(instance, schema)
DefaultValidator(schema).validate(instance)

0 comments on commit 76b403a

Please sign in to comment.