sveltekit-superforms
Version:
Making SvelteKit forms a pleasure to use!
91 lines (90 loc) • 5.32 kB
TypeScript
import { type ActionFailure, type RequestEvent, type Transport } from '@sveltejs/kit';
import { type ValidationAdapter } from './adapters/adapters.js';
import type { ErrorStatus, IsAny } from './utils.js';
import { type FormPathLeavesWithErrors } from './stringPath.js';
import type { JSONSchema } from './jsonSchema/index.js';
import type { InputConstraints } from './jsonSchema/constraints.js';
import type { SuperStructArray } from './superStruct.js';
import type { SchemaShape } from './jsonSchema/schemaShape.js';
export type SuperFormValidated<T extends Record<string, unknown>, Message = App.Superforms.Message extends never ? any : App.Superforms.Message, In extends Record<string, unknown> = T> = SuperValidated<T, Message, In> & {
constraints: InputConstraints<T>;
};
export type SuperValidated<Out extends Record<string, unknown>, Message = App.Superforms.Message extends never ? any : App.Superforms.Message, In extends Record<string, unknown> = Out> = {
id: string;
valid: boolean;
/**
* @deprecated posted is inconsistent between server and client validation, and SPA mode. Will be removed in v3. Use a status message or return your own data in the form action to handle form post status.
*/
posted: boolean;
errors: ValidationErrors<Out>;
data: Out;
constraints?: InputConstraints<Out>;
message?: Message;
shape?: SchemaShape;
};
export type ValidationErrors<Out extends Record<string, unknown>> = {
_errors?: string[];
} & SuperStructArray<Out, string[], {
_errors?: string[];
}>;
type SuperValidateData<In extends Record<string, unknown>> = RequestEvent | Request | FormData | URLSearchParams | URL | Partial<In> | null | undefined;
export type SuperValidateOptions<Out extends Record<string, unknown>> = Partial<{
errors: boolean;
id: string;
preprocessed: (keyof Out)[];
defaults: Out;
jsonSchema: JSONSchema;
strict: boolean;
allowFiles: boolean;
transport: IsAny<Transport> extends true ? never : Transport;
}>;
export type TaintedFields<T extends Record<string, unknown>> = SuperStructArray<T, boolean>;
export declare function superValidate<Out extends Record<string, unknown>, Message = App.Superforms.Message extends never ? any : App.Superforms.Message, In extends Record<string, unknown> = Out>(adapter: ValidationAdapter<Out, In>, options?: SuperValidateOptions<Out>): Promise<SuperValidated<Out, Message, In>>;
export declare function superValidate<Out extends Record<string, unknown>, M = App.Superforms.Message extends never ? any : App.Superforms.Message, In extends Record<string, unknown> = Out>(data: SuperValidateData<In>, adapter: ValidationAdapter<Out, In>, options?: SuperValidateOptions<Out>): Promise<SuperValidated<Out, M, In>>;
/**
* Sends a message with a form, with an optional HTTP status code that will set
* form.valid to false if status >= 400. A status lower than 400 cannot be sent.
*/
export declare function message<M, T extends Record<string, unknown> = Record<string, unknown>, In extends Record<string, unknown> = Record<string, unknown>>(form: SuperValidated<T, M, In>, message: M, options?: {
status?: ErrorStatus;
removeFiles?: boolean;
}): {
form: SuperValidated<T, M, In>;
} | ActionFailure<{
form: SuperValidated<T, M, In>;
}>;
export declare const setMessage: typeof message;
type SetErrorOptions = {
overwrite?: boolean;
status?: ErrorStatus;
removeFiles?: boolean;
};
/**
* Sets a form-level error.
* form.valid is automatically set to false.
*
* @param {SuperValidated<T, unknown>} form A validation object, usually returned from superValidate.
* @param {string | string[]} error Error message(s).
* @param {SetErrorOptions} options Option to overwrite previous errors and set a different status than 400. The status must be in the range 400-599.
* @returns fail(status, { form })
*/
export declare function setError<T extends Record<string, unknown>, M, In extends Record<string, unknown>>(form: SuperValidated<T, M, In>, error: string | string[], options?: SetErrorOptions): ActionFailure<{
form: SuperValidated<T, M, In>;
}>;
/**
* Sets an error for a form field or array field.
* form.valid is automatically set to false.
*
* @param {SuperValidated<T, unknown>} form A validation object, usually returned from superValidate.
* @param {'' | FormPathLeavesWithErrors<T>} 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.
* @param {string | string[]} error Error message(s).
* @param {SetErrorOptions} options Option to overwrite previous errors and set a different status than 400. The status must be in the range 400-599.
* @returns fail(status, { form })
*/
export declare function setError<T extends Record<string, unknown>, Path extends FormPathLeavesWithErrors<T>, M, In extends Record<string, unknown>>(form: SuperValidated<T, M, In>, path: '' | Path, error: string | string[], options?: SetErrorOptions): ActionFailure<{
form: SuperValidated<T, M, In>;
}>;
export declare function withFiles<T extends object>(obj: T): T;
export declare const removeFiles: typeof withFiles;
export declare function fail<T extends Record<string, unknown> | undefined>(status: number, data?: T): ActionFailure<T>;
export {};