Litestone guide / Validation
Checks
A check is a rule the database itself refuses. @@check spans columns, which no single-field validator can, and it holds for every writer, including jobs and seeds.
model Booking {
id Int
startsAt Int
endsAt Int
qty Int ("qty > 0", "Book at least one")
("startsAt < endsAt", "An end must come after its start")
}
await db.booking.create({ data: { startsAt: 1, endsAt: 2, qty: 1 } })
try { await db.booking.create({ data: { startsAt: 5, endsAt: 2, qty: 1 } }) } catch (e) { if (e.name !== 'ValidationError') throw e }
try { await db.booking.create({ data: { startsAt: 1, endsAt: 2, qty: 0 } }) } catch (e) { if (e.name !== 'ValidationError') throw e }
What else it can do
@check("expr")on a field is a SQLiteCHECKon that column- The last argument is the message a person sees; without one the expression goes to
err.constraint @@checkis repeatable- A violation is a
ValidationError, so a form shows it like any other refused value @validateand@@validatemark a rule written in code, for what SQL cannot state- A rule that reads child rows belongs in the function that writes the document, not in a check
In depth: Schema § Constraints the database enforces, § What a @@check cannot say, § A check written in code