UNPKG

4.5 kBTypeScriptView Raw
1import { CustomSanitizer, CustomValidator, ErrorMessage, FieldMessageFactory, Location, Request } from '../base';
2import { BailOptions, OptionalOptions, ValidationChain, ValidationChainLike } from '../chain';
3import { ResultWithContext } from '../chain/context-runner';
4import { Sanitizers } from '../chain/sanitizers';
5import { Validators } from '../chain/validators';
6type BaseValidatorSchemaOptions = {
7 /**
8 * The error message if there's a validation error,
9 * or a function for creating an error message dynamically.
10 */
11 errorMessage?: FieldMessageFactory | ErrorMessage;
12 /**
13 * Whether the validation should be reversed.
14 */
15 negated?: boolean;
16 /**
17 * Whether the validation should bail after running this validator
18 */
19 bail?: boolean | BailOptions;
20 /**
21 * Specify a condition upon which this validator should run.
22 * Can either be a validation chain, or a custom validator function.
23 */
24 if?: CustomValidator | ValidationChain;
25};
26type ValidatorSchemaOptions<K extends keyof Validators<any>> = boolean | (BaseValidatorSchemaOptions & {
27 /**
28 * Options to pass to the validator.
29 */
30 options?: Parameters<Validators<any>[K]> | Parameters<Validators<any>[K]>[0];
31});
32type CustomValidatorSchemaOptions = BaseValidatorSchemaOptions & {
33 /**
34 * The implementation of a custom validator.
35 */
36 custom: CustomValidator;
37};
38export type ExtensionValidatorSchemaOptions = boolean | BaseValidatorSchemaOptions;
39export type ValidatorsSchema = {
40 [K in Exclude<keyof Validators<any>, 'not' | 'withMessage'>]?: ValidatorSchemaOptions<K>;
41};
42type SanitizerSchemaOptions<K extends keyof Sanitizers<any>> = boolean | {
43 /**
44 * Options to pass to the sanitizer.
45 */
46 options?: Parameters<Sanitizers<any>[K]> | Parameters<Sanitizers<any>[K]>[0];
47};
48type CustomSanitizerSchemaOptions = {
49 /**
50 * The implementation of a custom sanitizer.
51 */
52 customSanitizer: CustomSanitizer;
53};
54export type ExtensionSanitizerSchemaOptions = true;
55export type SanitizersSchema = {
56 [K in keyof Sanitizers<any>]?: SanitizerSchemaOptions<K>;
57};
58type BaseParamSchema = {
59 /**
60 * Which request location(s) the field to validate is.
61 * If unset, the field will be checked in every request location.
62 */
63 in?: Location | Location[];
64 /**
65 * The general error message in case a validator doesn't specify one,
66 * or a function for creating the error message dynamically.
67 */
68 errorMessage?: FieldMessageFactory | any;
69 /**
70 * Whether this field should be considered optional
71 */
72 optional?: boolean | {
73 options?: OptionalOptions;
74 };
75};
76export type DefaultSchemaKeys = keyof BaseParamSchema | keyof ValidatorsSchema | keyof SanitizersSchema;
77/**
78 * Defines a schema of validations/sanitizations for a field
79 */
80export type ParamSchema<T extends string = DefaultSchemaKeys> = BaseParamSchema & ValidatorsSchema & SanitizersSchema & {
81 [K in T]?: K extends keyof BaseParamSchema ? BaseParamSchema[K] : K extends keyof ValidatorsSchema ? ValidatorsSchema[K] : K extends keyof SanitizersSchema ? SanitizersSchema[K] : CustomValidatorSchemaOptions | CustomSanitizerSchemaOptions;
82};
83/**
84 * Defines a mapping from field name to a validations/sanitizations schema.
85 */
86export type Schema<T extends string = DefaultSchemaKeys> = Record<string, ParamSchema<T>>;
87/**
88 * Shortcut type for the return of a {@link checkSchema()}-like function.
89 */
90export type RunnableValidationChains<C extends ValidationChainLike> = C[] & {
91 run(req: Request): Promise<ResultWithContext[]>;
92};
93/**
94 * Factory for a {@link checkSchema()} function which can have extension validators and sanitizers.
95 *
96 * @see {@link checkSchema()}
97 */
98export declare function createCheckSchema<C extends ValidationChainLike>(createChain: (fields?: string | string[], locations?: Location[], errorMessage?: any) => C, extraValidators?: (keyof C)[], extraSanitizers?: (keyof C)[]): <T extends string = DefaultSchemaKeys>(schema: Schema<T>, defaultLocations?: Location[]) => RunnableValidationChains<C>;
99/**
100 * Creates an express middleware with validations for multiple fields at once in the form of
101 * a schema object.
102 *
103 * @param schema the schema to validate.
104 * @param defaultLocations
105 * @returns
106 */
107export declare const checkSchema: <T extends string = DefaultSchemaKeys>(schema: Schema<T>, defaultLocations?: Location[]) => RunnableValidationChains<ValidationChain>;
108export {};