@spinajs/validation 2.0.407 → 2.0.408
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/exceptions/index.d.ts +91 -1
- package/lib/cjs/exceptions/index.d.ts.map +1 -1
- package/lib/cjs/exceptions/index.js +156 -2
- package/lib/cjs/exceptions/index.js.map +1 -1
- package/lib/cjs/validator.d.ts +4 -0
- package/lib/cjs/validator.d.ts.map +1 -1
- package/lib/cjs/validator.js +22 -1
- package/lib/cjs/validator.js.map +1 -1
- package/lib/mjs/exceptions/index.d.ts +91 -1
- package/lib/mjs/exceptions/index.d.ts.map +1 -1
- package/lib/mjs/exceptions/index.js +156 -2
- package/lib/mjs/exceptions/index.js.map +1 -1
- package/lib/mjs/validator.d.ts +4 -0
- package/lib/mjs/validator.d.ts.map +1 -1
- package/lib/mjs/validator.js +22 -1
- package/lib/mjs/validator.js.map +1 -1
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +5 -5
|
@@ -1,12 +1,102 @@
|
|
|
1
1
|
import { Exception } from '@spinajs/exceptions';
|
|
2
2
|
import { ErrorObject } from 'ajv';
|
|
3
|
+
/**
|
|
4
|
+
* Detailed information about a validation error
|
|
5
|
+
*/
|
|
6
|
+
export interface IValidationErrorDetail {
|
|
7
|
+
/**
|
|
8
|
+
* The field path that failed validation (e.g., "/user/email")
|
|
9
|
+
*/
|
|
10
|
+
field: string;
|
|
11
|
+
/**
|
|
12
|
+
* The validation rule that failed (e.g., "required", "minLength", "pattern")
|
|
13
|
+
*/
|
|
14
|
+
rule: string;
|
|
15
|
+
/**
|
|
16
|
+
* Human-readable description of why the validation failed
|
|
17
|
+
*/
|
|
18
|
+
message: string;
|
|
19
|
+
/**
|
|
20
|
+
* The actual value that failed validation (if available)
|
|
21
|
+
*/
|
|
22
|
+
value?: any;
|
|
23
|
+
/**
|
|
24
|
+
* Additional parameters related to the validation rule
|
|
25
|
+
*/
|
|
26
|
+
params?: Record<string, any>;
|
|
27
|
+
}
|
|
3
28
|
/**
|
|
4
29
|
* The exception that is thrown when JSON entity is checked against schema and is invalid
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* try {
|
|
34
|
+
* validator.validate(schema, data);
|
|
35
|
+
* } catch (err) {
|
|
36
|
+
* if (err instanceof ValidationFailed) {
|
|
37
|
+
* // Access detailed error information
|
|
38
|
+
* console.log(err.details);
|
|
39
|
+
* // [{ field: '/email', rule: 'format', message: '/email must be a valid email', value: 'invalid-email' }]
|
|
40
|
+
*
|
|
41
|
+
* // Get human-readable summary
|
|
42
|
+
* console.log(err.getSummary());
|
|
43
|
+
* // "/email: /email must be a valid email; /age: /age must be at least 18"
|
|
44
|
+
*
|
|
45
|
+
* // Get errors for specific field
|
|
46
|
+
* const emailErrors = err.getFieldErrors('/email');
|
|
47
|
+
*
|
|
48
|
+
* // Access the data that failed
|
|
49
|
+
* console.log(err.data);
|
|
50
|
+
*
|
|
51
|
+
* // Access the expected schema
|
|
52
|
+
* console.log(err.schema);
|
|
53
|
+
*
|
|
54
|
+
* // Get requirements for a specific field
|
|
55
|
+
* const emailRequirements = err.getFieldRequirements('email');
|
|
56
|
+
* }
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
5
59
|
*/
|
|
6
60
|
export declare class ValidationFailed extends Exception {
|
|
7
61
|
parameter: IValidationError[];
|
|
8
|
-
|
|
62
|
+
/**
|
|
63
|
+
* The data that failed validation
|
|
64
|
+
*/
|
|
65
|
+
data: any;
|
|
66
|
+
/**
|
|
67
|
+
* The schema that was used for validation
|
|
68
|
+
*/
|
|
69
|
+
schema: any;
|
|
70
|
+
/**
|
|
71
|
+
* Detailed information about each validation failure
|
|
72
|
+
*/
|
|
73
|
+
details: IValidationErrorDetail[];
|
|
74
|
+
constructor(message: string, validationErrors: IValidationError[], data?: any, schema?: any);
|
|
75
|
+
/**
|
|
76
|
+
* Builds detailed error information from AJV validation errors
|
|
77
|
+
*/
|
|
78
|
+
private buildDetailedErrors;
|
|
79
|
+
/**
|
|
80
|
+
* Builds a human-readable error message from an AJV error
|
|
81
|
+
*/
|
|
82
|
+
private buildErrorMessage;
|
|
9
83
|
toString(): string;
|
|
84
|
+
/**
|
|
85
|
+
* Returns a summary of all validation errors
|
|
86
|
+
*/
|
|
87
|
+
getSummary(): string;
|
|
88
|
+
/**
|
|
89
|
+
* Gets all validation errors for a specific field
|
|
90
|
+
*/
|
|
91
|
+
getFieldErrors(field: string): IValidationErrorDetail[];
|
|
92
|
+
/**
|
|
93
|
+
* Gets the expected schema used for validation
|
|
94
|
+
*/
|
|
95
|
+
getExpectedSchema(): any;
|
|
96
|
+
/**
|
|
97
|
+
* Gets a simplified view of schema requirements for a specific field
|
|
98
|
+
*/
|
|
99
|
+
getFieldRequirements(field: string): any;
|
|
10
100
|
}
|
|
11
101
|
export interface IValidationError extends ErrorObject {
|
|
12
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,KAAK,CAAC,EAAE,GAAG,CAAC;IAEZ;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;IACtC,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAErC;;OAEG;IACI,IAAI,EAAE,GAAG,CAAC;IAEjB;;OAEG;IACI,MAAM,EAAE,GAAG,CAAC;IAEnB;;OAEG;IACI,OAAO,EAAE,sBAAsB,EAAE,CAAC;gBAE7B,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG;IAQ3F;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA2B3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IA8BzB,QAAQ,IAAI,MAAM;IAqClB;;OAEG;IACI,UAAU,IAAI,MAAM;IAI3B;;OAEG;IACI,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,sBAAsB,EAAE;IAI9D;;OAEG;IACI,iBAAiB,IAAI,GAAG;IAI/B;;OAEG;IACI,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG;CAkBhD;AAGD,MAAM,WAAW,gBAAiB,SAAQ,WAAW;CAAG"}
|
|
@@ -4,14 +4,168 @@ exports.ValidationFailed = void 0;
|
|
|
4
4
|
const exceptions_1 = require("@spinajs/exceptions");
|
|
5
5
|
/**
|
|
6
6
|
* The exception that is thrown when JSON entity is checked against schema and is invalid
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* try {
|
|
11
|
+
* validator.validate(schema, data);
|
|
12
|
+
* } catch (err) {
|
|
13
|
+
* if (err instanceof ValidationFailed) {
|
|
14
|
+
* // Access detailed error information
|
|
15
|
+
* console.log(err.details);
|
|
16
|
+
* // [{ field: '/email', rule: 'format', message: '/email must be a valid email', value: 'invalid-email' }]
|
|
17
|
+
*
|
|
18
|
+
* // Get human-readable summary
|
|
19
|
+
* console.log(err.getSummary());
|
|
20
|
+
* // "/email: /email must be a valid email; /age: /age must be at least 18"
|
|
21
|
+
*
|
|
22
|
+
* // Get errors for specific field
|
|
23
|
+
* const emailErrors = err.getFieldErrors('/email');
|
|
24
|
+
*
|
|
25
|
+
* // Access the data that failed
|
|
26
|
+
* console.log(err.data);
|
|
27
|
+
*
|
|
28
|
+
* // Access the expected schema
|
|
29
|
+
* console.log(err.schema);
|
|
30
|
+
*
|
|
31
|
+
* // Get requirements for a specific field
|
|
32
|
+
* const emailRequirements = err.getFieldRequirements('email');
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
7
36
|
*/
|
|
8
37
|
class ValidationFailed extends exceptions_1.Exception {
|
|
9
|
-
constructor(message, validationErrors) {
|
|
38
|
+
constructor(message, validationErrors, data, schema) {
|
|
10
39
|
super(message);
|
|
11
40
|
this.parameter = validationErrors;
|
|
41
|
+
this.data = data;
|
|
42
|
+
this.schema = schema;
|
|
43
|
+
this.details = this.buildDetailedErrors(validationErrors, data);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Builds detailed error information from AJV validation errors
|
|
47
|
+
*/
|
|
48
|
+
buildDetailedErrors(errors, data) {
|
|
49
|
+
return errors.map((error) => {
|
|
50
|
+
const detail = {
|
|
51
|
+
field: error.instancePath || '(root)',
|
|
52
|
+
rule: error.keyword,
|
|
53
|
+
message: this.buildErrorMessage(error),
|
|
54
|
+
params: error.params,
|
|
55
|
+
};
|
|
56
|
+
// Try to extract the actual value that failed validation
|
|
57
|
+
if (data && error.instancePath) {
|
|
58
|
+
try {
|
|
59
|
+
const path = error.instancePath.split('/').filter(p => p.length > 0);
|
|
60
|
+
let value = data;
|
|
61
|
+
for (const segment of path) {
|
|
62
|
+
value = value?.[segment];
|
|
63
|
+
}
|
|
64
|
+
detail.value = value;
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// If we can't extract the value, just skip it
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return detail;
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Builds a human-readable error message from an AJV error
|
|
75
|
+
*/
|
|
76
|
+
buildErrorMessage(error) {
|
|
77
|
+
const field = error.instancePath || 'root';
|
|
78
|
+
const params = error.params;
|
|
79
|
+
switch (error.keyword) {
|
|
80
|
+
case 'required':
|
|
81
|
+
return `Field '${params.missingProperty}' is required`;
|
|
82
|
+
case 'type':
|
|
83
|
+
return `${field} must be of type ${params.type}`;
|
|
84
|
+
case 'minLength':
|
|
85
|
+
return `${field} must be at least ${params.limit} characters long`;
|
|
86
|
+
case 'maxLength':
|
|
87
|
+
return `${field} must not exceed ${params.limit} characters`;
|
|
88
|
+
case 'minimum':
|
|
89
|
+
return `${field} must be at least ${params.limit}`;
|
|
90
|
+
case 'maximum':
|
|
91
|
+
return `${field} must not exceed ${params.limit}`;
|
|
92
|
+
case 'pattern':
|
|
93
|
+
return `${field} does not match the required pattern`;
|
|
94
|
+
case 'format':
|
|
95
|
+
return `${field} must be a valid ${params.format}`;
|
|
96
|
+
case 'enum':
|
|
97
|
+
return `${field} must be one of: ${params.allowedValues?.join(', ')}`;
|
|
98
|
+
case 'additionalProperties':
|
|
99
|
+
return `${field} has additional property '${params.additionalProperty}' which is not allowed`;
|
|
100
|
+
default:
|
|
101
|
+
return error.message || `${field} failed validation rule '${error.keyword}'`;
|
|
102
|
+
}
|
|
12
103
|
}
|
|
13
104
|
toString() {
|
|
14
|
-
|
|
105
|
+
const detailedErrors = this.details
|
|
106
|
+
.map((detail) => {
|
|
107
|
+
const parts = [
|
|
108
|
+
` - Field: ${detail.field}`,
|
|
109
|
+
` Rule: ${detail.rule}`,
|
|
110
|
+
` Message: ${detail.message}`,
|
|
111
|
+
];
|
|
112
|
+
if (detail.value !== undefined) {
|
|
113
|
+
parts.push(` Value: ${JSON.stringify(detail.value)}`);
|
|
114
|
+
}
|
|
115
|
+
if (detail.params && Object.keys(detail.params).length > 0) {
|
|
116
|
+
parts.push(` Params: ${JSON.stringify(detail.params)}`);
|
|
117
|
+
}
|
|
118
|
+
return parts.join('\n');
|
|
119
|
+
})
|
|
120
|
+
.join('\n\n');
|
|
121
|
+
let result = `${this.message}\n\nValidation errors:\n${detailedErrors}`;
|
|
122
|
+
// Optionally include schema information if available
|
|
123
|
+
if (this.schema) {
|
|
124
|
+
const schemaId = this.schema.$id || this.schema.title || 'inline schema';
|
|
125
|
+
result += `\n\nExpected schema: ${schemaId}`;
|
|
126
|
+
// Include schema description if available
|
|
127
|
+
if (this.schema.description) {
|
|
128
|
+
result += `\nDescription: ${this.schema.description}`;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Returns a summary of all validation errors
|
|
135
|
+
*/
|
|
136
|
+
getSummary() {
|
|
137
|
+
return this.details.map(d => `${d.field}: ${d.message}`).join('; ');
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Gets all validation errors for a specific field
|
|
141
|
+
*/
|
|
142
|
+
getFieldErrors(field) {
|
|
143
|
+
return this.details.filter(d => d.field === field || d.field.startsWith(field + '/'));
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Gets the expected schema used for validation
|
|
147
|
+
*/
|
|
148
|
+
getExpectedSchema() {
|
|
149
|
+
return this.schema;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Gets a simplified view of schema requirements for a specific field
|
|
153
|
+
*/
|
|
154
|
+
getFieldRequirements(field) {
|
|
155
|
+
if (!this.schema || !this.schema.properties) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const path = field.split('/').filter(p => p.length > 0);
|
|
159
|
+
let schemaNode = this.schema;
|
|
160
|
+
for (const segment of path) {
|
|
161
|
+
if (schemaNode && schemaNode.properties && schemaNode.properties[segment]) {
|
|
162
|
+
schemaNode = schemaNode.properties[segment];
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return schemaNode;
|
|
15
169
|
}
|
|
16
170
|
}
|
|
17
171
|
exports.ValidationFailed = ValidationFailed;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/exceptions/index.ts"],"names":[],"mappings":";;;AAAA,oDAAgD;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/exceptions/index.ts"],"names":[],"mappings":";;;AAAA,oDAAgD;AAiChD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAa,gBAAiB,SAAQ,sBAAS;IAkB7C,YAAY,OAAe,EAAE,gBAAoC,EAAE,IAAU,EAAE,MAAY;QACzF,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,SAAS,GAAG,gBAAgB,CAAC;QAClC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,mBAAmB,CAAC,gBAAgB,EAAE,IAAI,CAAC,CAAC;IAClE,CAAC;IAED;;OAEG;IACK,mBAAmB,CAAC,MAA0B,EAAE,IAAS;QAC/D,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YAC1B,MAAM,MAAM,GAA2B;gBACrC,KAAK,EAAE,KAAK,CAAC,YAAY,IAAI,QAAQ;gBACrC,IAAI,EAAE,KAAK,CAAC,OAAO;gBACnB,OAAO,EAAE,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC;gBACtC,MAAM,EAAE,KAAK,CAAC,MAAM;aACrB,CAAC;YAEF,yDAAyD;YACzD,IAAI,IAAI,IAAI,KAAK,CAAC,YAAY,EAAE,CAAC;gBAC/B,IAAI,C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IAAI,CAAC,MAAM,CAAC;QAE7B,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;YAC3B,IAAI,UAAU,IAAK,UAAkB,CAAC,UAAU,IAAK,UAAkB,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC5F,UAAU,GAAI,UAAkB,CAAC,UAAU,CAAC,OAAO,CAAC,CAAC;YACvD,CAAC;iBAAM,CAAC;gBACN,OAAO,IAAI,CAAC;YACd,CAAC;QACH,CAAC;QAED,OAAO,UAAU,CAAC;IACpB,CAAC;CACF;AAxKD,4CAwKC"}
|
package/lib/cjs/validator.d.ts
CHANGED
|
@@ -42,6 +42,10 @@ export declare class DataValidator extends AsyncService {
|
|
|
42
42
|
* @param data - to be validated
|
|
43
43
|
*/
|
|
44
44
|
tryValidate(schema: object | string, data: object): [boolean, IValidationError[]];
|
|
45
|
+
/**
|
|
46
|
+
* Internal method to get the schema being used for validation
|
|
47
|
+
*/
|
|
48
|
+
private getValidationSchema;
|
|
45
49
|
extractSchema(object: any): ISchemaObject;
|
|
46
50
|
/**
|
|
47
51
|
* Validate given data. When failed, exception is thrown
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.d.ts","sourceRoot":"","sources":["../../src/validator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,SAAS,EAAE,YAAY,EAAa,MAAM,aAAa,CAAC;AAG7E,OAAO,EAAE,gBAAgB,EAAoB,MAAM,uBAAuB,CAAC;AAG3E,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAAU,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAGlD,OAAO,cAAc,CAAC;AAQtB;;;;GAIG;AAGH,qBACa,aAAc,SAAQ,YAAY;IAEtC,OAAO,EAAE,kBAAkB,CAAC;IAGnC,SAAS,CAAC,OAAO,EAAE,YAAY,EAAE,CAAC;IAGlC,SAAS,CAAC,GAAG,EAAE,GA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GAAG,OAAO;IAI3C;;;;;OAKG;IACI,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,CAAC,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC/D;;;;;OAKG;IACI,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAyBxF;;OAEG;IACH,OAAO,CAAC,mBAAmB;IAiBpB,aAAa,CAAC,MAAM,EAAE,GAAG;IAIhC;;;;;OAKG;IACI,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAEnC;;;;;;OAMG;IACI,QAAQ,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI;CAiB7D"}
|
package/lib/cjs/validator.js
CHANGED
|
@@ -125,19 +125,40 @@ let DataValidator = class DataValidator extends di_1.AsyncService {
|
|
|
125
125
|
}
|
|
126
126
|
return [true, null];
|
|
127
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Internal method to get the schema being used for validation
|
|
130
|
+
*/
|
|
131
|
+
getValidationSchema(schemaOrData, data) {
|
|
132
|
+
let schema = null;
|
|
133
|
+
if (data === null || data === undefined) {
|
|
134
|
+
schema = Reflect.getMetadata(decorators_js_1.SCHEMA_SYMBOL, schemaOrData);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
if (typeof schemaOrData === 'string') {
|
|
138
|
+
/* eslint-disable */
|
|
139
|
+
schema = this.Validator.getSchema(schemaOrData)?.schema ?? null;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
schema = schemaOrData;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return schema;
|
|
146
|
+
}
|
|
128
147
|
extractSchema(object) {
|
|
129
148
|
return Reflect.getMetadata(decorators_js_1.SCHEMA_SYMBOL, object) ?? Reflect.getMetadata(decorators_js_1.SCHEMA_SYMBOL, object.prototype);
|
|
130
149
|
}
|
|
131
150
|
validate(schemaOrData, data) {
|
|
132
151
|
const [isValid, errors] = this.tryValidate(schemaOrData, data);
|
|
133
152
|
if (!isValid) {
|
|
153
|
+
const validatedData = data !== null && data !== undefined ? data : schemaOrData;
|
|
154
|
+
const usedSchema = this.getValidationSchema(schemaOrData, data);
|
|
134
155
|
switch (errors[0].keyword) {
|
|
135
156
|
case 'invalid_argument':
|
|
136
157
|
throw new exceptions_1.InvalidArgument('data is null or undefined');
|
|
137
158
|
case 'empty_schema':
|
|
138
159
|
throw new exceptions_1.InvalidArgument('objects schema is not set');
|
|
139
160
|
default:
|
|
140
|
-
throw new index_js_1.ValidationFailed('validation error', errors);
|
|
161
|
+
throw new index_js_1.ValidationFailed('validation error', errors, validatedData, usedSchema);
|
|
141
162
|
}
|
|
142
163
|
}
|
|
143
164
|
}
|
package/lib/cjs/validator.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validator.js","sourceRoot":"","sources":["../../src/validator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oCAA6E;AAC7E,8CAAsB;AACtB,0DAAgD;AAChD,oDAA2E;AAC3E,oDAAwE;AACxE,mDAAgD;AAChD,yCAA6E;AAC7E,oDAAkD;AAElD,wBAAwB;AACxB,wBAAsB;AAGtB,sEAA0D;AAC1D,8DAAoD;AACpD,gEAAsD;AAGtD;;;;GAIG;AAII,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,iBAAY;IAoBtC,KAAK,CAAC,OAAO;QAClB,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC/C,MAAM,IAAI,6BAAgB,CAAC,yDAAyD,CAAC,CAAC;QACxF,CAAC;QAED,MAAM,SAAS,GAAG;YAChB,MAAM,EAAE;gBACN,GAAG,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,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AAI,CAAC,CAAC;QAC/D,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,aAAa,GAAG,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,YAAY,CAAC;YAChF,MAAM,UAAU,GAAG,IAAI,CAAC,mBAAmB,CAAC,YAAY,EAAE,IAAI,CAAC,CAAC;YAEhE,QAAQ,MAAM,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,CAAC;gBAC1B,KAAK,kBAAkB;oBACrB,MAAM,IAAI,4BAAe,CAAC,2BAA2B,CAAC,CAAC;gBACzD,KAAK,cAAc;oBACjB,MAAM,IAAI,4BAAe,CAAC,2BAA2B,CAAC,CAAC;gBACzD;oBACE,MAAM,IAAI,2BAAgB,CAAC,kBAAkB,EAAE,MAAM,EAAE,aAAa,EAAE,UAAU,CAAC,CAAC;YACtF,CAAC;QACH,CAAC;IACH,CAAC;CACF,CAAA;AAtMY,sCAAa;AAEjB;IADN,IAAA,sBAAM,EAAC,YAAY,CAAC;;8CACc;AAGzB;IADT,IAAA,eAAU,EAAC,uBAAY,CAAC;;8CACS;AAGxB;IADT,IAAA,mBAAM,EAAC,YAAY,CAAC;8BACN,gBAAG;0CAAC;AAUT;IADT,IAAA,eAAU,GAAE;8BACQ,cAAS;gDAAC;wBAlBpB,aAAa;IADzB,IAAA,cAAS,GAAE;GACC,aAAa,CAsMzB"}
|
|
@@ -1,12 +1,102 @@
|
|
|
1
1
|
import { Exception } from '@spinajs/exceptions';
|
|
2
2
|
import { ErrorObject } from 'ajv';
|
|
3
|
+
/**
|
|
4
|
+
* Detailed information about a validation error
|
|
5
|
+
*/
|
|
6
|
+
export interface IValidationErrorDetail {
|
|
7
|
+
/**
|
|
8
|
+
* The field path that failed validation (e.g., "/user/email")
|
|
9
|
+
*/
|
|
10
|
+
field: string;
|
|
11
|
+
/**
|
|
12
|
+
* The validation rule that failed (e.g., "required", "minLength", "pattern")
|
|
13
|
+
*/
|
|
14
|
+
rule: string;
|
|
15
|
+
/**
|
|
16
|
+
* Human-readable description of why the validation failed
|
|
17
|
+
*/
|
|
18
|
+
message: string;
|
|
19
|
+
/**
|
|
20
|
+
* The actual value that failed validation (if available)
|
|
21
|
+
*/
|
|
22
|
+
value?: any;
|
|
23
|
+
/**
|
|
24
|
+
* Additional parameters related to the validation rule
|
|
25
|
+
*/
|
|
26
|
+
params?: Record<string, any>;
|
|
27
|
+
}
|
|
3
28
|
/**
|
|
4
29
|
* The exception that is thrown when JSON entity is checked against schema and is invalid
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* try {
|
|
34
|
+
* validator.validate(schema, data);
|
|
35
|
+
* } catch (err) {
|
|
36
|
+
* if (err instanceof ValidationFailed) {
|
|
37
|
+
* // Access detailed error information
|
|
38
|
+
* console.log(err.details);
|
|
39
|
+
* // [{ field: '/email', rule: 'format', message: '/email must be a valid email', value: 'invalid-email' }]
|
|
40
|
+
*
|
|
41
|
+
* // Get human-readable summary
|
|
42
|
+
* console.log(err.getSummary());
|
|
43
|
+
* // "/email: /email must be a valid email; /age: /age must be at least 18"
|
|
44
|
+
*
|
|
45
|
+
* // Get errors for specific field
|
|
46
|
+
* const emailErrors = err.getFieldErrors('/email');
|
|
47
|
+
*
|
|
48
|
+
* // Access the data that failed
|
|
49
|
+
* console.log(err.data);
|
|
50
|
+
*
|
|
51
|
+
* // Access the expected schema
|
|
52
|
+
* console.log(err.schema);
|
|
53
|
+
*
|
|
54
|
+
* // Get requirements for a specific field
|
|
55
|
+
* const emailRequirements = err.getFieldRequirements('email');
|
|
56
|
+
* }
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
5
59
|
*/
|
|
6
60
|
export declare class ValidationFailed extends Exception {
|
|
7
61
|
parameter: IValidationError[];
|
|
8
|
-
|
|
62
|
+
/**
|
|
63
|
+
* The data that failed validation
|
|
64
|
+
*/
|
|
65
|
+
data: any;
|
|
66
|
+
/**
|
|
67
|
+
* The schema that was used for validation
|
|
68
|
+
*/
|
|
69
|
+
schema: any;
|
|
70
|
+
/**
|
|
71
|
+
* Detailed information about each validation failure
|
|
72
|
+
*/
|
|
73
|
+
details: IValidationErrorDetail[];
|
|
74
|
+
constructor(message: string, validationErrors: IValidationError[], data?: any, schema?: any);
|
|
75
|
+
/**
|
|
76
|
+
* Builds detailed error information from AJV validation errors
|
|
77
|
+
*/
|
|
78
|
+
private buildDetailedErrors;
|
|
79
|
+
/**
|
|
80
|
+
* Builds a human-readable error message from an AJV error
|
|
81
|
+
*/
|
|
82
|
+
private buildErrorMessage;
|
|
9
83
|
toString(): string;
|
|
84
|
+
/**
|
|
85
|
+
* Returns a summary of all validation errors
|
|
86
|
+
*/
|
|
87
|
+
getSummary(): string;
|
|
88
|
+
/**
|
|
89
|
+
* Gets all validation errors for a specific field
|
|
90
|
+
*/
|
|
91
|
+
getFieldErrors(field: string): IValidationErrorDetail[];
|
|
92
|
+
/**
|
|
93
|
+
* Gets the expected schema used for validation
|
|
94
|
+
*/
|
|
95
|
+
getExpectedSchema(): any;
|
|
96
|
+
/**
|
|
97
|
+
* Gets a simplified view of schema requirements for a specific field
|
|
98
|
+
*/
|
|
99
|
+
getFieldRequirements(field: string): any;
|
|
10
100
|
}
|
|
11
101
|
export interface IValidationError extends ErrorObject {
|
|
12
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/exceptions/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAChD,OAAO,EAAE,WAAW,EAAE,MAAM,KAAK,CAAC;AAElC;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;OAEG;IACH,OAAO,EAAE,MAAM,CAAC;IAEhB;;OAEG;IACH,KAAK,CAAC,EAAE,GAAG,CAAC;IAEZ;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAC9B;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,gBAAiB,SAAQ,SAAS;IACtC,SAAS,EAAE,gBAAgB,EAAE,CAAC;IAErC;;OAEG;IACI,IAAI,EAAE,GAAG,CAAC;IAEjB;;OAEG;IACI,MAAM,EAAE,GAAG,CAAC;IAEnB;;OAEG;IACI,OAAO,EAAE,sBAAsB,EAAE,CAAC;gBAE7B,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,EAAE,IAAI,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,EAAE,GAAG;IAQ3F;;OAEG;IACH,OAAO,CAAC,mBAAmB;IA2B3B;;OAEG;IACH,OAAO,CAAC,iBAAiB;IA8BzB,QAAQ,IAAI,MAAM;IAqClB;;OAEG;IACI,UAAU,IAAI,MAAM;IAI3B;;OAEG;IACI,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,sBAAsB,EAAE;IAI9D;;OAEG;IACI,iBAAiB,IAAI,GAAG;IAI/B;;OAEG;IACI,oBAAoB,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG;CAkBhD;AAGD,MAAM,WAAW,gBAAiB,SAAQ,WAAW;CAAG"}
|
|
@@ -1,14 +1,168 @@
|
|
|
1
1
|
import { Exception } from '@spinajs/exceptions';
|
|
2
2
|
/**
|
|
3
3
|
* The exception that is thrown when JSON entity is checked against schema and is invalid
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* try {
|
|
8
|
+
* validator.validate(schema, data);
|
|
9
|
+
* } catch (err) {
|
|
10
|
+
* if (err instanceof ValidationFailed) {
|
|
11
|
+
* // Access detailed error information
|
|
12
|
+
* console.log(err.details);
|
|
13
|
+
* // [{ field: '/email', rule: 'format', message: '/email must be a valid email', value: 'invalid-email' }]
|
|
14
|
+
*
|
|
15
|
+
* // Get human-readable summary
|
|
16
|
+
* console.log(err.getSummary());
|
|
17
|
+
* // "/email: /email must be a valid email; /age: /age must be at least 18"
|
|
18
|
+
*
|
|
19
|
+
* // Get errors for specific field
|
|
20
|
+
* const emailErrors = err.getFieldErrors('/email');
|
|
21
|
+
*
|
|
22
|
+
* // Access the data that failed
|
|
23
|
+
* console.log(err.data);
|
|
24
|
+
*
|
|
25
|
+
* // Access the expected schema
|
|
26
|
+
* console.log(err.schema);
|
|
27
|
+
*
|
|
28
|
+
* // Get requirements for a specific field
|
|
29
|
+
* const emailRequirements = err.getFieldRequirements('email');
|
|
30
|
+
* }
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
4
33
|
*/
|
|
5
34
|
export class ValidationFailed extends Exception {
|
|
6
|
-
constructor(message, validationErrors) {
|
|
35
|
+
constructor(message, validationErrors, data, schema) {
|
|
7
36
|
super(message);
|
|
8
37
|
this.parameter = validationErrors;
|
|
38
|
+
this.data = data;
|
|
39
|
+
this.schema = schema;
|
|
40
|
+
this.details = this.buildDetailedErrors(validationErrors, data);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Builds detailed error information from AJV validation errors
|
|
44
|
+
*/
|
|
45
|
+
buildDetailedErrors(errors, data) {
|
|
46
|
+
return errors.map((error) => {
|
|
47
|
+
const detail = {
|
|
48
|
+
field: error.instancePath || '(root)',
|
|
49
|
+
rule: error.keyword,
|
|
50
|
+
message: this.buildErrorMessage(error),
|
|
51
|
+
params: error.params,
|
|
52
|
+
};
|
|
53
|
+
// Try to extract the actual value that failed validation
|
|
54
|
+
if (data && error.instancePath) {
|
|
55
|
+
try {
|
|
56
|
+
const path = error.instancePath.split('/').filter(p => p.length > 0);
|
|
57
|
+
let value = data;
|
|
58
|
+
for (const segment of path) {
|
|
59
|
+
value = value?.[segment];
|
|
60
|
+
}
|
|
61
|
+
detail.value = value;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
// If we can't extract the value, just skip it
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return detail;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Builds a human-readable error message from an AJV error
|
|
72
|
+
*/
|
|
73
|
+
buildErrorMessage(error) {
|
|
74
|
+
const field = error.instancePath || 'root';
|
|
75
|
+
const params = error.params;
|
|
76
|
+
switch (error.keyword) {
|
|
77
|
+
case 'required':
|
|
78
|
+
return `Field '${params.missingProperty}' is required`;
|
|
79
|
+
case 'type':
|
|
80
|
+
return `${field} must be of type ${params.type}`;
|
|
81
|
+
case 'minLength':
|
|
82
|
+
return `${field} must be at least ${params.limit} characters long`;
|
|
83
|
+
case 'maxLength':
|
|
84
|
+
return `${field} must not exceed ${params.limit} characters`;
|
|
85
|
+
case 'minimum':
|
|
86
|
+
return `${field} must be at least ${params.limit}`;
|
|
87
|
+
case 'maximum':
|
|
88
|
+
return `${field} must not exceed ${params.limit}`;
|
|
89
|
+
case 'pattern':
|
|
90
|
+
return `${field} does not match the required pattern`;
|
|
91
|
+
case 'format':
|
|
92
|
+
return `${field} must be a valid ${params.format}`;
|
|
93
|
+
case 'enum':
|
|
94
|
+
return `${field} must be one of: ${params.allowedValues?.join(', ')}`;
|
|
95
|
+
case 'additionalProperties':
|
|
96
|
+
return `${field} has additional property '${params.additionalProperty}' which is not allowed`;
|
|
97
|
+
default:
|
|
98
|
+
return error.message || `${field} failed validation rule '${error.keyword}'`;
|
|
99
|
+
}
|
|
9
100
|
}
|
|
10
101
|
toString() {
|
|
11
|
-
|
|
102
|
+
const detailedErrors = this.details
|
|
103
|
+
.map((detail) => {
|
|
104
|
+
const parts = [
|
|
105
|
+
` - Field: ${detail.field}`,
|
|
106
|
+
` Rule: ${detail.rule}`,
|
|
107
|
+
` Message: ${detail.message}`,
|
|
108
|
+
];
|
|
109
|
+
if (detail.value !== undefined) {
|
|
110
|
+
parts.push(` Value: ${JSON.stringify(detail.value)}`);
|
|
111
|
+
}
|
|
112
|
+
if (detail.params && Object.keys(detail.params).length > 0) {
|
|
113
|
+
parts.push(` Params: ${JSON.stringify(detail.params)}`);
|
|
114
|
+
}
|
|
115
|
+
return parts.join('\n');
|
|
116
|
+
})
|
|
117
|
+
.join('\n\n');
|
|
118
|
+
let result = `${this.message}\n\nValidation errors:\n${detailedErrors}`;
|
|
119
|
+
// Optionally include schema information if available
|
|
120
|
+
if (this.schema) {
|
|
121
|
+
const schemaId = this.schema.$id || this.schema.title || 'inline schema';
|
|
122
|
+
result += `\n\nExpected schema: ${schemaId}`;
|
|
123
|
+
// Include schema description if available
|
|
124
|
+
if (this.schema.description) {
|
|
125
|
+
result += `\nDescription: ${this.schema.description}`;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return result;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Returns a summary of all validation errors
|
|
132
|
+
*/
|
|
133
|
+
getSummary() {
|
|
134
|
+
return this.details.map(d => `${d.field}: ${d.message}`).join('; ');
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Gets all validation errors for a specific field
|
|
138
|
+
*/
|
|
139
|
+
getFieldErrors(field) {
|
|
140
|
+
return this.details.filter(d => d.field === field || d.field.startsWith(field + '/'));
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Gets the expected schema used for validation
|
|
144
|
+
*/
|
|
145
|
+
getExpectedSchema() {
|
|
146
|
+
return this.schema;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Gets a simplified view of schema requirements for a specific field
|
|
150
|
+
*/
|
|
151
|
+
getFieldRequirements(field) {
|
|
152
|
+
if (!this.schema || !this.schema.properties) {
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const path = field.split('/').filter(p => p.length > 0);
|
|
156
|
+
let schemaNode = this.schema;
|
|
157
|
+
for (const segment of path) {
|
|
158
|
+
if (schemaNode && schemaNode.properties && schemaNode.properties[segment]) {
|
|
159
|
+
schemaNode = schemaNode.properties[segment];
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return schemaNode;
|
|
12
166
|
}
|
|
13
167
|
}
|
|
14
168
|
//# sourceMappingURL=index.js.map
|