UNPKG

5.13 kBTypeScriptView Raw
1import type { Model } from '..';
2import type { ErrorOptions } from './base-error';
3import BaseError from './base-error';
4/**
5 * An enum that is used internally by the `ValidationErrorItem` class
6 * that maps current `type` strings (as given to ValidationErrorItem.constructor()) to
7 * our new `origin` values.
8 */
9export declare enum ValidationErrorItemType {
10 'notnull violation' = "CORE",
11 'string violation' = "CORE",
12 'unique violation' = "DB",
13 'validation error' = "FUNCTION"
14}
15/**
16 * An enum that defines valid ValidationErrorItem `origin` values
17 */
18export declare enum ValidationErrorItemOrigin {
19 /**
20 * specifies errors that originate from the sequelize "core"
21 */
22 CORE = "CORE",
23 /**
24 * specifies validation errors that originate from the storage engine
25 */
26 DB = "DB",
27 /**
28 * specifies validation errors that originate from validator functions (both built-in and custom) defined for a given attribute
29 */
30 FUNCTION = "FUNCTION"
31}
32/**
33 * Validation Error Item
34 * Instances of this class are included in the `ValidationError.errors` property.
35 */
36export declare class ValidationErrorItem {
37 /**
38 * @deprecated Will be removed in v7
39 */
40 static TypeStringMap: typeof ValidationErrorItemType;
41 /**
42 * @deprecated Will be removed in v7
43 */
44 static Origins: typeof ValidationErrorItemOrigin;
45 /**
46 * An error message
47 */
48 readonly message: string;
49 /**
50 * The type/origin of the validation error
51 */
52 readonly type: keyof typeof ValidationErrorItemType | null;
53 /**
54 * The field that triggered the validation error
55 */
56 readonly path: string | null;
57 /**
58 * The value that generated the error
59 */
60 readonly value: string | null;
61 readonly origin: keyof typeof ValidationErrorItemOrigin | null;
62 /**
63 * The DAO instance that caused the validation error
64 */
65 readonly instance: Model | null;
66 /**
67 * A validation "key", used for identification
68 */
69 readonly validatorKey: string | null;
70 /**
71 * Property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable
72 */
73 readonly validatorName: string | null;
74 /**
75 * Parameters used with the BUILT-IN validator function, if applicable
76 */
77 readonly validatorArgs: unknown[];
78 /**
79 * Creates a new ValidationError item. Instances of this class are included in the `ValidationError.errors` property.
80 *
81 * @param message An error message
82 * @param type The type/origin of the validation error
83 * @param path The field that triggered the validation error
84 * @param value The value that generated the error
85 * @param instance the DAO instance that caused the validation error
86 * @param validatorKey a validation "key", used for identification
87 * @param fnName property name of the BUILT-IN validator function that caused the validation error (e.g. "in" or "len"), if applicable
88 * @param fnArgs parameters used with the BUILT-IN validator function, if applicable
89 */
90 constructor(message: string, type: keyof typeof ValidationErrorItemType | keyof typeof ValidationErrorItemOrigin, path: string, value: string, instance: Model, validatorKey: string, fnName: string, fnArgs: unknown[]);
91 private isValidationErrorItemOrigin;
92 private normalizeString;
93 /**
94 * return a lowercase, trimmed string "key" that identifies the validator.
95 *
96 * Note: the string will be empty if the instance has neither a valid `validatorKey` property nor a valid `validatorName` property
97 *
98 * @param useTypeAsNS controls whether the returned value is "namespace",
99 * this parameter is ignored if the validator's `type` is not one of ValidationErrorItem.Origins
100 * @param NSSeparator a separator string for concatenating the namespace, must be not be empty,
101 * defaults to "." (fullstop). only used and validated if useTypeAsNS is TRUE.
102 * @throws {Error} thrown if NSSeparator is found to be invalid.
103 */
104 getValidatorKey(useTypeAsNS: boolean, NSSeparator: string): string;
105}
106/**
107 * Validation Error. Thrown when the sequelize validation has failed. The error contains an `errors` property,
108 * which is an array with 1 or more ValidationErrorItems, one for each validation that failed.
109 *
110 * @param message Error message
111 * @param errors Array of ValidationErrorItem objects describing the validation errors
112 */
113declare class ValidationError extends BaseError {
114 /** Array of ValidationErrorItem objects describing the validation errors */
115 readonly errors: ValidationErrorItem[];
116 constructor(message: string, errors: ValidationErrorItem[], options?: ErrorOptions);
117 /**
118 * Gets all validation error items for the path / field specified.
119 *
120 * @param {string} path The path to be checked for error items
121 *
122 * @returns {Array<ValidationErrorItem>} Validation error items for the specified path
123 */
124 get(path: string): ValidationErrorItem[];
125}
126export default ValidationError;