Schema Validation

ChronDB is schemaless by default, but supports optional JSON Schema validation per namespace (table). This allows you to enforce data integrity where needed while keeping the flexibility of schemaless storage elsewhere.

Overview

Validation schemas are:

  • Optional - Only namespaces with defined schemas are validated

  • Per-namespace - Each table/namespace can have its own schema

  • Version-controlled - Schema changes are tracked in Git history

  • Mode-configurable - Choose between strict rejection or warning-only logging

Validation Modes

Mode
Behavior

strict

Rejects invalid documents with an error

warning

Logs violations but allows the write (ideal for gradual migration)

disabled

No validation (default behavior)

Creating a Validation Schema

Via REST API

# Create schema for the "users" namespace
curl -X PUT http://localhost:3000/api/v1/schemas/validation/users \
  -H "Content-Type: application/json" \
  -d '{
    "mode": "strict",
    "schema": {
      "$schema": "http://json-schema.org/draft-07/schema#",
      "type": "object",
      "required": ["id", "email"],
      "properties": {
        "id": { "type": "string" },
        "email": { "type": "string", "format": "email" },
        "name": { "type": "string" },
        "age": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    }
  }'

Via Redis Protocol

Via SQL (PostgreSQL Protocol)

Querying Schemas

List All Schemas

REST:

Redis:

SQL:

Get Specific Schema

REST:

Redis:

SQL:

View Schema History

REST:

Dry-Run Validation

Test if a document is valid before saving:

REST:

Response (invalid document):

Redis:

Updating a Schema

Simply send a new schema with PUT - version history is automatically maintained:

Changing the Mode

Removing Validation

REST:

Redis:

SQL:

Validation Errors

When mode: strict and a document is invalid:

REST API (HTTP 400)

Redis Protocol

PostgreSQL Protocol

Gradual Migration Strategy

When adding validation to existing data:

  1. Create schema in warning mode:

  2. Monitor logs to identify existing invalid documents

  3. Fix existing data that violates the schema

  4. Switch to strict mode:

JSON Schema Reference

ChronDB supports JSON Schema Draft-07, 2019-09, and 2020-12. Common patterns:

Nested Objects

Array Validation

For complete JSON Schema documentation, see: https://json-schema.org/

Storage Details

Validation schemas are stored in Git at:

Each schema file contains:

Schema changes are versioned and can be time-traveled like any other data in ChronDB.

Last updated