Formatron v0.4.9
Formatron empowers everyone to control the output format of language models with minimal overhead.
Loading...
Searching...
No Matches
formatron.schemas.json_schema Namespace Reference

This module contains utilities for creating schemas from JSON schemas. More...

Classes

class  FieldInfo
 

Functions

schemas.schema.Schema create_schema (dict[str, typing.Any] schema, registry=Registry())
 Create a Schema object from a JSON schema object.
 
str _resolve_new_url (str uri, str ref)
 Adapted from https://github.com/python-jsonschema/referencing/blob/main/referencing/_core.py#L667.
 
None _validate_json_schema (dict[str, typing.Any] schema)
 
typing.Type _convert_json_schema_to_our_schema (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 Recursively handle all types needed to fully determine the type of a schema.
 
 _extract_fields_from_object_type (typing.Type object_type)
 
typing.Type _handle_anyOf (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 
typing.Type[typing.Any|None] _infer_type (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 Infer more specific types.
 
typing.Any _get_literal (dict[str, typing.Any] schema)
 
typing.Type _handle_literal (typing.Any literal, typing.Type obtained_type, dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 
typing.Type _handle_str_with_metadata (dict[str, typing.Any] schema)
 Handle string type with metadata such as maxLength, minLength, and pattern.
 
typing.Type _handle_numeric_with_metadata (dict[str, typing.Any] schema, typing.Type numeric_type)
 Handle numeric types (int or float) with metadata such as minimum, maximum, exclusiveMinimum, and exclusiveMaximum.
 
typing.Type _create_custom_type (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 
typing.Type _handle_list_metadata (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 Handle cases where the obtained type is a list.
 
typing.Type[typing.Any|None] _obtain_type (dict[str, typing.Any] schema, dict[int, typing.Type] json_schema_id_to_schema)
 Directly obtain type information from this schema's type keyword.
 
 _merge_referenced_schema (dict[str, typing.Any] schema, set[int] memo)
 
 _merge_key (dict[str, typing.Any] schema, str ref_key, typing.Any reference_value)
 
 _recursive_resolve_reference (str base_uri, typing.Any schema, Registry registry, set[int] memo)
 
 _resolve_reference (dict[str, typing.Any] schema, str key, typing.Any resolver)
 

Variables

int _counter
 

Detailed Description

This module contains utilities for creating schemas from JSON schemas.

Function Documentation

◆ _convert_json_schema_to_our_schema()

typing.Type formatron.schemas.json_schema._convert_json_schema_to_our_schema ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Recursively handle all types needed to fully determine the type of a schema.

Definition at line 156 of file json_schema.py.

◆ _create_custom_type()

typing.Type formatron.schemas.json_schema._create_custom_type ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Definition at line 280 of file json_schema.py.

◆ _extract_fields_from_object_type()

formatron.schemas.json_schema._extract_fields_from_object_type ( typing.Type object_type)
protected

Definition at line 173 of file json_schema.py.

◆ _get_literal()

typing.Any formatron.schemas.json_schema._get_literal ( dict[str, typing.Any] schema)
protected

Definition at line 225 of file json_schema.py.

◆ _handle_anyOf()

typing.Type formatron.schemas.json_schema._handle_anyOf ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Definition at line 180 of file json_schema.py.

◆ _handle_list_metadata()

typing.Type formatron.schemas.json_schema._handle_list_metadata ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Handle cases where the obtained type is a list.

Definition at line 296 of file json_schema.py.

◆ _handle_literal()

typing.Type formatron.schemas.json_schema._handle_literal ( typing.Any literal,
typing.Type obtained_type,
dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Definition at line 230 of file json_schema.py.

◆ _handle_numeric_with_metadata()

typing.Type formatron.schemas.json_schema._handle_numeric_with_metadata ( dict[str, typing.Any] schema,
typing.Type numeric_type )
protected

Handle numeric types (int or float) with metadata such as minimum, maximum, exclusiveMinimum, and exclusiveMaximum.

Definition at line 263 of file json_schema.py.

◆ _handle_str_with_metadata()

typing.Type formatron.schemas.json_schema._handle_str_with_metadata ( dict[str, typing.Any] schema)
protected

Handle string type with metadata such as maxLength, minLength, and pattern.

Definition at line 243 of file json_schema.py.

◆ _infer_type()

typing.Type[typing.Any | None] formatron.schemas.json_schema._infer_type ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Infer more specific types.

Definition at line 193 of file json_schema.py.

◆ _merge_key()

formatron.schemas.json_schema._merge_key ( dict[str, typing.Any] schema,
str ref_key,
typing.Any reference_value )
protected

Definition at line 380 of file json_schema.py.

◆ _merge_referenced_schema()

formatron.schemas.json_schema._merge_referenced_schema ( dict[str, typing.Any] schema,
set[int] memo )
protected

Definition at line 361 of file json_schema.py.

◆ _obtain_type()

typing.Type[typing.Any|None] formatron.schemas.json_schema._obtain_type ( dict[str, typing.Any] schema,
dict[int, typing.Type] json_schema_id_to_schema )
protected

Directly obtain type information from this schema's type keyword.

Definition at line 324 of file json_schema.py.

◆ _recursive_resolve_reference()

formatron.schemas.json_schema._recursive_resolve_reference ( str base_uri,
typing.Any schema,
Registry registry,
set[int] memo )
protected

Definition at line 399 of file json_schema.py.

◆ _resolve_new_url()

str formatron.schemas.json_schema._resolve_new_url ( str uri,
str ref )
protected

◆ _resolve_reference()

formatron.schemas.json_schema._resolve_reference ( dict[str, typing.Any] schema,
str key,
typing.Any resolver )
protected

Definition at line 421 of file json_schema.py.

◆ _validate_json_schema()

None formatron.schemas.json_schema._validate_json_schema ( dict[str, typing.Any] schema)
protected

Definition at line 138 of file json_schema.py.

◆ create_schema()

schemas.schema.Schema formatron.schemas.json_schema.create_schema ( dict[str, typing.Any] schema,
registry = Registry() )

Create a Schema object from a JSON schema object.

This function takes a JSON schema and converts it into a Schema object that can be used for data validation and serialization. Currently, only the following JSON Schema features are supported:

  • type keyword
  • minLength, maxLength, pattern keywords for string type
  • substringOf keyword for string type
  • minimum, maximum, exclusiveMinimum, exclusiveMaximum keywords for number type and integer type
  • items keyword
    • optionally with minItems, maxItems, prefixItems constraints
  • properties keyword
    • Due to implementation limitations, we always assume additionalProperties is false.
    • Note that properties is optional for object type.
  • enum and const keyword
    • This includes advanced enum types such as array and object.
    • Note that if both enum(or const) and type are present, type will be ignored.
  • required keyword
  • anyOf keyword
  • Schema references ($ref and $dynamicRef)
    • Hence, all types of schema identifications($defs, $id, $anchor, $dynamicAnchor) are supported.
    • This includes recursive schema references.
      • Recursive array references(like [[[[...]]]]) are not supported yet.
    • Due to implementation limitations, duplicate constraint keywords in both referrers and referents are not allowed.
      • This bound is expected to be loosened in future versions of Formatron where "easily mergeable" constraint keywords will be merged.
Requirements
  • The input schema must be a valid JSON Schema according to the JSON Schema Draft 2020-12 standard
  • The root schema's type must be exactly "object" or "array" or both
  • The schema must have a valid '$id' and '$schema' fields
  • All references must be resolvable within the given schema and registry
Parameters
schemaA dictionary representing a valid JSON schema.
registryA Registry object containing additional schema definitions. Defaults to an empty Registry.
Returns
schemas A Schema object representing the input JSON schema.
Exceptions
jsonschemaIf the input schema is not a valid JSON Schema.
ValueErrorIf there are issues with schema references, constraints or requirements.

Definition at line 115 of file json_schema.py.

Variable Documentation

◆ _counter

int formatron.schemas.json_schema._counter
protected

Definition at line 67 of file json_schema.py.