Skip to content
You are viewing the corvid 0.4.0 release snapshot — frozen at the 0.4.0 engine release.Current documentation

Schemas and constraints

Schemas are optional and opt-in; schemaless collections are unaffected. A declared schema is enforced on write — existing documents are never retroactively validated.

use corvid::schema::{Schema, Field, FieldType};
# use corvid::{Db};
# let db = Db::open_in_memory()?; let c = db.collection("users");
let schema = Schema::new()
.field(Field::new("name", FieldType::Text).required())
.field(Field::new("email", FieldType::Text).unique())
.field(Field::new("age", FieldType::Int));
c.set_schema(&schema)?; // future writes are validated; violations error
# Ok::<(), corvid::Error>(())

Collection::schema() reads back the declared fields (None when undeclared).

FieldType::Any | Bool | Int | Float | Text | Bytes | Vector | Array | Map. Any accepts every value; the others accept their kind exactly. A violation (type mismatch, missing required field, duplicate unique value) fails the write with Error::SchemaViolation — and nothing is stored.

The field must be present. A Null value counts as present (use Any + required checks sparingly; there is no not-null constraint distinct from presence).

No two documents may hold equal values of the field, enforced per write (insert, batch, patch, update, CAS, insert_auto):

  • Equality is storage-level semantic equalityNaN conflicts with NaN regardless of payload, -0.0 conflicts with 0.0, containers compare element-wise (see equality). Numeric kinds interop: Int(7) and Float(7.0) collide.
  • Works for non-index-encodable values (Bytes/Array/Map/Vector) when a scalar index exists on the field — and the check keys on the actual stored values, so numerically equal-but-distinct stored values never falsely reject.
  • A unique violation rolls back the whole write — including an insert_batch (the batch is all-or-nothing).
  • Delete-then-reinsert of the same value is allowed (uniqueness is over live documents).
  • A scalar index on the unique field makes enforcement index-served and keeps it enforced as values move.
PathBehavior
insert / insert_batch / insert_autovalidated; violations roll back the write
patch / update / compare_and_setthe resulting document must satisfy the schema
set_schema replacing an existing schemasubsequent writes validated against the new one
existing documentsnever re-validated
dump/loaddefinitions round-trip; load replays writes through validation

The sidecar exposes set_schema (fields array of {name, type: any|bool|int|float|text|bytes|vector|array|map, required?, unique?}) and get_schema ({fields: null} when none declared) — see the MCP sidecar.

Next: transactions.