nuxt
Version:
78 lines • 3.17 kB
TypeScript
import { NuxtErrorDetails } from "../app/error.js";
import { RequestEvent } from "nuxt/schema";
//#region src/server/validate.d.ts
/** A validation failure, as a Standard Schema reports it. */
interface ValidationIssue {
readonly message: string;
readonly path?: ReadonlyArray<PropertyKey | {
readonly key: PropertyKey;
}> | undefined;
}
type StandardResult<Output> = {
readonly value: Output;
readonly issues?: undefined;
} | {
readonly issues: ReadonlyArray<ValidationIssue>;
};
/** A schema implementing [Standard Schema](https://standardschema.dev), as Zod, Valibot and ArkType do. */
interface StandardSchema<Output = unknown> {
readonly '~standard': {
readonly version: 1;
readonly vendor: string;
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
readonly types?: {
readonly input: unknown;
readonly output: Output;
} | undefined;
};
}
type SchemaOutput<S extends StandardSchema> = NonNullable<S['~standard']['types']>['output'];
/**
* What a validator function returns: the validated value, `true` to accept
* the input as it is, or `false` to reject it. Returning nothing accepts the
* input, and throwing rejects it with the error thrown.
*
* @since 4.6.0
*/
export type ValidateResult<T> = T | true | false | void;
interface ValidateOptions {
/** The error to reject invalid input with, in place of a `400` carrying the issues as `data.issues`. */
onError?: (result: {
issues: ReadonlyArray<ValidationIssue>;
}) => NuxtErrorDetails;
}
/**
* Read the query string of the request, validated with a Standard Schema or a
* validator function. Invalid input is rejected with a `400` whose `data`
* carries the issues, unless `onError` returns a different error.
*
* @example
* ```ts
* import { z } from 'zod'
*
* export default defineEventHandler(async (event) => {
* const { page } = await getValidatedQuery(event, z.object({ page: z.coerce.number() }))
* return { page }
* })
* ```
*
* @since 4.6.0
*/
export declare function getValidatedQuery<S extends StandardSchema>(event: Pick<RequestEvent, 'req'> & {
url?: URL;
}, validate: S, options?: ValidateOptions): Promise<SchemaOutput<S>>;
export declare function getValidatedQuery<Output>(event: Pick<RequestEvent, 'req'> & {
url?: URL;
}, validate: (data: Record<string, string | string[]>) => ValidateResult<Output> | Promise<ValidateResult<Output>>, options?: ValidateOptions): Promise<Output>;
/**
* Read and parse the request body as {@link readBody} does, validated with a
* Standard Schema or a validator function. Invalid input is rejected with a
* `400` whose `data` carries the issues, unless `onError` returns a different
* error.
*
* @since 4.6.0
*/
export declare function readValidatedBody<S extends StandardSchema>(event: Pick<RequestEvent, 'req'>, validate: S, options?: ValidateOptions): Promise<SchemaOutput<S>>;
export declare function readValidatedBody<Output>(event: Pick<RequestEvent, 'req'>, validate: (data: unknown) => ValidateResult<Output> | Promise<ValidateResult<Output>>, options?: ValidateOptions): Promise<Output>;
//#endregion
export {};