Core

Schema

class belso.core.Schema[source]

Bases: object

A base class for defining schemas.

fields: ClassVar[List[BaseField]] = []
classmethod get_field_by_name(name)[source]

Get a field by its name.

Args

  • name (str): the name of the field.

Returns

  • Optional[belso.core.BaseField]: the field with the given name, or None if not found.

classmethod get_required_fields()[source]

Get the names of all required fields in the schema.

Returns

  • List[str]: a list of required field names.

Field

class belso.core.Field(name: str, type: Any, description: str = '', required: bool = True, default: Any | None = None, enum: List[Any] | None = None, range: tuple | None = None, exclusive_range: tuple | None = None, length_range: tuple | None = None, items_range: tuple | None = None, properties_range: tuple | None = None, regex: str | None = None, multiple_of: float | None = None, format: str | None = None)[source]

Bases: object

Factory class that returns the correct BaseField subtype (BaseField, NestedField, or ArrayField).

SchemaProcessor

class belso.core.SchemaProcessor[source]

Bases: object

A unified class for schema processing, including translation and validation. This class combines the functionality of the previous Translator and Validator classes.

static convert(schema, to, from_format=None)[source]

Convert a schema to a specific format. This method can automatically detect the input schema format and convert it to our internal format before translating to the target format.

Args

  • schema (Any): the schema to conver.

  • to (str): the target format. Can be a string or a belso.utils.FORMATS attribute.

  • from_format (Optional[str]): optional format hint for the input schema. If None, the format will be auto-detected. Defaults to None.

Returns

  • Dict[str, Any]

static detect_format(schema)[source]

Detect the format of a schema.

Args

  • schema (Any): the schema to detect.

Returns

  • str: the detected format as a string.

static display(schema, format_type=None)[source]

Pretty-print a schema using colors and better layout, including nested fields.

Args

  • schema (Any): the schema to print.

  • format_type (Optional[str]): format of the schema. Defaults to None.

static load(path, standardize=True)[source]

Load a schema from a file in the specified format.

Args

  • path (Union[str, Path]): the path to the file to load.

  • standardize (bool): whether to convert the schema to our internal ‘belso’ format. Defaults to True.

Returns

  • Any: the loaded schema.

static save(schema, path)[source]

Save a schema to a file in the specified format.

Args

  • schema (Any): the schema to save.

  • path (Union[str, Path]): the path to save the schema to.

static standardize(schema, from_format=None)[source]

Convert a schema from a specific format to our internal ‘belso’ format. If from_format is not specified, it will be auto-detected.

Args

  • schema (Any): the schema to convert.

  • from_format (Optional[str]): the format of the input schema. If None, the format will be auto-detected. Defaults to None.

Returns

  • Type[belso.Schema]: the converted belso schema.

static validate(data, schema)[source]

Validate that the provided data conforms to the given schema.

Args

  • data (Union[Dict[str, Any], str]): the data to validate (either a dict or JSON string).

  • schema (Type[belso.Schema]): the schema to validate against.

Returns:

  • Dict[str, Any]: the validated data.