Coding Challenge #5: JSON Config Validator
Your team's microservice is crashing in production because someone deployed it with a malformed configuration file. The JSON has a missing required field, a string where a number should be, or an invalid URL format. These issues slip through manual reviews and only show up when the service tries to start.
You need a config validator that catches these problems before deployment, validates data types, checks required fields, and ensures values meet business rules (valid URLs, positive numbers, etc.).
Your Mission
Build a JSON configuration validator that reads schema definitions and validates config files against them, providing clear error messages for any violations.
Requirements
Your tool must:
Read JSON schema definitions from a file
Validate JSON config files against the schema
Check for required fields and report missing ones
Validate data types (string, number, boolean, array, object)
Support format validation (email, URL, date patterns)
Validate value constraints (min/max, string length, enum values)
Generate detailed error reports with field paths and suggestions
Exit with appropriate codes for CI/CD integration
Support nested object and array validation
Sample Schema (app_schema.json
)
{
"type": "object",
"required": ["database", "server", "logging"],
"properties": {
"database": {
"type": "object",
"required": ["host", "port", "name"],
"properties": {
"host": {
"type": "string",
"format": "hostname"
},
"port": {
"type": "integer",
"minimum": 1,
"maximum": 65535
},
"name": {
"type": "string",
"minLength": 1,
"maxLength": 50
},
"ssl": {
"type": "boolean",
"default": false
}
}
},
"server": {
"type": "object",
"required": ["port", "host"],
"properties": {
"port": {
"type": "integer",
"minimum": 1024,
"maximum": 9999
},
"host": {
"type": "string",
"default": "localhost"
},
"workers": {
"type": "integer",
"minimum": 1,
"maximum": 16,
"default": 4
}
}
},
"logging": {
"type": "object",
"required": ["level"],
"properties": {
"level": {
"type": "string",
"enum": ["DEBUG", "INFO", "WARNING", "ERROR"]
},
"file": {
"type": "string",
"format": "filepath"
},
"max_size": {
"type": "string",
"pattern": "^\\d+[KMG]B$"
}
}
},
"features": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true
}
}
}
Sample Config (app_config.json
)
{
"database": {
"host": "db.example.com",
"port": 5432,
"name": "myapp_prod"
},
"server": {
"port": 8080,
"host": "0.0.0.0",
"workers": 8
},
"logging": {
"level": "INFO",
"file": "/var/log/app.log",
"max_size": "100MB"
},
"features": ["auth", "metrics", "cache"]
}
Expected Output (Valid Config)
JSON Configuration Validation Report
=====================================
📄 Schema: app_schema.json
📄 Config: app_config.json
⏱️ Validation time: 12ms
✅ Configuration is VALID
📊 Validation Summary:
======================
✅ All required fields present
✅ All data types correct
✅ All constraints satisfied
✅ No format violations
🔍 Field Analysis:
==================
database.host: ✅ Valid hostname
database.port: ✅ Valid port (5432)
database.name: ✅ Valid string (length: 10)
server.port: ✅ Valid port (8080)
server.workers: ✅ Valid range (8/16)
logging.level: ✅ Valid enum (INFO)
logging.max_size: ✅ Valid pattern (100MB)
features: ✅ Valid array (3 unique items)
Exit code: 0
Expected Output (Invalid Config)
JSON Configuration Validation Report
=====================================
📄 Schema: app_schema.json
📄 Config: invalid_config.json
⏱️ Validation time: 8ms
❌ Configuration is INVALID
🚨 Validation Errors (4 found):
================================
Error 1: Missing required field
--------------------------------
Path: database.ssl
Expected: boolean (optional with default: false)
Suggestion: Add "ssl": false to database object
Error 2: Invalid data type
---------------------------
Path: server.port
Expected: integer (1024-9999)
Actual: "8080" (string)
Suggestion: Remove quotes: "port": 8080
Error 3: Value out of range
----------------------------
Path: server.workers
Expected: integer (1-16)
Actual: 20
Suggestion: Use value between 1 and 16
Error 4: Invalid enum value
----------------------------
Path: logging.level
Expected: one of ["DEBUG", "INFO", "WARNING", "ERROR"]
Actual: "TRACE"
Suggestion: Use "DEBUG" for detailed logging
Exit code: 1 (validation failed)
Keep reading with a 7-day free trial
Subscribe to Crack That Weekly to keep reading this post and get 7 days of free access to the full post archives.