import { CustomSanitizer, CustomValidator, ErrorMessage, FieldMessageFactory, Location, Middleware, Request, ValidationError } from './base'; import { ContextRunner, ValidationChain } from './chain'; import { MatchedDataOptions } from './matched-data'; import { checkExact } from './middlewares/exact'; import { OneOfOptions } from './middlewares/one-of'; import { DefaultSchemaKeys, ExtensionSanitizerSchemaOptions, ExtensionValidatorSchemaOptions, ParamSchema, RunnableValidationChains } from './middlewares/schema'; import { ErrorFormatter, Result } from './validation-result'; type CustomValidatorsMap = Record; type CustomSanitizersMap = Record; type CustomOptions = { errorFormatter?: ErrorFormatter; }; /** * A validation chain that contains some extension validators/sanitizers. * * Built-in methods return the same chain type so that chaining using more of the extensions is * possible. * * @example * ``` * function createChain(chain: ValidationChainWithExtensions<'isAllowedDomain' | 'removeEmailAttribute'>) { * return chain * .isEmail() * .isAllowedDomain() * .trim() * .removeEmailAttribute(); * } * ``` */ export type ValidationChainWithExtensions = Middleware & { [K in keyof ValidationChain]: ValidationChain[K] extends (...args: infer A) => ValidationChain ? (...params: A) => ValidationChainWithExtensions : ValidationChain[K]; } & { [K in T]: () => ValidationChainWithExtensions; }; /** * Schema of validations/sanitizations for a field, including extension validators/sanitizers */ export type ParamSchemaWithExtensions = { [K in keyof ParamSchema | V | S]?: K extends V ? ExtensionValidatorSchemaOptions : K extends S ? ExtensionSanitizerSchemaOptions : K extends keyof ParamSchema ? ParamSchema[K] : never; }; /** * Type of a validation chain created by a custom ExpressValidator instance. * * @example * ``` * const myExpressValidator = new ExpressValidator({ * isAllowedDomain: value => value.endsWith('@gmail.com') * }); * * type MyCustomValidationChain = CustomValidationChain * function createMyCustomChain(): MyCustomValidationChain { * return myExpressValidator.body('email').isAllowedDomain(); * } * ``` */ export type CustomValidationChain> = T extends ExpressValidator ? ValidationChainWithExtensions> : never; /** * Mapping from field name to a validations/sanitizations schema, including extensions from an * ExpressValidator instance. */ export type CustomSchema, K extends string = DefaultSchemaKeys> = T extends ExpressValidator ? Record, Extract, K>> : never; export declare class ExpressValidator { private readonly validators?; private readonly sanitizers?; private readonly options?; private readonly validatorEntries; private readonly sanitizerEntries; constructor(validators?: V | undefined, sanitizers?: S | undefined, options?: CustomOptions | undefined); private createChain; buildCheckFunction(locations: Location[]): (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Creates a middleware/validation chain for one or more fields that may be located in * any of the following: * * - `req.body` * - `req.cookies` * - `req.headers` * - `req.params` * - `req.query` * * @param fields a string or array of field names to validate/sanitize * @param message an error message to use when failed validations don't specify a custom message. * Defaults to `Invalid Value`. */ readonly check: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Same as {@link ExpressValidator.check}, but only validates in `req.body`. */ readonly body: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Same as {@link ExpressValidator.check}, but only validates in `req.cookies`. */ readonly cookie: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Same as {@link ExpressValidator.check}, but only validates in `req.headers`. */ readonly header: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Same as {@link ExpressValidator.check}, but only validates in `req.params`. */ readonly param: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Same as {@link ExpressValidator.check}, but only validates in `req.query`. */ readonly query: (fields?: string | string[], message?: FieldMessageFactory | ErrorMessage) => CustomValidationChain; /** * Checks whether the request contains exactly only those fields that have been validated. * * This method is here for convenience; it does exactly the same as `checkExact`. * * @see {@link checkExact} */ readonly checkExact: typeof checkExact; /** * Creates an express middleware with validations for multiple fields at once in the form of * a schema object. * * @param schema the schema to validate. * @param defaultLocations which locations to validate in each field. Defaults to every location. */ readonly checkSchema: (schema: CustomSchema, locations?: Location[]) => RunnableValidationChains>; /** * Creates a middleware that will ensure that at least one of the given validation chains * or validation chain groups are valid. * * If none are, a single error of type `alternative` is added to the request, * with the errors of each chain made available under the `nestedErrors` property. * * @param chains an array of validation chains to check if are valid. * If any of the items of `chains` is an array of validation chains, then all of them * must be valid together for the request to be considered valid. */ oneOf(chains: (CustomValidationChain | CustomValidationChain[])[], options?: OneOfOptions): Middleware & ContextRunner; /** * Extracts the validation errors of an express request using the default error formatter of this * instance. * * @see {@link validationResult()} * @param req the express request object * @returns a `Result` which will by default use the error formatter passed when * instantiating `ExpressValidator`. */ readonly validationResult: (req: Request) => Result; /** * Extracts data validated or sanitized from the request, and builds an object with them. * * This method is a shortcut for `matchedData`; it does nothing different than it. * * @see {@link matchedData} */ matchedData(req: Request, options?: Partial): Record; } export {};