@aeriajs/validation 0.0.122 → 0.0.124
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/dist/validate.d.ts +1 -0
- package/dist/validate.js +30 -28
- package/dist/validate.mjs +28 -26
- package/package.json +1 -1
package/dist/validate.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { JsonSchema, Property, InferSchema, Description, PropertyValidation
|
|
|
2
2
|
import { Result } from '@aeriajs/types';
|
|
3
3
|
import { ValidationErrorCode } from '@aeriajs/types';
|
|
4
4
|
export type ValidateOptions = {
|
|
5
|
+
tolerateExtraneous?: boolean;
|
|
5
6
|
filterOutExtraneous?: boolean;
|
|
6
7
|
throwOnError?: boolean;
|
|
7
8
|
coerce?: boolean;
|
package/dist/validate.js
CHANGED
|
@@ -10,24 +10,24 @@ const getValueType = (value) => {
|
|
|
10
10
|
: typeof value;
|
|
11
11
|
};
|
|
12
12
|
const getPropertyType = (property) => {
|
|
13
|
-
if ('
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
if ('type' in property) {
|
|
14
|
+
if ('format' in property && property.format) {
|
|
15
|
+
if ([
|
|
16
|
+
'date',
|
|
17
|
+
'date-time',
|
|
18
|
+
].includes(property.format)) {
|
|
19
|
+
return 'datetime';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return property.type;
|
|
17
23
|
}
|
|
18
24
|
if ('enum' in property) {
|
|
19
25
|
return typeof property.enum[0];
|
|
20
26
|
}
|
|
21
|
-
if ('
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
].includes(property.format)) {
|
|
26
|
-
return 'datetime';
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
if ('type' in property) {
|
|
30
|
-
return property.type;
|
|
27
|
+
if ('$ref' in property
|
|
28
|
+
|| 'properties' in property
|
|
29
|
+
|| 'additionalProperties' in property) {
|
|
30
|
+
return 'object';
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
const makePropertyError = (type, details) => {
|
|
@@ -41,24 +41,20 @@ const makeValidationError = (error) => {
|
|
|
41
41
|
};
|
|
42
42
|
exports.makeValidationError = makeValidationError;
|
|
43
43
|
const validateProperty = (propName, what, property, options = {}) => {
|
|
44
|
-
const { filterOutExtraneous, coerce } = options;
|
|
45
|
-
if (what === null || what === undefined) {
|
|
46
|
-
return types_1.Result.result(what);
|
|
47
|
-
}
|
|
48
44
|
if (!property) {
|
|
49
45
|
if (options.parentProperty && 'additionalProperties' in options.parentProperty && options.parentProperty.additionalProperties) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (filterOutExtraneous) {
|
|
53
|
-
return types_1.Result.result(undefined);
|
|
54
|
-
}
|
|
46
|
+
if (options.filterOutExtraneous) {
|
|
47
|
+
return types_1.Result.result(undefined);
|
|
55
48
|
}
|
|
56
49
|
return types_1.Result.result(what);
|
|
57
50
|
}
|
|
51
|
+
if (options.tolerateExtraneous) {
|
|
52
|
+
return types_1.Result.result(undefined);
|
|
53
|
+
}
|
|
58
54
|
return types_1.Result.error(makePropertyError(types_2.PropertyValidationErrorCode.Extraneous));
|
|
59
55
|
}
|
|
60
|
-
if (
|
|
61
|
-
return types_1.Result.result(
|
|
56
|
+
if (what === null || what === undefined) {
|
|
57
|
+
return types_1.Result.result(what);
|
|
62
58
|
}
|
|
63
59
|
if ('properties' in property) {
|
|
64
60
|
return (0, exports.validate)(what, property, options);
|
|
@@ -72,11 +68,14 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
72
68
|
}
|
|
73
69
|
return types_1.Result.result(what);
|
|
74
70
|
}
|
|
75
|
-
const expectedType = getPropertyType(property);
|
|
76
|
-
const actualType = getValueType(what);
|
|
77
71
|
if ('enum' in property && property.enum.length === 0) {
|
|
78
72
|
return types_1.Result.result(what);
|
|
79
73
|
}
|
|
74
|
+
if ('getter' in property) {
|
|
75
|
+
return types_1.Result.result(undefined);
|
|
76
|
+
}
|
|
77
|
+
const expectedType = getPropertyType(property);
|
|
78
|
+
const actualType = getValueType(what);
|
|
80
79
|
if (actualType !== expectedType
|
|
81
80
|
&& !('items' in property && actualType === 'array')
|
|
82
81
|
&& !(actualType === 'number' && expectedType === 'integer')) {
|
|
@@ -88,7 +87,7 @@ const validateProperty = (propName, what, property, options = {}) => {
|
|
|
88
87
|
return types_1.Result.result(what);
|
|
89
88
|
}
|
|
90
89
|
}
|
|
91
|
-
if (coerce) {
|
|
90
|
+
if (options.coerce) {
|
|
92
91
|
if (expectedType === 'number' && typeof what === 'string') {
|
|
93
92
|
const coerced = parseFloat(what);
|
|
94
93
|
if (!isNaN(coerced)) {
|
|
@@ -212,6 +211,9 @@ const validate = (what, schema, options = {}) => {
|
|
|
212
211
|
}
|
|
213
212
|
const wholenessError = (0, exports.validateWholeness)(what, schema);
|
|
214
213
|
if (wholenessError) {
|
|
214
|
+
if (options.throwOnError) {
|
|
215
|
+
throw new Error(types_2.ValidationErrorCode.MissingProperties);
|
|
216
|
+
}
|
|
215
217
|
return types_1.Result.error(wholenessError);
|
|
216
218
|
}
|
|
217
219
|
const errors = {};
|
package/dist/validate.mjs
CHANGED
|
@@ -6,22 +6,22 @@ const getValueType = (value) => {
|
|
|
6
6
|
return Array.isArray(value) ? "array" : typeof value;
|
|
7
7
|
};
|
|
8
8
|
const getPropertyType = (property) => {
|
|
9
|
-
if ("
|
|
10
|
-
|
|
9
|
+
if ("type" in property) {
|
|
10
|
+
if ("format" in property && property.format) {
|
|
11
|
+
if ([
|
|
12
|
+
"date",
|
|
13
|
+
"date-time"
|
|
14
|
+
].includes(property.format)) {
|
|
15
|
+
return "datetime";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return property.type;
|
|
11
19
|
}
|
|
12
20
|
if ("enum" in property) {
|
|
13
21
|
return typeof property.enum[0];
|
|
14
22
|
}
|
|
15
|
-
if ("
|
|
16
|
-
|
|
17
|
-
"date",
|
|
18
|
-
"date-time"
|
|
19
|
-
].includes(property.format)) {
|
|
20
|
-
return "datetime";
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
if ("type" in property) {
|
|
24
|
-
return property.type;
|
|
23
|
+
if ("$ref" in property || "properties" in property || "additionalProperties" in property) {
|
|
24
|
+
return "object";
|
|
25
25
|
}
|
|
26
26
|
};
|
|
27
27
|
const makePropertyError = (type, details) => {
|
|
@@ -34,24 +34,20 @@ export const makeValidationError = (error) => {
|
|
|
34
34
|
return error;
|
|
35
35
|
};
|
|
36
36
|
export const validateProperty = (propName, what, property, options = {}) => {
|
|
37
|
-
const { filterOutExtraneous, coerce } = options;
|
|
38
|
-
if (what === null || what === void 0) {
|
|
39
|
-
return Result.result(what);
|
|
40
|
-
}
|
|
41
37
|
if (!property) {
|
|
42
38
|
if (options.parentProperty && "additionalProperties" in options.parentProperty && options.parentProperty.additionalProperties) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (filterOutExtraneous) {
|
|
46
|
-
return Result.result(void 0);
|
|
47
|
-
}
|
|
39
|
+
if (options.filterOutExtraneous) {
|
|
40
|
+
return Result.result(void 0);
|
|
48
41
|
}
|
|
49
42
|
return Result.result(what);
|
|
50
43
|
}
|
|
44
|
+
if (options.tolerateExtraneous) {
|
|
45
|
+
return Result.result(void 0);
|
|
46
|
+
}
|
|
51
47
|
return Result.error(makePropertyError(PropertyValidationErrorCode.Extraneous));
|
|
52
48
|
}
|
|
53
|
-
if (
|
|
54
|
-
return Result.result(
|
|
49
|
+
if (what === null || what === void 0) {
|
|
50
|
+
return Result.result(what);
|
|
55
51
|
}
|
|
56
52
|
if ("properties" in property) {
|
|
57
53
|
return validate(what, property, options);
|
|
@@ -65,11 +61,14 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
65
61
|
}
|
|
66
62
|
return Result.result(what);
|
|
67
63
|
}
|
|
68
|
-
const expectedType = getPropertyType(property);
|
|
69
|
-
const actualType = getValueType(what);
|
|
70
64
|
if ("enum" in property && property.enum.length === 0) {
|
|
71
65
|
return Result.result(what);
|
|
72
66
|
}
|
|
67
|
+
if ("getter" in property) {
|
|
68
|
+
return Result.result(void 0);
|
|
69
|
+
}
|
|
70
|
+
const expectedType = getPropertyType(property);
|
|
71
|
+
const actualType = getValueType(what);
|
|
73
72
|
if (actualType !== expectedType && !("items" in property && actualType === "array") && !(actualType === "number" && expectedType === "integer")) {
|
|
74
73
|
if (expectedType === "datetime" && what instanceof Date) {
|
|
75
74
|
return Result.result(what);
|
|
@@ -79,7 +78,7 @@ export const validateProperty = (propName, what, property, options = {}) => {
|
|
|
79
78
|
return Result.result(what);
|
|
80
79
|
}
|
|
81
80
|
}
|
|
82
|
-
if (coerce) {
|
|
81
|
+
if (options.coerce) {
|
|
83
82
|
if (expectedType === "number" && typeof what === "string") {
|
|
84
83
|
const coerced = parseFloat(what);
|
|
85
84
|
if (!isNaN(coerced)) {
|
|
@@ -191,6 +190,9 @@ export const validate = (what, schema, options = {}) => {
|
|
|
191
190
|
}
|
|
192
191
|
const wholenessError = validateWholeness(what, schema);
|
|
193
192
|
if (wholenessError) {
|
|
193
|
+
if (options.throwOnError) {
|
|
194
|
+
throw new Error(ValidationErrorCode.MissingProperties);
|
|
195
|
+
}
|
|
194
196
|
return Result.error(wholenessError);
|
|
195
197
|
}
|
|
196
198
|
const errors = {};
|