|
1 | 1 | import { z } from 'zod'; |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Schema for Validation Rules. |
5 | | - * Ensures data integrity by preventing saves when conditions aren't met. |
| 4 | + * Base Validation Rule |
6 | 5 | */ |
7 | | -export const ValidationRuleSchema = z.object({ |
8 | | - /** Machine name of the rule */ |
| 6 | +const BaseValidationSchema = z.object({ |
9 | 7 | name: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Unique rule name'), |
10 | | - |
11 | | - /** |
12 | | - * The condition that triggers the error. |
13 | | - * If this evaluates to TRUE, the validation FAILS and the error is shown. |
14 | | - * Example: "amount < 0" (Prevents negative amounts) |
15 | | - */ |
16 | | - error_condition: z.string().describe('Formula expression. If TRUE, validation fails.'), |
17 | | - |
18 | | - /** The error message displayed to the user */ |
19 | | - error_message: z.string().describe('Error message to display to the user'), |
20 | | - |
21 | | - /** Active status of the rule */ |
22 | | - active: z.boolean().default(true).describe('Whether this rule is active'), |
| 8 | + active: z.boolean().default(true), |
| 9 | + severity: z.enum(['error', 'warning', 'info']).default('error'), |
| 10 | + message: z.string().describe('Error message to display'), |
23 | 11 | }); |
24 | 12 |
|
| 13 | +/** |
| 14 | + * 1. Script/Expression Validation |
| 15 | + * Generic formula-based validation. |
| 16 | + */ |
| 17 | +export const ScriptValidationSchema = BaseValidationSchema.extend({ |
| 18 | + type: z.literal('script'), |
| 19 | + condition: z.string().describe('Formula expression. If TRUE, validation fails. (e.g. amount < 0)'), |
| 20 | +}); |
| 21 | + |
| 22 | +/** |
| 23 | + * 2. Uniqueness Validation |
| 24 | + * specialized optimized check for unique constraints. |
| 25 | + */ |
| 26 | +export const UniquenessValidationSchema = BaseValidationSchema.extend({ |
| 27 | + type: z.literal('unique'), |
| 28 | + fields: z.array(z.string()).describe('Fields that must be combined unique'), |
| 29 | + scope: z.string().optional().describe('Formula condition for scope (e.g. active = true)'), |
| 30 | + caseSensitive: z.boolean().default(true), |
| 31 | +}); |
| 32 | + |
| 33 | +/** |
| 34 | + * 3. State Machine Validation |
| 35 | + * State transition logic. |
| 36 | + */ |
| 37 | +export const StateMachineValidationSchema = BaseValidationSchema.extend({ |
| 38 | + type: z.literal('state_machine'), |
| 39 | + field: z.string().describe('State field (e.g. status)'), |
| 40 | + transitions: z.record(z.array(z.string())).describe('Map of { OldState: [AllowedNewStates] }'), |
| 41 | +}); |
| 42 | + |
| 43 | +/** |
| 44 | + * 4. Value Format Validation |
| 45 | + * Regex or specialized formats. |
| 46 | + */ |
| 47 | +export const FormatValidationSchema = BaseValidationSchema.extend({ |
| 48 | + type: z.literal('format'), |
| 49 | + field: z.string(), |
| 50 | + regex: z.string().optional(), |
| 51 | + format: z.enum(['email', 'url', 'phone', 'json']).optional(), |
| 52 | +}); |
| 53 | + |
| 54 | +/** |
| 55 | + * Master Validation Rule Schema |
| 56 | + */ |
| 57 | +export const ValidationRuleSchema = z.discriminatedUnion('type', [ |
| 58 | + ScriptValidationSchema, |
| 59 | + UniquenessValidationSchema, |
| 60 | + StateMachineValidationSchema, |
| 61 | + FormatValidationSchema |
| 62 | +]); |
| 63 | + |
25 | 64 | export type ValidationRule = z.infer<typeof ValidationRuleSchema>; |
| 65 | +export type ScriptValidation = z.infer<typeof ScriptValidationSchema>; |
| 66 | +export type UniquenessValidation = z.infer<typeof UniquenessValidationSchema>; |
| 67 | +export type StateMachineValidation = z.infer<typeof StateMachineValidationSchema>; |
0 commit comments