valgen 5.19.2 → 5.19.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/constants.js +1 -1
- package/index.js +6 -2
- package/package.json +1 -1
- package/rules/type-rules/is-enum.d.ts +2 -2
- package/rules/type-rules/is-enum.js +9 -0
package/constants.js
CHANGED
package/index.js
CHANGED
|
@@ -58,10 +58,14 @@ const toBoolean = vg.isBoolean({ coerce: true });
|
|
|
58
58
|
const toDate = vg.isDate({ coerce: true });
|
|
59
59
|
const toDateStringValidators = new Map();
|
|
60
60
|
const toDateString = (input, options, ctx) => {
|
|
61
|
-
const trim = options?.trim ?? '
|
|
61
|
+
const trim = options?.trim ?? 'ms';
|
|
62
62
|
let validator = toDateStringValidators.get(trim);
|
|
63
63
|
if (!validator) {
|
|
64
|
-
validator = vg.isDateString({
|
|
64
|
+
validator = vg.isDateString({
|
|
65
|
+
coerce: true,
|
|
66
|
+
precisionMax: trim,
|
|
67
|
+
trim: true,
|
|
68
|
+
});
|
|
65
69
|
toDateStringValidators.set(trim, validator);
|
|
66
70
|
}
|
|
67
71
|
return validator(input, options, ctx);
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@ import { type ValidationOptions, type Validator } from '../../core/index.js';
|
|
|
3
3
|
* Validates if given value is one of enum values.
|
|
4
4
|
* @validator isEnum
|
|
5
5
|
*/
|
|
6
|
-
export declare function isEnum<T1
|
|
6
|
+
export declare function isEnum<T1>(values: any, options?: ValidationOptions & {
|
|
7
7
|
caseInSensitive?: boolean;
|
|
8
8
|
enumName?: string;
|
|
9
|
-
}): Validator<T1,
|
|
9
|
+
}): Validator<T1, any>;
|
|
@@ -7,6 +7,15 @@ export function isEnum(values, options) {
|
|
|
7
7
|
const caseInSensitive = !!options?.caseInSensitive;
|
|
8
8
|
const enumName = options?.enumName;
|
|
9
9
|
// Prepare an object for fast lookup
|
|
10
|
+
if (values && typeof values === 'object' && !Array.isArray(values)) {
|
|
11
|
+
const keys = Object.keys(values).filter(k => !/^\d+$/.test(k));
|
|
12
|
+
values = keys.reduce((a, k) => {
|
|
13
|
+
if (values[k] != null)
|
|
14
|
+
a.push(values[k]);
|
|
15
|
+
return a;
|
|
16
|
+
// v => !(typeof v === 'number' && !keys.includes(String(v))),
|
|
17
|
+
}, []);
|
|
18
|
+
}
|
|
10
19
|
const valObj = (Array.isArray(values) ? values : [values]).reduce((a, v) => {
|
|
11
20
|
if (typeof v === 'string' && caseInSensitive)
|
|
12
21
|
a[v.toUpperCase()] = true;
|