{"id":946,"library":"schema","title":"Schema","description":"Schema is a Python library for validating data structures, such as those obtained from config files, forms, external services, or command-line parsing, often converted from JSON/YAML to Python data types. It is actively maintained with moderate release frequency, typically addressing bug fixes and minor features, with occasional larger updates.","status":"active","version":"0.7.8","language":"python","source_language":"en","source_url":"https://github.com/keleshev/schema","tags":["validation","data validation","schema","json schema","type checking"],"install":[{"cmd":"pip install schema","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"Schema","correct":"from schema import Schema"},{"symbol":"And","correct":"from schema import And"},{"symbol":"Or","correct":"from schema import Or"},{"symbol":"Use","correct":"from schema import Use"},{"symbol":"Optional","correct":"from schema import Optional"},{"symbol":"Regex","correct":"from schema import Regex"},{"symbol":"SchemaError","correct":"from schema import SchemaError"}],"quickstart":{"code":"from schema import Schema, And, Use, Optional, SchemaError\n\nschema = Schema(\n    [\n        {\n            \"name\": And(str, len),\n            \"age\": And(Use(int), lambda n: 18 <= n <= 99),\n            Optional(\"gender\"): And(str, Use(str.lower), lambda s: s in (\"male\", \"female\", \"other\")),\n        }\n    ]\n)\n\ndata = [\n    {\"name\": \"Sue\", \"age\": \"28\", \"gender\": \"Female\"},\n    {\"name\": \"Sam\", \"age\": \"42\"},\n    {\"name\": \"Sacha\", \"age\": \"20\", \"gender\": \"Other\"},\n]\n\ntry:\n    validated_data = schema.validate(data)\n    print(\"Validation successful!\")\n    print(validated_data)\nexcept SchemaError as e:\n    print(f\"Validation failed: {e}\")\n\n# Example of invalid data\ninvalid_data = [\n    {\"name\": \"Alice\", \"age\": \"15\"} # Age too young\n]\n\ntry:\n    schema.validate(invalid_data)\nexcept SchemaError as e:\n    print(f\"Validation failed for invalid data: {e}\")","lang":"python","description":"This quickstart defines a schema for a list of personal information entries, including validation for name length, age range (with type conversion), and an optional gender field with specific allowed values. It then attempts to validate both valid and invalid data, demonstrating how `Schema.validate()` works and how `SchemaError` is raised for invalid input."},"warnings":[{"fix":"Ensure you are importing from the correct library based on whether you need a Pythonic schema definition or adherence to the JSON Schema specification.","message":"Do not confuse `schema` (keleshev/schema) with `jsonschema`. This library provides a Pythonic way to define and validate data structures, and can generate JSON schema from its definitions. `jsonschema` is a separate library that strictly implements the JSON Schema specification.","severity":"gotcha","affected_versions":"All versions"},{"fix":"When catching `SchemaError`, consider if more granular handling is needed for `SchemaWrongKey`, `SchemaMissingKeyError`, or other specific subclasses. Generally, catching the base `SchemaError` is sufficient for overall validation failure.","message":"Since version 0.6.2, `schema.SchemaError` has specific subclasses like `SchemaWrongKey` and `SchemaMissingKeyError`. While `except SchemaError` will still catch these, code that relies on precise error type checking for key-related issues (e.g., `isinstance(e, SchemaWrongKey)`) may need to be updated if it was written before these subclasses were introduced and expected only the base `SchemaError` class for specific key problems.","severity":"gotcha","affected_versions":"0.6.2 and later"},{"fix":"Always use `schema` with a currently supported Python version (e.g., Python 3.8+). Update your Python environment if necessary.","message":"The `schema` library was tested against older Python versions (e.g., Python 2.6, 2.7, 3.2-3.9). These older Python versions are no longer officially supported by the Python Software Foundation and may have security vulnerabilities or compatibility issues with modern libraries.","severity":"deprecated","affected_versions":"< 0.7.0 (for explicit Python 2/older 3 support), all versions when used with EOL Python runtimes"},{"fix":"Ensure you are using version 0.6.4 or later if you experienced unexpected behavior with `Optional` fields in older installations. Always test `Optional` field behavior thoroughly after any major version upgrade.","message":"An issue related to `Optional` fields was reverted in version 0.6.4 (\"Revert the optional error commit\"). This implies a temporary instability or unexpected behavior regarding `Optional` field validation in an intermediate version prior to 0.6.4. Users upgrading from a version that contained this problematic change might have encountered validation issues.","severity":"gotcha","affected_versions":"Potentially a specific, short-lived version prior to 0.6.4"}],"env_vars":null,"search_vec":"'activ':36 'address':43 'bug':44 'check':60 'command':23 'command-lin':22 'config':16 'convert':27 'data':9,32,54 'extern':19 'featur':48 'file':17 'fix':45 'form':18 'frequenc':41 'json':57 'json/yaml':29 'larger':51 'librari':6 'line':24 'maintain':37 'minor':47 'moder':39 'obtain':14 'occasion':50 'often':26 'pars':25 'python':5,31 'releas':40 'schema':1,2,56,58 'servic':20 'structur':10 'type':33,59 'typic':42 'updat':52 'valid':8,53,55","created_at":"2026-03-29T06:08:58.062893+00:00","updated_at":"2026-04-17T15:14:57.370084+00:00","problems":[{"fix":"Rename the local 'schema.py' file to avoid conflicts with the 'schema' library.","cause":"This error occurs when there's a naming conflict between a local module named 'schema' and the installed 'schema' library, leading Python to import the local module instead.","error":"ImportError: cannot import name 'Base' from 'schema'"},{"fix":"Install the 'schema' library using pip: 'pip install schema'.","cause":"This error occurs when the 'schema' library is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'schema'"},{"fix":"Ensure that the data being validated matches the expected type; for example, convert strings to integers before validation.","cause":"This error occurs when the data being validated does not match the expected type defined in the schema.","error":"schema.SchemaError: '123' should be instance of 'int'"},{"fix":"Verify that the data being validated meets the conditions of the validation function; for example, check that the file or directory exists.","cause":"This error occurs when a validation function, such as 'os.path.exists', returns False for the provided data.","error":"schema.SchemaError: exists('./non-existent/') should evaluate to True"},{"fix":"Ensure the input data dictionary contains all keys specified as required in the schema, or mark keys as `Optional` in the schema definition if they are not always present.","cause":"The input data is missing one or more keys that are defined as required in the schema.","error":"schema.SchemaError: Missing keys: 'some_key'"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.7.8","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/keleshev/schema","docs":null,"changelog":null,"pypi":"https://pypi.org/project/schema/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["serialization","data"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-27","next_check":"2026-07-28","install_tag":"verified"}}