Schema

The Schema class validates a dictionary of data against a set of validators — one validator per field.

Basic usage

from valify import Schema, StringValidator, IntValidator

schema = Schema({
    "name": StringValidator(min_length=2),
    "age":  IntValidator(min_value=0),
})

result = schema.validate({"name": "Alice", "age": 30})

Strict mode

By default extra fields in the data are silently ignored. In strict mode they raise ValidationError:

schema = Schema(
    {"name": StringValidator()},
    strict=True,
)

# Raises ValidationError — 'extra' is not in the schema
schema.validate({"name": "Alice", "extra": "field"})

Nested schemas

Schema inherits from Validator, so it can be used as a field value inside another schema:

address_schema = Schema({
    "city": StringValidator(),
    "pin":  StringValidator(min_length=6),
})

user_schema = Schema({
    "name":    StringValidator(),
    "address": address_schema,
})

API reference

class valify.schema.Schema(fields: dict[str, Validator], *, strict: bool = False)[source]

Bases: Validator

Validates a dictionary against a set of validators.

Parameters:
  • fields (dict) – A mapping of field names to Validator instances.

  • strict (bool) – If True, raise ValidationError for keys in the data that are not defined in the schema. Defaults to False.

Example

schema = Schema({

“name”: StringValidator(min_length=2), “age”: IntValidator(min_value=0, max_value=120),

})

result = schema.validate({“name”: “Alice”, “age”: 30}) print(result) # {“name”: “Alice”, “age”: 30}

classmethod from_example(example: dict[str, Any]) Schema[source]

Generate a Schema automatically from a sample data dictionary.

Inspects the type of each value and maps it to the appropriate validator. Supports nested dictionaries recursively.

Parameters:

example (dict) – A sample dictionary representing the expected data shape.

Returns:

A Schema instance with inferred validators.

Return type:

Schema

Example

schema = Schema.from_example({

“name”: “Alice”, “age”: 30, “email”: “alice@example.com”, “score”: 9.5, “active”: True,

}) schema.validate({

“name”: “Bob”, “age”: 25, “email”: “bob@example.com”, “score”: 8.0, “active”: False,

})

to_json_schema() dict[str, Any][source]

Returns a JSON Schema representation of this validator.

Returns:

JSON schema fragment describing the validator.

Return type:

dict

validate(data: dict[str, Any]) dict[str, Any][source]

Validate a dictionary of data against the schema.

Parameters:

data (dict) – The raw data to validate.

Returns:

A new dictionary containing only the validated (and possibly coerced) values.

Return type:

dict

Raises:

Auto-generating Schemas

Use Schema.from_example() to automatically generate a Schema from a sample data dictionary:

schema = Schema.from_example({
    "name":    "Alice",
    "age":     30,
    "email":   "alice@example.com",
    "score":   9.5,
    "active":  True,
    "address": {
        "city": "Pune",
        "pin":  "411001",
    },
    "tags": ["python", "developer"],
})

# Automatically inferred:
# name    → StringValidator
# age     → IntValidator
# email   → EmailValidator
# score   → FloatValidator
# active  → BoolValidator
# address → nested Schema
# tags    → ListValidator(StringValidator)

result = schema.validate({
    "name":    "Bob",
    "age":     25,
    "email":   "bob@example.com",
    "score":   8.0,
    "active":  False,
    "address": {"city": "Mumbai", "pin": "400001"},
    "tags":    ["django", "fastapi"],
})