Validation Annotations
SlintORM validates annotated fields on every insert() and update(). If validation fails, a ValidationError is thrown with per-field error messages.
Format Validators
@email
src/interfaces.ts
// @email
email?: string;
// Valid: "user@example.com", "a@b.co"
// Invalid: "not-an-email", "@missing.com", "user@.com"@url
src/interfaces.ts
// @url
url?: string;
// Valid: "https://example.com", "http://localhost:3000/path?q=1"
// Invalid: "not-a-url", "ftp://..."@uuid
src/interfaces.ts
// @uuid
uuid?: string;
// Valid: "550e8400-e29b-41d4-a716-446655440000"
// Invalid: "not-a-uuid", "550e8400-e29b-41d4"@phone
src/interfaces.ts
// @phone
phone?: string;
// Valid: "+1-555-123-4567", "5551234567", "(555) 123-4567"
// Invalid: "abc", "12"Numeric Range
src/interfaces.ts
// @min:N — minimum value (inclusive)
// @max:N — maximum value (inclusive)
// @min:0
// @max:120
age: number;
const data = { age: 150 };
await User.validate(data, rules); // throws: "must be at most 120"String Length
src/interfaces.ts
// @minLength:N — minimum string length
// @maxLength:N — maximum string length
// @minLength:2
// @maxLength:100
name: string;Custom Regex
src/interfaces.ts
// @pattern:<regex>
// @pattern:^[A-Za-z0-9_-]+$
status?: string;
// Valid: "ACTIVE", "pending_review", "DRAFT-1"
// Invalid: "with spaces!", "special@chars"Validation on Write
src/auth.ts
try {
await User.insert({
name: "X", // fails @minLength:2
email: "bad", // fails @email
score: 999, // fails @max:100
});
} catch (err) {
if (err instanceof ValidationError) {
err.message // "Validation failed: must be at least 2 characters; ..."
err.errors // { name: "must be at least 2 characters", email: "...", score: "..." }
}
}
// Updates also validate:
await User.update({ id: 1 }, { email: "not-an-email" });
// throws ValidationErrorErrors
| Error | Cause | Fix |
|---|---|---|
ValidationError with field errors | One or more fields fail their annotation rules | Check err.errors for per-field messages |
must be at least N characters | @minLength:N violated | Provide a longer string |
must be a valid email address | @email pattern mismatch | Use a valid email format |
must be at most N | @max:N exceeded | Provide a smaller number |
Conventions
- Validation runs on every insert and update — optional fields skip validation when
undefined - Use
@pattern:^...$for formats not covered by built-in validators - Multiple annotations on the same field are separated by
; - For manual validation, use
User.validate(data, rules)orUser.check(data, rules)