Home → Custom JSON Validator

Custom JSON Validator

Define your own validation rules using JavaScript expressions.

About This Tool

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.

What Custom JSON Validation Is

Custom validation enforces business-specific rules on JSON data, going beyond syntax checks to verify that the content makes sense for your application.

Rule-Based Validation

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.

Cross-Field Validation

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.

Custom Validation vs JSON Schema

Both tools validate JSON, but they are suited to different situations and have different trade-offs.

When to Use JSON Schema

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.

When to Use Custom Rules

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.

Frequently Asked Questions

What is a custom JSON validator?+
A custom JSON validator lets you define your own validation rules beyond what the JSON standard requires. While standard validation only checks syntax, custom validation checks business rules: required fields, value ranges, allowed enum values, string format patterns, and relationships between fields.
How do I write custom validation rules?+
Rules are written as simple expressions: required('email'), type('age', 'number'), range('age', 0, 150), matches('email', '^.+@.+\..+$'), oneOf('status', ['active', 'inactive']). The tool evaluates each rule against your JSON and reports which pass and which fail.
Is custom validation the same as JSON Schema validation?+
They overlap but are different. JSON Schema is a standardized specification supported by many libraries and tools. Custom validation is ad-hoc and written for quick checks. Use JSON Schema when you need a standard, shareable contract; use custom rules for rapid one-off validation.
Can I save and reuse my custom validation rules?+
Yes. Your rules are saved in the browser's localStorage and persist between sessions on the same device. You can also export the rules as a JSON file and import them on another device or share with a teammate.

Custom JSON Validation Rule Examples

Beyond standard JSON Schema constraints, real applications need business logic validation: conditional requirements, cross-field rules, and domain-specific formats.

Advanced Schema Patterns

// 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"}}
    }
  }
}

Custom Ajv Keyword Example

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

Custom Validation Rules: Examples

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;
}

Custom Validation vs JSON Schema: What to Use When

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 fieldsBetter
Type checkingBetter
String formatBetter (format keyword)
Cross-field dependencyBetter
Business logicBetter
Database lookupsOnly option
Math constraintsPartialBetter
Conditional rulesWith if/then/elseBoth work