You may use schema (PyPi Link)

schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.

Copyfrom schema import Schema, And, Use, Optional, SchemaError

def check(conf_schema, conf):
    try:
        conf_schema.validate(conf)
        return True
    except SchemaError:
        return False

conf_schema = Schema({
    'version': And(Use(int)),
    'info': {
        'conf_one': And(Use(float)),
        'conf_two': And(Use(str)),
        'conf_three': And(Use(bool)),
        Optional('optional_conf'): And(Use(str))
    }
})

conf = {
    'version': 1,
    'info': {
        'conf_one': 2.5,
        'conf_two': 'foo',
        'conf_three': False,
        'optional_conf': 'bar'
    }
}

print(check(conf_schema, conf))
Answer from Danil Speransky on Stack Overflow
🌐
jsonschema
python-jsonschema.readthedocs.io › en › latest › validate
Schema Validation - jsonschema 4.26.1.dev25+gad0a1b301 documentation
jsonschema tries to strike a balance between performance in the common case and generality. For instance, JSON Schema defines a number type, which can be validated with a schema such as {"type" : "number"}. By default, this will accept instances of Python numbers.Number.
🌐
PyPI
pypi.org › project › schema
schema · PyPI
schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.
      » pip install schema
    
Published   Oct 11, 2025
Version   0.7.8
Top answer
1 of 10
76

You may use schema (PyPi Link)

schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.

Copyfrom schema import Schema, And, Use, Optional, SchemaError

def check(conf_schema, conf):
    try:
        conf_schema.validate(conf)
        return True
    except SchemaError:
        return False

conf_schema = Schema({
    'version': And(Use(int)),
    'info': {
        'conf_one': And(Use(float)),
        'conf_two': And(Use(str)),
        'conf_three': And(Use(bool)),
        Optional('optional_conf'): And(Use(str))
    }
})

conf = {
    'version': 1,
    'info': {
        'conf_one': 2.5,
        'conf_two': 'foo',
        'conf_three': False,
        'optional_conf': 'bar'
    }
}

print(check(conf_schema, conf))
2 of 10
36

Use Pydantic!

Pydantic enforces type hints at runtime, and provides user friendly errors when data is invalid. Define how data should be in pure, canonical python; validate it with pydantic, as simple as that:

Copyfrom pydantic import BaseModel


class Info(BaseModel):
    conf_one: float
    conf_two: str
    conf_three: bool

    class Config:
        extra = 'forbid'


class ConfStructure(BaseModel):
    version: int
    info: Info

If validation fails pydantic will raise an error with a breakdown of what was wrong:

Copymy_conf_wrong = {
    'version': 1,

    'info': {
        'conf_one': 2.5,
        'conf_two': 'foo',
        'conf_three': False,
        'optional_conf': 'bar'
    }
}

my_conf_right = {
    'version': 10,

    'info': {
        'conf_one': 14.5,
        'conf_two': 'something',
        'conf_three': False
    }
}

model = ConfStructure(**my_conf_right)
print(model.dict())
# {'version': 10, 'info': {'conf_one': 14.5, 'conf_two': 'something', 'conf_three': False}}

res = ConfStructure(**my_conf_wrong)
# pydantic.error_wrappers.ValidationError: 1 validation error for ConfStructure
#     info -> optional_conf
# extra fields not permitted (type=value_error.extra)
🌐
Python-cerberus
docs.python-cerberus.org › schemas.html
Validation Schemas - Cerberus — Data validation for Python
A validation schema is a mapping, usually a dict. Schema keys are the keys allowed in the target dictionary.
🌐
jsonschema
python-jsonschema.readthedocs.io › en › stable › validate
Schema Validation - jsonschema 4.26.0 documentation
jsonschema tries to strike a balance between performance in the common case and generality. For instance, JSON Schema defines a number type, which can be validated with a schema such as {"type" : "number"}. By default, this will accept instances of Python numbers.Number.
🌐
Code Like A Girl
code.likeagirl.io › python-schema-validation-with-marshmallow-d4c3b9752655
Python Schema Validation with Marshmallow | by Python Code Nemesis | Code Like A Girl
February 27, 2023 - You can use the Marshmallow library in Python to validate database schema by defining a schema class for each table in your database and using the Marshmallow validate method to validate user input against the schema.
🌐
GitHub
github.com › keleshev › schema
GitHub - keleshev/schema: Schema validation just got Pythonic · GitHub
schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.
Starred by 2.9K users
Forked by 217 users
Languages   Python
🌐
Pydantic
docs.pydantic.dev › latest
Welcome to Pydantic - Pydantic Validation
Powered by type hints — with Pydantic, schema validation and serialization are controlled by type annotations; less to learn, less code to write, and integration with your IDE and static analysis tools. Learn more… · Speed — Pydantic's core validation logic is written in Rust. As a result, Pydantic is among the fastest data validation libraries for Python...
Find elsewhere
🌐
GitHub
github.com › vajol › python-data-engineering-resources › blob › main › resources › data-schema-validation.md
python-data-engineering-resources/resources/data-schema-validation.md at main · vajol/python-data-engineering-resources
Description: Pydantic is a data validation and settings management library using Python type annotations. It allows for data parsing and validation using Python's standard typing module, ensuring that the data conforms to defined schemas.
Author   vajol
🌐
Python-cerberus
docs.python-cerberus.org
Cerberus — Data validation for Python
>>> schema = {'name': {'type': 'string'}} >>> v = Validator(schema)
🌐
Yeah Hub
yeahhub.com › 7-best-python-libraries-validating-data
7 Best Python Libraries for Validating Data – Yeah Hub
Schema is a library for validating Python data structures, such as those obtained from config-files, forms, external services or command-line parsing, converted from JSON/YAML (or something else) to Python data-types.
🌐
Horejsek
horejsek.github.io › python-fastjsonschema
Fast JSON schema for Python — fastjsonschema documentation
Generates validation code for validating JSON schema passed in definition. Example: import fastjsonschema code = fastjsonschema.compile_to_code({'type': 'string'}) with open('your_file.py', 'w') as f: f.write(code) ... echo "{'type': 'string'}" | python3 -m fastjsonschema > your_file.py python3 ...
🌐
Python.org
discuss.python.org › python help
JSON Schema validation - Python Help - Discussions on Python.org
January 23, 2023 - What is the recommended package to use for validating json data with a schema? I see schema, schema · PyPI I also see Schema Validation - jsonschema 4.17.4.dev60+g65afdce documentation I was using the 1st one, schema, and was trying to understand how to represent tuples and then realized, ...
🌐
Pydantic
docs.pydantic.dev › latest › concepts › json_schema
JSON Schema - Pydantic Validation
Specify the mode of JSON schema generation via the mode parameter in the model_json_schema and TypeAdapter.json_schema methods. By default, the mode is set to 'validation', which produces a JSON schema corresponding to the model's validation schema.
🌐
Readthedocs
python-jsonschema.readthedocs.io › en › v4.8.0 › validate
Schema Validation - jsonschema 4.8.0 documentation
The protocol to which all validator classes should adhere. ... schema – the schema that the validator object will validate with. It is assumed to be valid, and providing an invalid schema can lead to undefined behavior.
🌐
PyPI
pypi.org › project › jsonschema
jsonschema · PyPI
>>> from jsonschema import validate >>> # A sample schema, like what we'd get from json.load() >>> schema = { ... "type" : "object", ... "properties" : { ... "price" : {"type" : "number"}, ... "name" : {"type" : "string"}, ... }, ... } >>> # If no exception is raised by validate(), the instance is valid.
      » pip install jsonschema
    
Published   Jan 07, 2026
Version   4.26.0
🌐
Andrewvillazon
andrewvillazon.com › validate-yaml-python-schema
Validate YAML in Python with Schema
from schema import Schema, SchemaError import yaml config_schema = Schema({ "application": { "logging": { "filename": lambda fp: fp.endswith(".log") } } }, ignore_extra_keys=True) conf_as_yaml = """ application: database: connection_string: sqlite:///app.db logging: filename: logs.log concurrency: workers: 6 """ configuration = yaml.safe_load(conf_as_yaml) try: config_schema.validate(configuration) print("Configuration is valid.") except SchemaError as se: raise se · Configuration is valid. Notice how the logging key is validated, but the surrounding keys are not. Another option for ignoring keys is to define a rule using the object type. Because every object in Python is an object type, these keys are always valid.
🌐
Built In
builtin.com › software-engineering-perspectives › python-json-schema
How to Use JSON Schema to Validate JSON Documents in Python | Built In
It supports nested objects, arrays, and uses $defs for reusable code. A Validator instance allows for efficient multi-document validation. In Python, the JSON Schema library can be used to validate a JSON document against a schema.