util.d.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Error thrown by validation. Besides an informative message, it includes the path to the
  3. * property which triggered the failure.
  4. */
  5. export declare class VError extends Error {
  6. path: string;
  7. constructor(path: string, message: string);
  8. }
  9. /**
  10. * IContext is used during validation to collect error messages. There is a "noop" fast
  11. * implementation that does not pay attention to messages, and a full implementation that does.
  12. */
  13. export interface IContext {
  14. fail(relPath: string | number | null, message: string | null, score: number): false;
  15. unionResolver(): IUnionResolver;
  16. resolveUnion(ur: IUnionResolver): void;
  17. }
  18. /**
  19. * This helper class is used to collect error messages reported while validating unions.
  20. */
  21. export interface IUnionResolver {
  22. createContext(): IContext;
  23. }
  24. /**
  25. * IErrorDetail describes errors as returned by the validate() and validateStrict() methods.
  26. */
  27. export interface IErrorDetail {
  28. path: string;
  29. message: string;
  30. nested?: IErrorDetail[];
  31. }
  32. /**
  33. * Fast implementation of IContext used for first-pass validation. If that fails, we can validate
  34. * using DetailContext to collect error messages. That's faster for the common case when messages
  35. * normally pass validation.
  36. */
  37. export declare class NoopContext implements IContext, IUnionResolver {
  38. fail(relPath: string | number | null, message: string | null, score: number): false;
  39. unionResolver(): IUnionResolver;
  40. createContext(): IContext;
  41. resolveUnion(ur: IUnionResolver): void;
  42. }
  43. /**
  44. * Complete implementation of IContext that collects meaningfull errors.
  45. */
  46. export declare class DetailContext implements IContext {
  47. private _propNames;
  48. private _messages;
  49. private _score;
  50. fail(relPath: string | number | null, message: string | null, score: number): false;
  51. unionResolver(): IUnionResolver;
  52. resolveUnion(unionResolver: IUnionResolver): void;
  53. getError(path: string): VError;
  54. getErrorDetail(path: string): IErrorDetail | null;
  55. }