The Constraints Plugin allows you to enforce complex business rules and relationships between items in a desk. It supports a wide variety of constraint types, making it ideal for scenarios where data consistency and inter-item logic are required.
import { useCheckIn } from 'vue-airport';
import { createConstraintsPlugin, ConstraintType } from '@vue-airport/plugins-validation';
interface Member {
email: string;
role: string;
age?: number;
}
const constraints = [
{ type: ConstraintType.Unique, key: 'email', message: 'Email already used' },
{ type: ConstraintType.MaxCount, count: 5, message: 'Maximum 5 members' },
{ type: ConstraintType.Required, key: 'role', message: 'Role is required' },
{ type: ConstraintType.Enum, key: 'role', values: ['admin', 'user'], message: 'Role must be admin or user' },
{
type: ConstraintType.Custom,
fn: (child, children) => child.role === 'admin' && children.filter(u => u.role === 'admin').length >= 2
? 'Maximum 2 admins'
: null,
message: 'Maximum 2 admins'
},
];
const { desk } = useCheckIn<Member>({
plugins: [createConstraintsPlugin(constraints)]
});
<, >, <=, >=, ===, !==).email, url, phone, etc).const constraints = [
{ type: ConstraintType.Required, key: 'email' },
{ type: ConstraintType.Pattern, key: 'email', regex: /^[^\s@]+@[^\s@]+\.[^\s@]+$/ },
{ type: ConstraintType.MinLength, key: 'name', length: 2 },
{ type: ConstraintType.MaxLength, key: 'name', length: 30 },
{ type: ConstraintType.Enum, key: 'role', values: ['admin', 'user'] },
{ type: ConstraintType.Range, key: 'age', min: 18, max: 65 },
{ type: ConstraintType.UniqueGroup, keys: ['email', 'role'] },
{ type: ConstraintType.DateRange, key: 'createdAt', min: '2023-01-01', max: '2025-12-31' },
{ type: ConstraintType.Dependency, key: 'role', value: 'admin', required: 'adminCode' },
(child, children) => child.role === 'admin' && children.filter(u => u.role === 'admin').length >= 2
? 'Maximum 2 admins'
: null,
];
If a constraint is violated during check-in or check-out, the plugin throws an exception and blocks the operation. You can provide custom error messages for each constraint.
createConstraintsPlugin<T>(constraints: Constraint<T>[]): CheckInPlugin<T>Creates a plugin enforcing the provided constraints.
type ConstraintFn<T> = (child: T, children: T[]) => string | null;
type ConstraintObj<T> =
| { type: ConstraintType.Unique; key: keyof T; message?: string }
| { type: ConstraintType.MaxCount; count: number; message?: string }
| { type: ConstraintType.Required; key: keyof T; message?: string }
| { type: ConstraintType.Pattern; key: keyof T; regex: RegExp; message?: string }
| { type: ConstraintType.MinLength; key: keyof T; length: number; message?: string }
| { type: ConstraintType.MaxLength; key: keyof T; length: number; message?: string }
| { type: ConstraintType.Enum; key: keyof T; values: any[]; message?: string }
| { type: ConstraintType.Range; key: keyof T; min: number; max: number; message?: string }
| { type: ConstraintType.UniqueGroup; keys: (keyof T)[]; message?: string }
| { type: ConstraintType.DateRange; key: keyof T; min: string | Date; max: string | Date; message?: string }
| { type: ConstraintType.Dependency; key: keyof T; value: any; required: keyof T; message?: string }
| { type: ConstraintType.Relation; rule: ConstraintFn<T>; message?: string }
| { type: ConstraintType.BeforeCheckOut; rule: ConstraintFn<T>; message?: string }
| { type: ConstraintType.Custom; fn: (child: T, children: T[]) => string | null | Promise<string | null>; message?: string }
// ...and more
The Constraints Plugin and the Validation Plugin are designed to be complementary:
Typical usage:
Both plugins can be used together in the same desk for robust data integrity and business rule enforcement.
See the Business Rules Example for a complete working implementation.