From d644ac6c8e666d0c1d860938e4f0436621ba1f91 Mon Sep 17 00:00:00 2001 From: Isaac To Date: Wed, 29 Jan 2025 11:40:55 -0800 Subject: [PATCH] feat: provide helper func to create appropriate jsonschema validator --- dandischema/utils.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/dandischema/utils.py b/dandischema/utils.py index f82fac44..0ed5c060 100644 --- a/dandischema/utils.py +++ b/dandischema/utils.py @@ -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 @@ -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 = ( + 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) + + if check_format: + # Return a validator with format checking enabled + return validator_cls(schema, format_checker=validator_cls.FORMAT_CHECKER) + + # Return a validator with format checking disabled + return validator_cls(schema)