{"id":1412,"library":"cerberus","title":"Cerberus: Lightweight Schema Validator","description":"Cerberus is a lightweight, extensible data validation library for Python dictionaries. It allows you to define a schema and then validate whether a given dictionary conforms to that schema, supporting various data types, constraints, and custom validation rules. The current version is 1.3.8, and it maintains a stable release cadence with incremental updates within the 1.x series.","status":"active","version":"1.3.8","language":"en","source_language":"en","source_url":"https://github.com/pyeve/cerberus","tags":["validation","schema","data-validation","dictionaries"],"install":[{"cmd":"pip install cerberus","lang":"bash","label":"Install Cerberus"}],"dependencies":[],"imports":[{"symbol":"Validator","correct":"from cerberus import Validator"}],"quickstart":{"code":"from cerberus import Validator\n\nschema = {\n    'name': {'type': 'string', 'minlength': 3, 'maxlength': 10, 'required': True},\n    'age': {'type': 'integer', 'min': 0, 'max': 99, 'nullable': True},\n    'email': {'type': 'string', 'regex': '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\\\.[a-zA-Z]{2,}$'}\n}\n\ndocument = {\n    'name': 'John Doe',\n    'age': 30,\n    'email': 'john.doe@example.com'\n}\n\nv = Validator(schema)\n\nif v.validate(document):\n    print('Document is valid.')\n    print(f'Normalized document: {v.normalized(document)}')\nelse:\n    print('Document is invalid.')\n    print(f'Errors: {v.errors}')\n\n# Example with an invalid document\ninvalid_document = {\n    'name': 'Jo',\n    'age': 150,\n    'city': 'New York' # Unknown field\n}\n\nif not v.validate(invalid_document):\n    print(f'Invalid document errors: {v.errors}')\n","lang":"python","description":"This quickstart demonstrates defining a schema with various rules (type, length, required, regex, min/max, nullable). It then creates a Validator instance, validates a sample document, and prints success or error messages. It also includes an example of an invalid document to show error reporting."},"warnings":[{"fix":"If your application relies on unknown fields being allowed, explicitly set `allow_unknown=True` when initializing the `Validator` (`v = Validator(schema, allow_unknown=True)`) or define `allow_unknown: True` in relevant schema rules.","message":"Starting with Cerberus 1.3.0, the `allow_unknown` validator option (which controls whether unknown fields in a document are allowed) now defaults to `False`. Previously, it implicitly allowed unknown fields.","severity":"breaking","affected_versions":">=1.3.0"},{"fix":"Be aware of this order. If your intent is to validate the type *after* coercion, ensure your `coerce` rule correctly handles the initial type, or apply type validation in a custom rule that runs post-coercion if necessary.","message":"Since Cerberus 1.1, `type` schema rules are processed *before* `coerce` rules. This means type validation is performed on the original value, not the coerced value.","severity":"gotcha","affected_versions":">=1.1.0"},{"fix":"Understand the distinction: `{'field': {'required': True, 'nullable': False}}` requires the field and its value cannot be `None`. `{'field': {'required': False, 'nullable': True}}` makes the field optional, but if present, its value can be `None`.","message":"The `required: True` and `nullable: True` schema rules are orthogonal. `required: True` means the key *must* exist in the document. `nullable: True` means that if the key exists, its value *can* be `None`.","severity":"gotcha","affected_versions":"All 1.x versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}