Validation Functions
These functions provide JSON Schema validation with graceful fallback when jsonschema is not available.
source
validate_config
validate_config (config:Dict[str,Any], schema:Dict[str,Any])
Validate a configuration dictionary against a JSON Schema.
config
Dict
Configuration to validate
schema
Dict
JSON Schema to validate against
Returns
Tuple
(is_valid, error_message)
Uses the jsonschema library for full validation if available, otherwise falls back to basic validation.
The basic validation (when jsonschema is not installed) provides minimal support checking: - Required fields - Field types - Enum values - Numeric constraints (minimum, maximum)
source
Example: Validating Configuration
import json
# Define a schema
schema = {
"type" : "object" ,
"properties" : {
"model" : {
"type" : "string" ,
"enum" : ["tiny" , "base" , "small" , "medium" , "large" ],
"default" : "base" ,
"description" : "Model size to use"
},
"temperature" : {
"type" : "number" ,
"minimum" : 0.0 ,
"maximum" : 1.0 ,
"default" : 0.0
},
"batch_size" : {
"type" : "integer" ,
"minimum" : 1 ,
"maximum" : 32 ,
"default" : 8
}
},
"required" : ["model" ]
}
print ("Schema:" )
print (json.dumps(schema, indent= 2 ))
Schema:
{
"type": "object",
"properties": {
"model": {
"type": "string",
"enum": [
"tiny",
"base",
"small",
"medium",
"large"
],
"default": "base",
"description": "Model size to use"
},
"temperature": {
"type": "number",
"minimum": 0.0,
"maximum": 1.0,
"default": 0.0
},
"batch_size": {
"type": "integer",
"minimum": 1,
"maximum": 32,
"default": 8
}
},
"required": [
"model"
]
}
# Test valid configurations
valid_configs = [
{"model" : "tiny" },
{"model" : "base" , "temperature" : 0.5 },
{"model" : "large" , "batch_size" : 16 }
]
print (" \n Validating valid configurations:" )
for config in valid_configs:
is_valid, error = validate_config(config, schema)
print (f"Config: { config} " )
print (f" Valid: { is_valid} " )
if error:
print (f" Error: { error} " )
Validating valid configurations:
Config: {'model': 'tiny'}
Valid: True
Config: {'model': 'base', 'temperature': 0.5}
Valid: True
Config: {'model': 'large', 'batch_size': 16}
Valid: True
# Test invalid configurations
invalid_configs = [
({"temperature" : 0.5 }, "Missing required 'model' field" ),
({"model" : "invalid" }, "Invalid enum value" ),
({"model" : "base" , "temperature" : 1.5 }, "Temperature exceeds maximum" ),
({"model" : "base" , "batch_size" : 100 }, "Batch size exceeds maximum" )
]
print (" \n Validating invalid configurations:" )
for config, description in invalid_configs:
is_valid, error = validate_config(config, schema)
print (f" \n { description} :" )
print (f" Config: { config} " )
print (f" Valid: { is_valid} " )
if error:
print (f" Error: { error[:100 ]} ..." ) # Truncate long errors
Validating invalid configurations:
Missing required 'model' field:
Config: {'temperature': 0.5}
Valid: False
Error: 'model' is a required property
Failed validating 'required' in schema:
{'type': 'object',
...
Invalid enum value:
Config: {'model': 'invalid'}
Valid: False
Error: 'invalid' is not one of ['tiny', 'base', 'small', 'medium', 'large']
Failed validating 'enum' in sc...
Temperature exceeds maximum:
Config: {'model': 'base', 'temperature': 1.5}
Valid: False
Error: 1.5 is greater than the maximum of 1.0
Failed validating 'maximum' in schema['properties']['tempera...
Batch size exceeds maximum:
Config: {'model': 'base', 'batch_size': 100}
Valid: False
Error: 100 is greater than the maximum of 32
Failed validating 'maximum' in schema['properties']['batch_si...
# Test extracting defaults
defaults = extract_defaults(schema)
print (" \n Default values extracted from schema:" )
print (json.dumps(defaults, indent= 2 ))
Default values extracted from schema:
{
"model": "base",
"temperature": 0.0,
"batch_size": 8
}