context.d.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { FieldInstance, FieldValidationError, Location, Meta, Request, UnknownFieldInstance, ValidationError } from './base';
  2. import { ContextItem } from './context-items';
  3. /**
  4. * Defines which kind of value makes a field optional.
  5. *
  6. * - `undefined`: only `undefined` values; equivalent to `value === undefined`
  7. * - `null`: only `undefined` and `null` values; equivalent to `value == null`
  8. * - `falsy`: all falsy values; equivalent to `!value`
  9. * - `false`: not optional.
  10. */
  11. export type Optional = 'undefined' | 'null' | 'falsy' | false;
  12. export type AddErrorOptions = {
  13. type: 'field';
  14. message?: any;
  15. value: any;
  16. meta: Meta;
  17. } | {
  18. type: 'unknown_fields';
  19. req: Request;
  20. message?: any;
  21. fields: UnknownFieldInstance[];
  22. } | {
  23. type: 'alternative';
  24. req: Request;
  25. message?: any;
  26. nestedErrors: FieldValidationError[];
  27. } | {
  28. type: 'alternative_grouped';
  29. req: Request;
  30. message?: any;
  31. nestedErrors: FieldValidationError[][];
  32. };
  33. export type ValueVisibility = {
  34. type: 'visible';
  35. } | {
  36. type: 'hidden';
  37. } | {
  38. type: 'redacted';
  39. value: string;
  40. };
  41. export declare class Context {
  42. readonly fields: string[];
  43. readonly locations: Location[];
  44. readonly stack: ReadonlyArray<ContextItem>;
  45. readonly optional: Optional;
  46. readonly bail: boolean;
  47. readonly visibility: ValueVisibility;
  48. readonly message?: any | undefined;
  49. private readonly _errors;
  50. get errors(): ReadonlyArray<ValidationError>;
  51. private readonly dataMap;
  52. constructor(fields: string[], locations: Location[], stack: ReadonlyArray<ContextItem>, optional: Optional, bail: boolean, visibility?: ValueVisibility, message?: any | undefined);
  53. getData(options?: {
  54. requiredOnly: boolean;
  55. }): FieldInstance[];
  56. addFieldInstances(instances: FieldInstance[]): void;
  57. setData(path: string, value: any, location: Location): void;
  58. addError(opts: AddErrorOptions): void;
  59. private updateVisibility;
  60. }
  61. export type ReadonlyContext = Pick<Context, Exclude<keyof Context, 'setData' | 'addFieldInstances' | 'addError'>>;