Skip to content

Commit

Permalink
feat: provide helper func to create appropriate jsonschema validator
Browse files Browse the repository at this point in the history
  • Loading branch information
candleindark committed Jan 29, 2025
1 parent 6fd04fb commit d1bebc7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions dandischema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import re
from typing import Any, Iterator, List, Union, get_args, get_origin

from jsonschema.protocols import Validator as JsonschemaValidator
from jsonschema.validators import validator_for
from pydantic.json_schema import GenerateJsonSchema, JsonSchemaMode, JsonSchemaValue
from pydantic_core import CoreSchema, core_schema

Expand Down Expand Up @@ -136,3 +138,41 @@ def sanitize_value(value: str, field: str = "non-extension", sub: str = "-") ->
if field != "extension":
value = value.replace(".", sub)
return value


def jsonschema_validator(
schema: dict[str, Any],
*,
check_format: bool,
default_cls: type[JsonschemaValidator] | None = None,
) -> JsonschemaValidator:
"""
Create a JSON schema validator appropriate for validating instances against a given
schema
:param schema: The JSON schema to validate against
:param check_format: Indicates whether to check the format against format
specifications in the schema
:param default_cls: The default JSON schema validator class to use to create the
validator should the appropriate validator class cannot be determined based on
the schema (by assessing the `$schema` property). If `None`, the class
representing the latest JSON schema draft supported by the `jsonschema` package.
:return: The JSON schema validator
:raises jsonschema.exceptions.SchemaError: If the JSON schema is invalid
"""
# Retrieve appropriate validator class for validating the given schema
validator_cls = (

Check warning on line 164 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L164

Added line #L164 was not covered by tests
validator_for(schema, default_cls)
if default_cls is not None
else validator_for(schema)
)

# Ensure the schema is valid
validator_cls.check_schema(schema)

Check warning on line 171 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L171

Added line #L171 was not covered by tests

if check_format:

Check warning on line 173 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L173

Added line #L173 was not covered by tests
# Return a validator with format checking enabled
return validator_cls(schema, format_checker=validator_cls.FORMAT_CHECKER)

Check warning on line 175 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L175

Added line #L175 was not covered by tests

# Return a validator with format checking disabled
return validator_cls(schema)

Check warning on line 178 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L178

Added line #L178 was not covered by tests

0 comments on commit d1bebc7

Please sign in to comment.