On this page
Properties, enums and validation
Problem. The same value (an email, a money amount, a status) appears in many schemas and must always mean the same thing.
Pattern. Declare each value once as a registered property, then reuse it with field(...).
import { dryv, field, property, schema } from "@dryvcode/author";
const shared = dryv.group({ name: "shared" });
const base = shared.properties({
Id: property.string.uuid(),
Email: property.string.email(),
Name: property.string(),
Count: property.number.nonNegativeInteger(),
Active: property.literal("active"),
CreatedAt: property.temporal.instant().precision("millisecond"),
Status: property.enum({ active: "active", suspended: "suspended" }),
});
Combine values
const advanced = shared.properties({
Coordinates: property.composite({
x: field(base.Count),
y: field(base.Count),
}),
Identifier: property.union({
discriminant: "kind",
members: { id: base.Id, active: base.Active },
}),
Pair: property.tuple(base.Id, base.Active),
Labels: property.record(base.Id, base.Name),
Box: property.generic.define((T) => property.composite({ value: field(T) })),
});
Shape a field
const users = dryv.group({ name: "users" });
const schemas = users.schemas({
User: schema({
id: field(base.Id).system(),
email: field(base.Email)
.optional()
.nullable()
.query({ filter: ["equal", "contains"], sort: true, search: true }),
names: field(base.Name).array({ min: 1 }),
status: field(base.Status).default("active"),
secret: field(base.Name).sensitive(),
}),
});
| Helper | Meaning |
|---|---|
.required() / .optional() | Presence in requests and storage |
.nullable() | Value may be null (different from absent) |
.array({ min, max }) | A list of the value |
.default(...) | Default when not supplied |
.system() / .createOnly() | Generated by the system / settable only on creation |
.query({ filter, sort, search }) | Which operations clients may use against the field |
.sensitive() | Mark data packs should treat carefully (logs, docs, examples) |
Notes
- There are no reusable
unknownordynamicregistries:unknown()anddynamic()are intrinsic and stay inline. - Properties never carry framework types. A pack decides that
property.string.uuid()becomesGuid,uuid.UUIDorString. - A generic is defined once and applied with registered arguments:
property.generic.apply(advanced.Box, [schemas.User]).