|
1 |
| -import type { FieldContext } from '@vinejs/vine/build/src/types' |
2 | 1 | import { USD, currency } from '@stacksjs/utils'
|
3 | 2 | import { validator } from './validate'
|
4 | 3 |
|
| 4 | +/** |
| 5 | + * Thanks to VineJS for the following types: |
| 6 | + * |
| 7 | + * The context shared with the entire validation pipeline. |
| 8 | + * Each field gets its own context object. |
| 9 | + */ |
| 10 | +export interface FieldContext { |
| 11 | + /** |
| 12 | + * Field value |
| 13 | + */ |
| 14 | + value: unknown |
| 15 | + |
| 16 | + /** |
| 17 | + * The data property is the top-level object under validation. |
| 18 | + */ |
| 19 | + data: any |
| 20 | + |
| 21 | + /** |
| 22 | + * Shared metadata across the entire validation lifecycle. It can be |
| 23 | + * used to pass data between validation rules |
| 24 | + */ |
| 25 | + meta: Record<string, any> |
| 26 | + |
| 27 | + /** |
| 28 | + * Mutate the value of field under validation. |
| 29 | + */ |
| 30 | + mutate(newValue: any, field: FieldContext): void |
| 31 | + |
| 32 | + /** |
| 33 | + * Report error to the error reporter |
| 34 | + */ |
| 35 | + report: ErrorReporterContract['report'] |
| 36 | + |
| 37 | + /** |
| 38 | + * Is this field valid. Default: true |
| 39 | + */ |
| 40 | + isValid: boolean |
| 41 | + |
| 42 | + /** |
| 43 | + * Is this field has value defined. |
| 44 | + */ |
| 45 | + isDefined: boolean |
| 46 | + |
| 47 | + /** |
| 48 | + * Wildcard path for the field. The value is a nested |
| 49 | + * pointer to the field under validation. |
| 50 | + * |
| 51 | + * In case of arrays, the `*` wildcard is used. |
| 52 | + */ |
| 53 | + wildCardPath: string |
| 54 | + |
| 55 | + /** |
| 56 | + * The parent property is the parent of the field. It could be an |
| 57 | + * array or an object. |
| 58 | + */ |
| 59 | + parent: any |
| 60 | + |
| 61 | + /** |
| 62 | + * Name of the field under validation. In case of an array, the field |
| 63 | + * name will be a number |
| 64 | + */ |
| 65 | + name: string | number |
| 66 | + |
| 67 | + /** |
| 68 | + * Is this field an array member |
| 69 | + */ |
| 70 | + isArrayMember: boolean |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Thanks to VineJS for the following types: |
| 75 | + * |
| 76 | + * The error reporter is used to report errors during the validation |
| 77 | + * process. |
| 78 | + */ |
| 79 | +export interface ErrorReporterContract { |
| 80 | + /** |
| 81 | + * A boolean to known if there are one or more |
| 82 | + * errors. |
| 83 | + */ |
| 84 | + hasErrors: boolean |
| 85 | + |
| 86 | + /** |
| 87 | + * Creates an instance of an exception to throw |
| 88 | + */ |
| 89 | + createError(): Error |
| 90 | + |
| 91 | + /** |
| 92 | + * Report error for a field |
| 93 | + */ |
| 94 | + report(message: string, rule: string, field: FieldContext, args?: Record<string, any>): any |
| 95 | +} |
| 96 | + |
| 97 | +// export const isMoney = validator.createRule((value: unknown, _, field: FieldContext) => { |
5 | 98 | export const isMoney = validator.createRule((value: unknown, _, field: FieldContext) => {
|
6 | 99 | /**
|
7 | 100 | * Convert string representation of a number to a JavaScript
|
@@ -30,4 +123,4 @@ export const isMoney = validator.createRule((value: unknown, _, field: FieldCont
|
30 | 123 | * Mutate the field's value
|
31 | 124 | */
|
32 | 125 | field.mutate(amount, field)
|
33 |
| -}) |
| 126 | +}) as any |
0 commit comments