Home/Documentation

Schema Validation

Optionally enforce data shapes with JSON Schema on a per-collection basis. Invalid documents are rejected with field-level error messages.

info

Optional by default

Collections are schemaless until you set a schema. Existing documents are not retroactively validated.

Set a Schema

PUT/v1/db/{ns}/{collection}/_schema

Set or update the JSON Schema for a collection.

curl -X PUT https://api.jsondb.cloud/v1/db/default/users/_schema \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "type": "object",
    "properties": {
      "name": { "type": "string", "minLength": 1, "maxLength": 100 },
      "email": { "type": "string", "format": "email" },
      "age": { "type": "integer", "minimum": 0, "maximum": 150 },
      "role": { "type": "string", "enum": ["admin", "user", "viewer"] }
    },
    "required": ["name", "email"],
    "additionalProperties": false
  }'

Get a Schema

GET/v1/db/{ns}/{collection}/_schema

Returns the current schema for a collection, or 404 if none is set.

Remove a Schema

DELETE/v1/db/{ns}/{collection}/_schema

Removes the schema, returning the collection to schemaless mode.

Dry-Run Validation

POST/v1/db/{ns}/{collection}/_validate

Validate a document against the collection schema without storing it.

curl -X POST https://api.jsondb.cloud/v1/db/default/users/_validate \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name": "", "age": -5, "role": "superadmin"}'

Validation failure response (400)

{
  "valid": false,
  "errors": [
    { "path": "/name", "message": "String is too short (0 < 1)", "keyword": "minLength" },
    { "path": "/email", "message": "Required property 'email' is missing", "keyword": "required" },
    { "path": "/age", "message": "Number is less than minimum (-5 < 0)", "keyword": "minimum" },
    { "path": "/role", "message": "Value 'superadmin' is not in enum [admin, user, viewer]", "keyword": "enum" }
  ]
}

Supported JSON Schema Keywords

KeywordApplies To
typeAll types
propertiesObjects
requiredObjects
additionalPropertiesObjects
minLength / maxLengthStrings
patternStrings
format (email)Strings
minimum / maximumNumbers
enumAll types