valgen 4.0.0-alpha.3 → 4.0.0-alpha.4
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/cjs/rules/is-date.js +7 -2
- package/cjs/rules/is-object.js +12 -20
- package/esm/rules/is-date.js +7 -2
- package/esm/rules/is-object.js +12 -20
- package/package.json +1 -1
- package/typings/rules/is-object.d.ts +1 -1
package/cjs/rules/is-date.js
CHANGED
|
@@ -46,10 +46,15 @@ function isDateString(options) {
|
|
|
46
46
|
const inputFormat = options?.format;
|
|
47
47
|
const coerceFormat = Array.isArray(options?.format) ? options?.format[0] : options?.format;
|
|
48
48
|
return (0, index_js_1.validator)('isDateString', function (input, context) {
|
|
49
|
-
if (
|
|
49
|
+
if (input instanceof Date) {
|
|
50
|
+
const d = (0, dayjs_1.default)(input);
|
|
51
|
+
if (d.isValid())
|
|
52
|
+
return coerceFormat ? d.format(coerceFormat) : d.toISOString();
|
|
53
|
+
}
|
|
54
|
+
else if (typeof input === 'string') {
|
|
50
55
|
const d = (0, dayjs_1.default)(input, inputFormat);
|
|
51
56
|
if (d.isValid()) {
|
|
52
|
-
if (context.coerce && options?.format
|
|
57
|
+
if (context.coerce && options?.format)
|
|
53
58
|
return coerceFormat ? d.format(coerceFormat) : d.toISOString();
|
|
54
59
|
return input;
|
|
55
60
|
}
|
package/cjs/rules/is-object.js
CHANGED
|
@@ -13,13 +13,6 @@ function isObject(schema, options) {
|
|
|
13
13
|
const additionalFields = options?.additionalFields ?? false;
|
|
14
14
|
const caseInSensitive = !!options?.caseInSensitive;
|
|
15
15
|
const detectCircular = !!options?.detectCircular;
|
|
16
|
-
if (options) {
|
|
17
|
-
delete options.name;
|
|
18
|
-
delete options.ctor;
|
|
19
|
-
delete options.additionalFields;
|
|
20
|
-
delete options.caseInSensitive;
|
|
21
|
-
delete options.detectCircular;
|
|
22
|
-
}
|
|
23
16
|
const propertyRules = {};
|
|
24
17
|
const propertyOptions = {};
|
|
25
18
|
Object.keys(schema).forEach(k => {
|
|
@@ -38,21 +31,12 @@ function isObject(schema, options) {
|
|
|
38
31
|
else
|
|
39
32
|
throw new TypeError(`Invalid definition in validation schema (${k})`);
|
|
40
33
|
});
|
|
41
|
-
const schemaKeys = Object.keys(propertyRules);
|
|
42
34
|
const _rule = (0, index_js_1.validator)('isObject', function (input, context) {
|
|
43
35
|
if (!(input && typeof input === 'object')) {
|
|
44
36
|
context.failure(`{{label}} must be an object`);
|
|
45
37
|
return;
|
|
46
38
|
}
|
|
47
|
-
const
|
|
48
|
-
.reduce((a, k) => {
|
|
49
|
-
if (caseInSensitive)
|
|
50
|
-
a[k.toLowerCase()] = k;
|
|
51
|
-
else
|
|
52
|
-
a[k] = k;
|
|
53
|
-
return a;
|
|
54
|
-
}, {});
|
|
55
|
-
const keys = Object.keys(keyMap);
|
|
39
|
+
const keys = [...Object.keys(input), ...Object.keys(schema)];
|
|
56
40
|
const l = keys.length;
|
|
57
41
|
let i = 0;
|
|
58
42
|
let inputKey;
|
|
@@ -70,14 +54,18 @@ function isObject(schema, options) {
|
|
|
70
54
|
}
|
|
71
55
|
const rootName = context.root?.input.context;
|
|
72
56
|
const location = context.input.location || '';
|
|
57
|
+
const processedSchemaKeys = {};
|
|
73
58
|
// Iterate object keys and perform rules
|
|
74
59
|
for (i = 0; i < l; i++) {
|
|
75
60
|
inputKey = keys[i];
|
|
76
|
-
schemaKey =
|
|
61
|
+
schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
|
|
77
62
|
v = input[inputKey];
|
|
78
63
|
_propRule = propertyRules[schemaKey] ||
|
|
79
64
|
((0, index_js_1.isValidator)(additionalFields) ? additionalFields : undefined);
|
|
80
65
|
if (_propRule) {
|
|
66
|
+
if (processedSchemaKeys[schemaKey])
|
|
67
|
+
continue;
|
|
68
|
+
processedSchemaKeys[schemaKey] = true;
|
|
81
69
|
const subCtx = context.extend(_propRule);
|
|
82
70
|
if (ctorName) {
|
|
83
71
|
if (rootName && rootName !== ctorName)
|
|
@@ -90,8 +78,12 @@ function isObject(schema, options) {
|
|
|
90
78
|
subCtx.input.property = schemaKey;
|
|
91
79
|
v = _propRule[index_js_1.kValidatorFn](v, subCtx, _propRule);
|
|
92
80
|
}
|
|
93
|
-
else if (v !== undefined
|
|
94
|
-
|
|
81
|
+
else if (v !== undefined) {
|
|
82
|
+
if (additionalFields === 'ignore')
|
|
83
|
+
continue;
|
|
84
|
+
if (!additionalFields)
|
|
85
|
+
context.failure(`${ctorName || 'Object'} has no field '${inputKey}' and does not accept additional fields`);
|
|
86
|
+
}
|
|
95
87
|
if (v !== undefined)
|
|
96
88
|
out[propertyOptions[schemaKey]?.as || schemaKey] = v;
|
|
97
89
|
}
|
package/esm/rules/is-date.js
CHANGED
|
@@ -39,10 +39,15 @@ export function isDateString(options) {
|
|
|
39
39
|
const inputFormat = options?.format;
|
|
40
40
|
const coerceFormat = Array.isArray(options?.format) ? options?.format[0] : options?.format;
|
|
41
41
|
return validator('isDateString', function (input, context) {
|
|
42
|
-
if (
|
|
42
|
+
if (input instanceof Date) {
|
|
43
|
+
const d = dayjs(input);
|
|
44
|
+
if (d.isValid())
|
|
45
|
+
return coerceFormat ? d.format(coerceFormat) : d.toISOString();
|
|
46
|
+
}
|
|
47
|
+
else if (typeof input === 'string') {
|
|
43
48
|
const d = dayjs(input, inputFormat);
|
|
44
49
|
if (d.isValid()) {
|
|
45
|
-
if (context.coerce && options?.format
|
|
50
|
+
if (context.coerce && options?.format)
|
|
46
51
|
return coerceFormat ? d.format(coerceFormat) : d.toISOString();
|
|
47
52
|
return input;
|
|
48
53
|
}
|
package/esm/rules/is-object.js
CHANGED
|
@@ -10,13 +10,6 @@ export function isObject(schema, options) {
|
|
|
10
10
|
const additionalFields = options?.additionalFields ?? false;
|
|
11
11
|
const caseInSensitive = !!options?.caseInSensitive;
|
|
12
12
|
const detectCircular = !!options?.detectCircular;
|
|
13
|
-
if (options) {
|
|
14
|
-
delete options.name;
|
|
15
|
-
delete options.ctor;
|
|
16
|
-
delete options.additionalFields;
|
|
17
|
-
delete options.caseInSensitive;
|
|
18
|
-
delete options.detectCircular;
|
|
19
|
-
}
|
|
20
13
|
const propertyRules = {};
|
|
21
14
|
const propertyOptions = {};
|
|
22
15
|
Object.keys(schema).forEach(k => {
|
|
@@ -35,21 +28,12 @@ export function isObject(schema, options) {
|
|
|
35
28
|
else
|
|
36
29
|
throw new TypeError(`Invalid definition in validation schema (${k})`);
|
|
37
30
|
});
|
|
38
|
-
const schemaKeys = Object.keys(propertyRules);
|
|
39
31
|
const _rule = validator('isObject', function (input, context) {
|
|
40
32
|
if (!(input && typeof input === 'object')) {
|
|
41
33
|
context.failure(`{{label}} must be an object`);
|
|
42
34
|
return;
|
|
43
35
|
}
|
|
44
|
-
const
|
|
45
|
-
.reduce((a, k) => {
|
|
46
|
-
if (caseInSensitive)
|
|
47
|
-
a[k.toLowerCase()] = k;
|
|
48
|
-
else
|
|
49
|
-
a[k] = k;
|
|
50
|
-
return a;
|
|
51
|
-
}, {});
|
|
52
|
-
const keys = Object.keys(keyMap);
|
|
36
|
+
const keys = [...Object.keys(input), ...Object.keys(schema)];
|
|
53
37
|
const l = keys.length;
|
|
54
38
|
let i = 0;
|
|
55
39
|
let inputKey;
|
|
@@ -67,14 +51,18 @@ export function isObject(schema, options) {
|
|
|
67
51
|
}
|
|
68
52
|
const rootName = context.root?.input.context;
|
|
69
53
|
const location = context.input.location || '';
|
|
54
|
+
const processedSchemaKeys = {};
|
|
70
55
|
// Iterate object keys and perform rules
|
|
71
56
|
for (i = 0; i < l; i++) {
|
|
72
57
|
inputKey = keys[i];
|
|
73
|
-
schemaKey =
|
|
58
|
+
schemaKey = caseInSensitive ? inputKey.toLowerCase() : inputKey;
|
|
74
59
|
v = input[inputKey];
|
|
75
60
|
_propRule = propertyRules[schemaKey] ||
|
|
76
61
|
(isValidator(additionalFields) ? additionalFields : undefined);
|
|
77
62
|
if (_propRule) {
|
|
63
|
+
if (processedSchemaKeys[schemaKey])
|
|
64
|
+
continue;
|
|
65
|
+
processedSchemaKeys[schemaKey] = true;
|
|
78
66
|
const subCtx = context.extend(_propRule);
|
|
79
67
|
if (ctorName) {
|
|
80
68
|
if (rootName && rootName !== ctorName)
|
|
@@ -87,8 +75,12 @@ export function isObject(schema, options) {
|
|
|
87
75
|
subCtx.input.property = schemaKey;
|
|
88
76
|
v = _propRule[kValidatorFn](v, subCtx, _propRule);
|
|
89
77
|
}
|
|
90
|
-
else if (v !== undefined
|
|
91
|
-
|
|
78
|
+
else if (v !== undefined) {
|
|
79
|
+
if (additionalFields === 'ignore')
|
|
80
|
+
continue;
|
|
81
|
+
if (!additionalFields)
|
|
82
|
+
context.failure(`${ctorName || 'Object'} has no field '${inputKey}' and does not accept additional fields`);
|
|
83
|
+
}
|
|
92
84
|
if (v !== undefined)
|
|
93
85
|
out[propertyOptions[schemaKey]?.as || schemaKey] = v;
|
|
94
86
|
}
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@ export type ObjectSchema = Record<string | number, Validator<any, any> | [Valida
|
|
|
7
7
|
export interface IsObjectOptions<T> extends ValidationOptions {
|
|
8
8
|
name?: string;
|
|
9
9
|
ctor?: Type<T>;
|
|
10
|
-
additionalFields?: boolean | Validator<any, any
|
|
10
|
+
additionalFields?: boolean | Validator<any, any> | 'ignore';
|
|
11
11
|
caseInSensitive?: boolean;
|
|
12
12
|
detectCircular?: boolean;
|
|
13
13
|
}
|