Convert JSON Schema to Confuse templates
Project description
confuse-jsonschema
Convert JSON Schema to Confuse templates for seamless configuration validation.
Overview
confuse-jsonschema is a library that allows you to create a
Confuse template from a JSON schema.
This allows you to benefit from the advanced validation capabilities of
JSON schema for checking your configuration, while taking advantage of the
flexibility of confuse for configuration management.
Installation
pip install confuse-jsonschema
Quick Start
from confuse_jsonschema import to_template
import confuse
# Define your configuration schema
schema = {
"type": "object",
"properties": {
"server": {
"type": "object",
"properties": {
"host": {"type": "string", "default": "localhost"},
"port": {"type": "integer", "default": 8080}
},
"required": ["host"]
},
"database": {
"type": "object",
"properties": {
"url": {"type": "string"},
"timeout": {"type": "number", "default": 30.0}
},
"required": ["url"]
}
},
"required": ["server", "database"]
}
# Convert to Confuse template
template = to_template(schema)
# Use with Confuse
config = confuse.Configuration('myapp')
validated_config = config.get(template)
Supported JSON Schema Features
Fully Supported
Basic Types
string- converts tostror customSchemaStringwith constraintsinteger- converts tointor customSchemaIntegerwith constraintsnumber- converts tofloator customSchemaNumberwith constraintsboolean- converts toboolor default valuenull- converts toNoneor default value
Advanced Types
const- fixed constant valuesenum- choice from enumerated values usingconfuse.Choice- Multiple types (e.g.,
["string", "integer"]) - converts toconfuse.OneOf
Complex Types
object- converts to nested dictionary templatearray- converts toconfuse.SequenceorSchemaSequencewith constraints
String Constraints
minLength/maxLength- enforced viaSchemaStringpattern- regex validation viaSchemaStringformat- special formats likepath,uri-referenceconvert toconfuse.Filename
Numeric Constraints
minimum/maximum- inclusive bounds viaSchemaInteger/SchemaNumberexclusiveMinimum/exclusiveMaximum- exclusive boundsmultipleOf- divisibility validation
Array Constraints
minItems/maxItems- size validation viaSchemaSequenceuniqueItems- uniqueness validation viaSchemaSequenceitems- item type specification
Object Features
properties- property definitionsrequired- required properties (others becomeconfuse.Optional)default- default values
Logical Operators
anyOf- any schema can match usingconfuse.OneOfoneOf- exactly one schema must match usingconfuse.OneOfallOf- all schemas must match using customAllOftemplateif/then/else- basic conditional support
Partially Supported
References
$ref- basic placeholder support (returns genericconfuse.Template)- Limitation: No actual reference resolution implemented
- Workaround: Manually inline referenced schemas
Object Constraints
-
additionalProperties- recognized but not enforced- Limitation: Confuse templates use fixed dictionary structures
- Impact: Additional properties will be accepted without validation
-
patternProperties- recognized but not enforced- Limitation: Confuse doesn't support dynamic property validation based on key patterns
- Impact: Pattern-based property validation is ignored
Complex Logic
if/then/else- basic implementation- Limitation: Doesn't evaluate conditions; just uses
thenorelse - Impact: Conditional validation doesn't work properly
- Limitation: Doesn't evaluate conditions; just uses
Not Supported
Advanced JSON Schema Features
-
Property Dependencies
{ "dependencies": { "credit_card": ["billing_address"] } }
No concept of field interdependencies
-
Conditional Validation
{ "if": {"properties": {"type": {"const": "credit_card"}}}, "then": {"required": ["number", "cvv"]} }
Dynamic validation based on other field values isn't supported
-
Complex Array Validation
{ "prefixItems": [{"type": "string"}, {"type": "number"}], "items": false }
Confuse sequences validate all items uniformly
-
Property Name Validation
{ "propertyNames": {"pattern": "^[A-Za-z_][A-Za-z0-9_]*$"} }
No validation of dictionary key names
-
Format Validation
{"type": "string", "format": "email"}
Most JSON Schema formats aren't supported (only
pathanduri-reference) -
Schema Metadata
title,description,examples- ignored (no impact on validation)$id,$schema- ignoreddeprecated- ignored
Limitations
-
Configuration Layering Conflicts
- JSON Schema defines single validation rules
- Confuse merges multiple configuration sources
- Impact: Schema validation happens after configuration merging
-
Type Coercion Differences
- JSON Schema: strict type validation
- Confuse: automatic type coercion (e.g., string "123" → integer 123)
- Impact: Some invalid values might be accepted after coercion
-
Error Reporting
- JSON Schema: detailed validation error paths
- Confuse: simpler error messages
- Impact: Less precise error information for complex schemas
-
Runtime vs Static Validation
- JSON Schema: can validate any data structure
- Confuse: designed for configuration files with known structure
- Impact: Less flexibility for dynamic data validation
Examples
Simple Configuration
schema = {
"type": "object",
"properties": {
"app_name": {"type": "string", "default": "MyApp"},
"debug": {"type": "boolean", "default": False},
"port": {"type": "integer", "default": 3000}
}
}
template = to_template(schema)
Workarounds for Unsupported Features
# Instead of $ref, inline the schema
# Not supported:
schema_with_ref = {
"type": "object",
"properties": {
"user": {"$ref": "#/definitions/User"}
},
"definitions": {
"User": {
"type": "object",
"properties": {"name": {"type": "string"}}
}
}
}
# Use this instead:
schema_inlined = {
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {"name": {"type": "string"}}
}
}
}
Format Validation
# Most formats aren't supported - use pattern instead
# Not supported:
{"type": "string", "format": "email"}
# Use pattern instead:
{"type": "string", "pattern": "^[^@]+@[^@]+\\.[^@]+$"}
# Supported formats:
{"type": "string", "format": "path"} # → confuse.Filename
{"type": "string", "format": "uri-reference"} # → confuse.Filename
Development
Setup
git clone https://github.com/chmduquesne/confuse-jsonschema.git
cd confuse-jsonschema
pip install -e ".[dev]"
Running Tests
pytest
Code Formatting
black .
flake8
Publishing to PyPI
- Build the package:
python -m build
- Upload to PyPI:
python -m twine upload dist/*
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file confuse_jsonschema-0.1.0.tar.gz.
File metadata
- Download URL: confuse_jsonschema-0.1.0.tar.gz
- Upload date:
- Size: 21.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a006d091c214eb05a488fd12e5b94580e60d797dac85214113013257a7415950
|
|
| MD5 |
c30ebbd972125f2f2560b90e6e41b749
|
|
| BLAKE2b-256 |
9695fc683b3c199e9e3225650a533b53a4f91981ef53860021f2487ff3dbb8e9
|
File details
Details for the file confuse_jsonschema-0.1.0-py3-none-any.whl.
File metadata
- Download URL: confuse_jsonschema-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
106d7a48c502326797c6faafe3e5b12cbc18087cb51d54459274de70d1aa4348
|
|
| MD5 |
c0fee54ccccd90a31dbb1ee459c3b50e
|
|
| BLAKE2b-256 |
08bc0fd374b81bcf609d8016265a99c61d36330215db4474f628a11665ea02fa
|