|
1 | | -import { fail, json, type RequestEvent } from '@sveltejs/kit'; |
| 1 | +import { ActionFailure, fail, json, type RequestEvent } from '@sveltejs/kit'; |
2 | 2 | import { parse, stringify } from 'devalue'; |
3 | 3 | import { |
4 | 4 | SuperFormError, |
@@ -55,19 +55,64 @@ export function message<T extends ZodValidation<AnyZodObject>, M>( |
55 | 55 |
|
56 | 56 | export const setMessage = message; |
57 | 57 |
|
| 58 | +type SetErrorOptions = { |
| 59 | + overwrite?: boolean; |
| 60 | + status?: NumericRange<400, 599>; |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Sets a form-level error. |
| 65 | + * form.valid is automatically set to false. |
| 66 | + * |
| 67 | + * @param {SuperValidated<T, unknown>} form A validation object, usually returned from superValidate. |
| 68 | + * @param {string | string[]} error Error message(s). |
| 69 | + * @param {SetErrorOptions} options Option to overwrite previous errors and set a different status than 400. The status must be in the range 400-599. |
| 70 | + * @returns fail(status, { form }) |
| 71 | + */ |
| 72 | +export function setError<T extends ZodValidation<AnyZodObject>>( |
| 73 | + form: SuperValidated<T, unknown>, |
| 74 | + error: string | string[], |
| 75 | + options?: SetErrorOptions |
| 76 | +): ActionFailure<{ form: SuperValidated<T, unknown> }>; |
| 77 | + |
58 | 78 | /** |
59 | | - * Sets an error for a form field, with an optional HTTP status code. |
60 | | - * form.valid is automatically set to false. A status lower than 400 cannot be sent. |
| 79 | + * Sets an error for a form field or array field. |
| 80 | + * form.valid is automatically set to false. |
| 81 | + * |
| 82 | + * @param {SuperValidated<T, unknown>} form A validation object, usually returned from superValidate. |
| 83 | + * @param {'' | StringPathLeaves<z.infer<UnwrapEffects<T>>, '_errors'>} path Path to the form field. Use an empty string to set a form-level error. Array-level errors can be set by appending "._errors" to the field. |
| 84 | + * @param {string | string[]} error Error message(s). |
| 85 | + * @param {SetErrorOptions} options Option to overwrite previous errors and set a different status than 400. The status must be in the range 400-599. |
| 86 | + * @returns fail(status, { form }) |
61 | 87 | */ |
62 | 88 | export function setError<T extends ZodValidation<AnyZodObject>>( |
63 | 89 | form: SuperValidated<T, unknown>, |
64 | 90 | path: '' | StringPathLeaves<z.infer<UnwrapEffects<T>>, '_errors'>, |
65 | 91 | error: string | string[], |
66 | | - options: { overwrite?: boolean; status?: NumericRange<400, 599> } = { |
67 | | - overwrite: false, |
68 | | - status: 400 |
| 92 | + options?: SetErrorOptions |
| 93 | +): ActionFailure<{ form: SuperValidated<T, unknown> }>; |
| 94 | + |
| 95 | +export function setError<T extends ZodValidation<AnyZodObject>>( |
| 96 | + form: SuperValidated<T, unknown>, |
| 97 | + path: |
| 98 | + | string |
| 99 | + | string[] |
| 100 | + | (string & StringPathLeaves<z.infer<UnwrapEffects<T>>, '_errors'>), |
| 101 | + error?: string | string[] | SetErrorOptions, |
| 102 | + options?: SetErrorOptions |
| 103 | +): ActionFailure<{ form: SuperValidated<T, unknown> }> { |
| 104 | + // Unify signatures |
| 105 | + if ( |
| 106 | + error == undefined || |
| 107 | + (typeof error !== 'string' && !Array.isArray(error)) |
| 108 | + ) { |
| 109 | + options = error; |
| 110 | + error = path; |
| 111 | + path = ''; |
69 | 112 | } |
70 | | -) { |
| 113 | + |
| 114 | + if (options === undefined) options = {}; |
| 115 | + |
71 | 116 | const errArr = Array.isArray(error) ? error : [error]; |
72 | 117 |
|
73 | 118 | if (!form.errors) form.errors = {}; |
|
0 commit comments