Home → Custom JSON Validator
Define your own validation rules using JavaScript expressions.
Define your own validation rules using JavaScript expressions. This tool runs entirely in your browser — no data is ever sent to a server. Free to use, no account required.
Custom validation enforces business-specific rules on JSON data, going beyond syntax checks to verify that the content makes sense for your application.
Define rules like required field checks, numeric ranges, string format patterns, and enum value constraints. Rules are evaluated against your JSON and each passes or fails with a clear message.
Validate relationships between fields — for example, ensuring end_date is after start_date, or that a shipping address is required when a shipping_required flag is true. These conditional rules are difficult to express in JSON Schema.
Both tools validate JSON, but they are suited to different situations and have different trade-offs.
JSON Schema is standardized, reusable, and supported by validators in every programming language. Use it for API contracts, documentation, and any validation that needs to be shared or consumed by other tools.
Custom rules are faster to write for simple checks and support imperative logic not expressible in JSON Schema. They are ideal for one-off validation tasks or prototyping before formalizing a schema.
Beyond standard JSON Schema constraints, real applications need business logic validation: conditional requirements, cross-field rules, and domain-specific formats.
// Conditional: if role is "admin", require permissions array
{
"type": "object",
"properties": {
"role": {"type": "string", "enum": ["user", "admin"]},
"permissions": {"type": "array", "items": {"type": "string"}}
},
"if": {"properties": {"role": {"const": "admin"}}},
"then": {"required": ["permissions"]}
}
// Cross-field: endDate must be after startDate (custom keyword)
// Uses ajv-keywords or custom ajv plugin
{
"type": "object",
"properties": {
"startDate": {"type": "string", "format": "date"},
"endDate": {"type": "string", "format": "date"}
},
"if": {"required": ["endDate"]},
"then": {
"properties": {
"endDate": {"formatMinimum": {"$data": "1/startDate"}}
}
}
}
const Ajv = require("ajv");
const ajv = new Ajv();
// Custom keyword: positive number
ajv.addKeyword({
keyword: "positiveNumber",
type: "number",
schemaType: "boolean",
validate: (schema, data) => !schema || data > 0,
errors: false
});
const schema = {type: "number", positiveNumber: true};
const validate = ajv.compile(schema);
console.log(validate(5)); // true
console.log(validate(-1)); // false
Explore more tools: All JSON Tools | Validator | Pretty Print | JSON Diff
JSON Schema covers structural and type-level constraints, but many real business rules require logic that spans multiple fields or involves calculations. The three functions below show patterns not expressible in JSON Schema alone.
// Rule 1: End date must be after start date
function validateDateRange(data) {
if (data.startDate && data.endDate) {
if (new Date(data.endDate) <= new Date(data.startDate)) {
return 'endDate must be after startDate';
}
}
return null; // valid
}
// Rule 2: Either email or phone must be present
function validateContact(data) {
if (!data.email && !data.phone) {
return 'At least one of email or phone is required';
}
return null;
}
// Rule 3: Total must equal sum of line items
function validateOrderTotal(data) {
if (data.items && data.total !== undefined) {
const sum = data.items.reduce((acc, item) => acc + item.price * item.qty, 0);
if (Math.abs(sum - data.total) > 0.01) {
return `Total ${data.total} doesn't match sum of items ${sum.toFixed(2)}`;
}
}
return null;
}
Use JSON Schema for everything it can express well — type checking, required fields, string formats. Reserve custom validation rules for logic that genuinely requires imperative code: cross-field dependencies, database lookups, and mathematical constraints.
| Validation Type | Use JSON Schema | Use Custom Rules |
|---|---|---|
| Required fields | Better | — |
| Type checking | Better | — |
| String format | Better (format keyword) | — |
| Cross-field dependency | — | Better |
| Business logic | — | Better |
| Database lookups | — | Only option |
| Math constraints | Partial | Better |
| Conditional rules | With if/then/else | Both work |