@travetto/schema 5.0.10 → 5.0.11
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@travetto/schema",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.11",
|
|
4
4
|
"description": "Data type registry for runtime validation, reflection and binding.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -27,10 +27,10 @@
|
|
|
27
27
|
"directory": "module/schema"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@travetto/registry": "^5.0.
|
|
30
|
+
"@travetto/registry": "^5.0.11"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@travetto/transformer": "^5.0.
|
|
33
|
+
"@travetto/transformer": "^5.0.9"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@travetto/transformer": {
|
package/src/validate/error.ts
CHANGED
|
@@ -8,7 +8,7 @@ import { ValidationError } from './types';
|
|
|
8
8
|
*/
|
|
9
9
|
export class ValidationResultError extends AppError<{ errors: ValidationError[] }> {
|
|
10
10
|
constructor(errors: ValidationError[]) {
|
|
11
|
-
super('Validation errors have occurred', 'data', { errors });
|
|
11
|
+
super('Validation errors have occurred', { category: 'data', details: { errors } });
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
@@ -18,7 +18,7 @@ export class ValidationResultError extends AppError<{ errors: ValidationError[]
|
|
|
18
18
|
*/
|
|
19
19
|
export class TypeMismatchError extends AppError {
|
|
20
20
|
constructor(cls: Class | string, type: string) {
|
|
21
|
-
super(`Expected ${typeof cls === 'string' ? cls : cls.name} but found ${type}`, 'data');
|
|
21
|
+
super(`Expected ${typeof cls === 'string' ? cls : cls.name} but found ${type}`, { category: 'data' });
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
package/src/validate/messages.ts
CHANGED
|
@@ -8,8 +8,8 @@ export const Messages = new Map<string, string>(Object.entries({
|
|
|
8
8
|
minlength: '{path} is not long enough ({n})',
|
|
9
9
|
maxlength: '{path} is too long ({n})',
|
|
10
10
|
match: '{path} should match {re}',
|
|
11
|
-
min: '{path} is
|
|
12
|
-
max: '{path} is
|
|
11
|
+
min: '{path} is less than ({n})',
|
|
12
|
+
max: '{path} is greater than ({n})',
|
|
13
13
|
telephone: '{path} is not a valid phone number',
|
|
14
14
|
url: '{path} is not a valid url',
|
|
15
15
|
simpleName: '{path} is not a proper name',
|
|
@@ -112,32 +112,13 @@ export class SchemaValidator {
|
|
|
112
112
|
* @param value The value to validate
|
|
113
113
|
*/
|
|
114
114
|
static #validateRange(field: FieldConfig, key: 'min' | 'max', value: string | number | Date): boolean {
|
|
115
|
-
|
|
116
115
|
const f = field[key]!;
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
value = new Date(value);
|
|
124
|
-
}
|
|
125
|
-
const valN = typeof value === 'number' ? value : value.getTime();
|
|
126
|
-
if (key === 'min' && valN < fn || key === 'max' && valN > fn) {
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
} else {
|
|
130
|
-
const date = fn.getTime();
|
|
131
|
-
if (typeof value === 'string') {
|
|
132
|
-
value = Date.parse(value);
|
|
133
|
-
} else if (value instanceof Date) {
|
|
134
|
-
value = value.getTime();
|
|
135
|
-
}
|
|
136
|
-
if (key === 'min' && value < date || key === 'max' && value > date) {
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return false;
|
|
116
|
+
const valueNum = (typeof value === 'string') ?
|
|
117
|
+
(field.type === Date ? Date.parse(value) : parseInt(value, 10)) :
|
|
118
|
+
(value instanceof Date ? value.getTime() : value);
|
|
119
|
+
|
|
120
|
+
const boundary = (typeof f.n === 'number' ? f.n : f.n.getTime());
|
|
121
|
+
return key === 'min' ? valueNum < boundary : valueNum > boundary;
|
|
141
122
|
}
|
|
142
123
|
|
|
143
124
|
/**
|
|
@@ -314,9 +295,9 @@ export class SchemaValidator {
|
|
|
314
295
|
await this.validate(cls, o, view);
|
|
315
296
|
} catch (err) {
|
|
316
297
|
if (err instanceof ValidationResultError) { // Don't check required fields
|
|
317
|
-
const errs = err.details
|
|
298
|
+
const errs = err.details.errors.filter(x => x.kind !== 'required');
|
|
318
299
|
if (errs.length) {
|
|
319
|
-
err.details
|
|
300
|
+
err.details.errors = errs;
|
|
320
301
|
throw err;
|
|
321
302
|
}
|
|
322
303
|
}
|