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}/_schemaSet 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}/_schemaReturns the current schema for a collection, or 404 if none is set.
Remove a Schema
DELETE
/v1/db/{ns}/{collection}/_schemaRemoves the schema, returning the collection to schemaless mode.
Dry-Run Validation
POST
/v1/db/{ns}/{collection}/_validateValidate 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
| Keyword | Applies To |
|---|---|
type | All types |
properties | Objects |
required | Objects |
additionalProperties | Objects |
minLength / maxLength | Strings |
pattern | Strings |
format (email) | Strings |
minimum / maximum | Numbers |
enum | All types |