Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/jacobian/schema_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ def _validated_schema(canonical_schema: bytes) -> Draft202012Validator:
return Draft202012Validator(normalized, format_checker=FormatChecker())


@lru_cache(maxsize=1024)
def _trusted_validated_schema(canonical_schema: bytes) -> Draft202012Validator:
"""Compile a schema without re-validating against the meta-schema.

Pydantic's model_json_schema() produces valid Draft 2020-12 schemas
by construction. Skipping the expensive meta-schema walk for those
schemas eliminates the dominant cost of runtime construction (~6.7s
across 527 unique schemas at ~12.7ms each). External references are
still rejected; only the meta-schema check is skipped.
"""

normalized = loads_strict_json(canonical_schema)
_reject_external_references(normalized)
return Draft202012Validator(normalized, format_checker=FormatChecker())


class SchemaRegistry:
"""Store and apply closed local JSON Schemas used by artifact contracts."""

Expand All @@ -103,8 +119,21 @@ def __init__(self, store: ArtifactRepository) -> None:
self._registrations: dict[tuple[str, str, bytes], str] = {}
self._pending: dict[int, _PendingRegistrations] = {}

def register(self, *, name: str, version: str, schema: dict[str, Any]) -> str:
"""Register a schema after rejecting unsupported external references."""
def register(
self,
*,
name: str,
version: str,
schema: dict[str, Any],
trusted: bool = False,
) -> str:
"""Register a schema after rejecting unsupported external references.

When ``trusted`` is True, the schema is assumed to be valid Draft
2020-12 by construction (e.g. generated by Pydantic's
``model_json_schema()``) and the expensive meta-schema validation is
skipped. External references are always rejected.
"""

self._reconcile_pending()
canonical_schema = canonicalize_json(schema)
Expand Down Expand Up @@ -132,7 +161,10 @@ def register(self, *, name: str, version: str, schema: dict[str, Any]) -> str:
version=version,
definition=schema,
)
_validated_schema(canonical_schema)
if trusted:
_trusted_validated_schema(canonical_schema)
else:
_validated_schema(canonical_schema)
schema_uri = self.store.register_descriptor(
kind="schema",
name=name,
Expand Down Expand Up @@ -168,6 +200,7 @@ def register_model(
name=name,
version=version,
schema=model_schema(model),
trusted=True,
Comment thread
morluto marked this conversation as resolved.
Outdated
)
self._bind_model_contract(schema_uri, model)
if producer_only:
Expand Down
Loading