Skip to main content

Utilities for working with JSON Schema.

Project description

jsonschematools

Description

jsonschematools is a Python library that provides utilities for working with JSON Schema. It includes features for adding and updating enums in the $defs section of a JSON schema, and updating a property of a JSON schema with an existing enum reference.

Installation

To install jsonschematools, you can use pip:

pip install jsonschematools

Usage

Here is a basic example of how to use jsonschematools:

from jsonschematools.enums import add_or_update_enum_in_defs, update_property_with_enum

# Define a basic schema
schema = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "tags": {"type": "array", "items": {"type": "string"}},
    },
    "$defs": {},
}

# Add a new enum to the schema
schema = add_or_update_enum_in_defs(schema, "Colors", ["Red", "Blue", "Green"])

# schema now looks like this:
# {
#   "type": "object",
#   "properties": {
#     "name": {
#       "type": "string"
#     },
#     "tags": {
#       "type": "array",
#       "items": {
#         "type": "string"
#       }
#     }
#   },
#   "$defs": {
#     "Colors": {
#       "type": "string",
#       "enum": [
#         "Red",
#         "Blue",
#         "Green"
#       ]
#     }
#   }
# }

# Update a property with the new enum
schema = update_property_with_enum(schema, "Colors", "tags")

# schema now looks like this:
# {
#   "type": "object",
#   "properties": {
#     "name": {
#       "type": "string"
#     },
#     "tags": {
#       "type": "array",
#       "items": {
#         "$ref": "#/$defs/Colors"
#       }
#     }
#   },
#   "$defs": {
#     "Colors": {
#       "type": "string",
#       "enum": [
#         "Red",
#         "Blue",
#         "Green"
#       ]
#     }
#   }
# }

Testing

Tests are written using pytest. To run the tests, use the following command:

pytest

Contributing

Contributions are welcome! Please fork the repository and create a pull request with your changes.

License

jsonschematools is licensed under the MIT License.

Contact

For any questions or concerns, please open an issue on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonschematools-0.2.0.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

jsonschematools-0.2.0-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page