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 d644ac6
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 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,40 @@ 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
"""
# Retrieve appropriate validator class for validating the given schema
validator_cls = (

Check warning on line 163 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L163

Added line #L163 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 170 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L170

Added line #L170 was not covered by tests

if check_format:

Check warning on line 172 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L172

Added line #L172 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 174 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L174

Added line #L174 was not covered by tests

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

Check warning on line 177 in dandischema/utils.py

View check run for this annotation

Codecov / codecov/patch

dandischema/utils.py#L177

Added line #L177 was not covered by tests

0 comments on commit d644ac6

Please sign in to comment.