schema.d.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { CustomSanitizer, CustomValidator, ErrorMessage, FieldMessageFactory, Location, Request } from '../base';
  2. import { BailOptions, OptionalOptions, ValidationChain, ValidationChainLike } from '../chain';
  3. import { ResultWithContext } from '../chain/context-runner';
  4. import { Sanitizers } from '../chain/sanitizers';
  5. import { Validators } from '../chain/validators';
  6. type 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. };
  26. type 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. });
  32. type CustomValidatorSchemaOptions = BaseValidatorSchemaOptions & {
  33. /**
  34. * The implementation of a custom validator.
  35. */
  36. custom: CustomValidator;
  37. };
  38. export type ExtensionValidatorSchemaOptions = boolean | BaseValidatorSchemaOptions;
  39. export type ValidatorsSchema = {
  40. [K in Exclude<keyof Validators<any>, 'not' | 'withMessage'>]?: ValidatorSchemaOptions<K>;
  41. };
  42. type 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. };
  48. type CustomSanitizerSchemaOptions = {
  49. /**
  50. * The implementation of a custom sanitizer.
  51. */
  52. customSanitizer: CustomSanitizer;
  53. };
  54. export type ExtensionSanitizerSchemaOptions = true;
  55. export type SanitizersSchema = {
  56. [K in keyof Sanitizers<any>]?: SanitizerSchemaOptions<K>;
  57. };
  58. type 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. };
  76. export type DefaultSchemaKeys = keyof BaseParamSchema | keyof ValidatorsSchema | keyof SanitizersSchema;
  77. /**
  78. * Defines a schema of validations/sanitizations for a field
  79. */
  80. export 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. */
  86. export type Schema<T extends string = DefaultSchemaKeys> = Record<string, ParamSchema<T>>;
  87. /**
  88. * Shortcut type for the return of a {@link checkSchema()}-like function.
  89. */
  90. export 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. */
  98. export 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. */
  107. export declare const checkSchema: <T extends string = DefaultSchemaKeys>(schema: Schema<T>, defaultLocations?: Location[]) => RunnableValidationChains<ValidationChain>;
  108. export {};