### Installing json-schema-to-pydantic Library - Bash Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This snippet demonstrates how to install the `json-schema-to-pydantic` library using `pip`, the standard Python package installer. This command fetches the latest version of the library from PyPI and makes it available in your Python environment. ```Bash pip install json-schema-to-pydantic ``` -------------------------------- ### Installing Development Dependencies with uv - Bash Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This command installs all development dependencies for the project using `uv`, a fast Python package installer. The `-e ".[dev]"` flag installs the package in editable mode and includes optional development dependencies. ```Bash uv pip install -e ".[dev]" ``` -------------------------------- ### Installing Development Dependencies with pip - Bash Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This command installs all development dependencies for the project using `pip`. The `-e ".[dev]"` flag installs the package in editable mode and includes optional development dependencies, useful for local development and testing. ```Bash pip install -e ".[dev]" ``` -------------------------------- ### Generating Pydantic Model from JSON Schema - Python Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This snippet demonstrates how to define a JSON Schema and use `create_model` from `json_schema_to_pydantic` to generate a Pydantic model. It shows how to instantiate the generated model with data and also includes an example of relaxing array item validation using `allow_undefined_array_items=True`. ```Python from json_schema_to_pydantic import create_model # Define your JSON Schema schema = { "title": "User", "type": "object", "properties": { "name": {"type": "string"}, "email": {"type": "string", "format": "email"}, "age": {"type": "integer", "minimum": 0} }, "required": ["name", "email"] } # Generate your Pydantic model UserModel = create_model(schema) # Use the model user = UserModel( name="John Doe", email="john@example.com", age=30 ) # Example with relaxed array item validation RelaxedModel = create_model( {"type": "object", "properties": {"tags": {"type": "array"}}}, allow_undefined_array_items=True ) relaxed_instance = RelaxedModel(tags=[1, "two", True]) ``` -------------------------------- ### Defining String Formats in JSON Schema Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/docs/features.md This JSON Schema snippet demonstrates how to define string properties with specific formats like 'email', 'uri', and 'date-time'. These formats guide the library in mapping them to appropriate Python types such as `datetime.datetime`, `pydantic.AnyUrl`, or validating email strings. ```json { "type": "object", "properties": { "email": {"type": "string", "format": "email"}, "website": {"type": "string", "format": "uri"}, "created_at": {"type": "string", "format": "date-time"} } } ``` -------------------------------- ### Creating Discriminated Unions with oneOf in JSON Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/docs/features.md This JSON Schema example uses `oneOf` to define a discriminated union, where an instance must validate against *exactly one* of the provided subschemas. The `type` property with a `const` value acts as the discriminator, allowing different structures based on its value (e.g., 'user' or 'admin'). ```json { "oneOf": [ { "type": "object", "properties": { "type": {"const": "user"}, "email": {"type": "string", "format": "email"} } }, { "type": "object", "properties": { "type": {"const": "admin"}, "permissions": {"type": "array", "items": {"type": "string"}} } } ] } ``` -------------------------------- ### Running Tests - Bash Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This command executes the test suite for the `json-schema-to-pydantic` project using `pytest`. Running tests is crucial to ensure that all changes and new features function as expected and do not introduce regressions. ```Bash pytest ``` -------------------------------- ### Using PydanticModelBuilder for Advanced Model Creation - Python Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This snippet illustrates the use of `PydanticModelBuilder` for more complex scenarios, providing direct control over the model generation process. It allows for advanced customization and integration beyond the simpler `create_model` function. ```Python from json_schema_to_pydantic import PydanticModelBuilder builder = PydanticModelBuilder() model = builder.create_pydantic_model(schema, root_schema) ``` -------------------------------- ### Handling Schema Generation Errors - Python Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/README.md This code demonstrates how to implement robust error handling when generating Pydantic models from JSON Schema. It shows how to catch specific exceptions like `TypeError` for invalid types and `ReferenceError` for issues with schema references, allowing for graceful failure and informative error messages. ```Python from json_schema_to_pydantic import ( SchemaError, # Base class for all schema errors TypeError, # Invalid or unsupported type CombinerError, # Error in schema combiners ReferenceError, # Error in schema references ) try: model = create_model(schema) except TypeError as e: print(f"Invalid type in schema: {e}") except ReferenceError as e: print(f"Invalid reference: {e}") ``` -------------------------------- ### Combining Schemas with allOf in JSON Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/docs/features.md This JSON Schema snippet demonstrates the `allOf` keyword, which combines multiple subschemas. An instance is considered valid only if it validates successfully against *all* schemas listed in the `allOf` array, effectively merging their properties and constraints. ```json { "allOf": [ {"type": "object", "properties": {"id": {"type": "integer"}}}, {"type": "object", "properties": {"name": {"type": "string"}}} ] } ``` -------------------------------- ### Using Nested Local References in Array Items (JSON) Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/docs/features.md This JSON Schema illustrates how local `$ref` can be used to define complex nested structures, specifically within array items. It defines `ComplexItem` and `NestedItem` schemas, demonstrating that arrays can contain items that are themselves defined by references, allowing for modular and reusable schema components. ```json { "type": "object", "properties": { "items": { "type": "array", "items": {"$ref": "#/definitions/ComplexItem"} } }, "definitions": { "NestedItem": { "type": "object", "properties": { "name": {"type": "string"}, "value": {"type": "integer"} } }, "ComplexItem": { "type": "object", "properties": { "id": {"type": "string"}, "nested_items": { "type": "array", "items": {"$ref": "#/definitions/NestedItem"} } } } } } ``` -------------------------------- ### Allowing Undefined Array Items in Pydantic Model Creation (Python) Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/docs/features.md This Python code illustrates how to use the `allow_undefined_array_items=True` option when calling `create_model`. This allows the library to generate a Pydantic model from a JSON Schema where an array property lacks an `items` definition, defaulting its type to `List[Any]` instead of raising a `TypeError`. ```python from json_schema_to_pydantic import create_model # Schema with an array lacking 'items' schema = {"type": "object", "properties": {"mixed_tags": {"type": "array"}}} # This would raise a TypeError by default # model = create_model(schema) # Allow arrays without 'items' RelaxedModel = create_model(schema, allow_undefined_array_items=True); # The field 'mixed_tags' will be List[Any] instance = RelaxedModel(mixed_tags=[1, "string", True, None]) ``` -------------------------------- ### Defining Recursive Local References in JSON Schema Source: https://github.com/richard-gyiko/json-schema-to-pydantic/blob/main/docs/features.md This JSON Schema demonstrates the use of local `$ref` to define recursive data structures. The `Node` definition references itself within its `children` array, allowing for hierarchical data representation and showcasing the library's support for circular references. ```json { "type": "object", "properties": { "parent": {"$ref": "#/definitions/Node"} }, "definitions": { "Node": { "type": "object", "properties": { "children": { "type": "array", "items": {"$ref": "#/definitions/Node"} } } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.