@oxog/vld 2.1.0 → 2.2.5
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/CHANGELOG.md +87 -0
- package/README.md +1296 -1235
- package/dist/chunks/errors-core-Cqi4-9ew.js +1 -0
- package/dist/chunks/index-CkbfQQ4n.js +1 -0
- package/dist/chunks/index-yjpbvFgw.js +1 -0
- package/dist/chunks/ip-validation-g5LWgy9t.js +1 -0
- package/dist/chunks/security-Dy24T8N7.js +1 -0
- package/dist/cjs/chunks/errors-core-DKPohm9I.cjs +1 -0
- package/dist/cjs/chunks/index-BEV9nG0N.cjs +1 -0
- package/dist/cjs/chunks/index-ZjHAxYdA.cjs +1 -0
- package/dist/cjs/chunks/ip-validation-BZqPamPC.cjs +1 -0
- package/dist/cjs/chunks/security-aXZVI7qW.cjs +1 -0
- package/dist/cjs/cli/bin.cjs +1 -505
- package/dist/cjs/cli/index.cjs +1 -209
- package/dist/cjs/codecs/index.cjs +1 -45
- package/dist/cjs/coercion/index.cjs +1 -588
- package/dist/cjs/compat/emitter.cjs +1 -184
- package/dist/cjs/compat/result.cjs +1 -189
- package/dist/cjs/errors.cjs +1 -180
- package/dist/cjs/index.cjs +1 -1894
- package/dist/cjs/kernel.cjs +1 -415
- package/dist/cjs/locales/af.cjs +1 -108
- package/dist/cjs/locales/ar.cjs +1 -108
- package/dist/cjs/locales/bn.cjs +1 -108
- package/dist/cjs/locales/da.cjs +1 -108
- package/dist/cjs/locales/de.cjs +1 -108
- package/dist/cjs/locales/en.cjs +1 -108
- package/dist/cjs/locales/es-MX.cjs +1 -107
- package/dist/cjs/locales/es.cjs +1 -96
- package/dist/cjs/locales/fi.cjs +1 -108
- package/dist/cjs/locales/fr.cjs +1 -96
- package/dist/cjs/locales/hi.cjs +1 -108
- package/dist/cjs/locales/id.cjs +1 -108
- package/dist/cjs/locales/index.cjs +1 -43
- package/dist/cjs/locales/it.cjs +1 -108
- package/dist/cjs/locales/ja.cjs +1 -108
- package/dist/cjs/locales/ko.cjs +1 -108
- package/dist/cjs/locales/lazy.cjs +1 -188
- package/dist/cjs/locales/nl.cjs +1 -108
- package/dist/cjs/locales/no.cjs +1 -108
- package/dist/cjs/locales/pl.cjs +1 -108
- package/dist/cjs/locales/pt-BR.cjs +1 -108
- package/dist/cjs/locales/pt.cjs +1 -108
- package/dist/cjs/locales/ru.cjs +1 -108
- package/dist/cjs/locales/runtime.cjs +1 -51
- package/dist/cjs/locales/sv.cjs +1 -108
- package/dist/cjs/locales/sw.cjs +1 -108
- package/dist/cjs/locales/th.cjs +1 -108
- package/dist/cjs/locales/tr.cjs +1 -108
- package/dist/cjs/locales/vi.cjs +1 -108
- package/dist/cjs/locales/zh.cjs +1 -108
- package/dist/cjs/mini.cjs +1 -242
- package/dist/cjs/pigment.cjs +1 -207
- package/dist/cjs/plugins/index.cjs +1 -12
- package/dist/cjs/registry.cjs +1 -37
- package/dist/cjs/v3/index.cjs +1 -425
- package/dist/cjs/v4/core/index.cjs +1 -894
- package/dist/cjs/v4/index.cjs +1 -425
- package/dist/cjs/v4/locales/index.cjs +1 -130
- package/dist/cjs/v4/mini/index.cjs +1 -509
- package/dist/cjs/v4-mini/index.cjs +1 -532
- package/dist/cjs/validators/any.cjs +1 -40
- package/dist/cjs/validators/array.cjs +1 -283
- package/dist/cjs/validators/base.cjs +1 -887
- package/dist/cjs/validators/base64.cjs +1 -73
- package/dist/cjs/validators/bigint.cjs +1 -232
- package/dist/cjs/validators/boolean.cjs +1 -146
- package/dist/cjs/validators/codec.cjs +1 -205
- package/dist/cjs/validators/custom.cjs +1 -71
- package/dist/cjs/validators/date.cjs +1 -278
- package/dist/cjs/validators/discriminated-union.cjs +1 -130
- package/dist/cjs/validators/enum.cjs +1 -154
- package/dist/cjs/validators/file.cjs +1 -130
- package/dist/cjs/validators/function.cjs +1 -63
- package/dist/cjs/validators/hex.cjs +1 -70
- package/dist/cjs/validators/index.cjs +1 -123
- package/dist/cjs/validators/intersection.cjs +1 -120
- package/dist/cjs/validators/json.cjs +1 -70
- package/dist/cjs/validators/lazy.cjs +1 -53
- package/dist/cjs/validators/literal.cjs +1 -64
- package/dist/cjs/validators/map.cjs +1 -218
- package/dist/cjs/validators/nan.cjs +1 -41
- package/dist/cjs/validators/never.cjs +1 -36
- package/dist/cjs/validators/null.cjs +1 -43
- package/dist/cjs/validators/number.cjs +1 -427
- package/dist/cjs/validators/object.cjs +1 -868
- package/dist/cjs/validators/promise.cjs +1 -115
- package/dist/cjs/validators/record.cjs +1 -262
- package/dist/cjs/validators/set.cjs +1 -203
- package/dist/cjs/validators/string-bool.cjs +1 -124
- package/dist/cjs/validators/string-formats.cjs +1 -168
- package/dist/cjs/validators/string.cjs +1 -12
- package/dist/cjs/validators/symbol.cjs +1 -62
- package/dist/cjs/validators/template-literal.cjs +1 -108
- package/dist/cjs/validators/tuple.cjs +1 -187
- package/dist/cjs/validators/uint8array.cjs +1 -106
- package/dist/cjs/validators/undefined.cjs +1 -43
- package/dist/cjs/validators/union.cjs +1 -191
- package/dist/cjs/validators/unknown.cjs +1 -40
- package/dist/cjs/validators/void.cjs +1 -48
- package/dist/cjs/validators/xor.cjs +1 -69
- package/dist/cli/bin.js +1 -406
- package/dist/cli/index.js +1 -205
- package/dist/codecs/index.js +1 -11
- package/dist/coercion/index.js +1 -579
- package/dist/compat/emitter.js +1 -180
- package/dist/compat/result.js +1 -170
- package/dist/errors-core.d.ts +20 -1
- package/dist/errors.js +1 -171
- package/dist/index.d.ts +248 -357
- package/dist/index.js +1 -1534
- package/dist/kernel.js +1 -409
- package/dist/locales/af.js +1 -106
- package/dist/locales/ar.js +1 -106
- package/dist/locales/bn.js +1 -106
- package/dist/locales/da.js +1 -106
- package/dist/locales/de.js +1 -106
- package/dist/locales/en.js +1 -106
- package/dist/locales/es-MX.js +1 -105
- package/dist/locales/es.js +1 -94
- package/dist/locales/fi.js +1 -106
- package/dist/locales/fr.js +1 -94
- package/dist/locales/hi.js +1 -106
- package/dist/locales/id.js +1 -106
- package/dist/locales/index.js +1 -29
- package/dist/locales/it.js +1 -106
- package/dist/locales/ja.js +1 -106
- package/dist/locales/ko.js +1 -106
- package/dist/locales/lazy.js +1 -177
- package/dist/locales/nl.js +1 -106
- package/dist/locales/no.js +1 -106
- package/dist/locales/pl.js +1 -106
- package/dist/locales/pt-BR.js +1 -106
- package/dist/locales/pt.js +1 -106
- package/dist/locales/ru.js +1 -106
- package/dist/locales/runtime.js +1 -41
- package/dist/locales/sv.js +1 -106
- package/dist/locales/sw.js +1 -106
- package/dist/locales/th.js +1 -106
- package/dist/locales/tr.js +1 -106
- package/dist/locales/vi.js +1 -106
- package/dist/locales/zh.js +1 -106
- package/dist/mini.d.ts +3 -3
- package/dist/mini.js +1 -156
- package/dist/pigment.js +1 -180
- package/dist/plugins/index.js +1 -1
- package/dist/registry.js +1 -34
- package/dist/utils/json-schema.d.ts +10 -1
- package/dist/v3/index.js +1 -46
- package/dist/v4/core/index.d.ts +28 -16
- package/dist/v4/core/index.js +1 -277
- package/dist/v4/index.js +1 -50
- package/dist/v4/locales/index.d.ts +4 -0
- package/dist/v4/locales/index.js +1 -77
- package/dist/v4/mini/index.js +1 -47
- package/dist/v4-mini/index.d.ts +1 -1
- package/dist/v4-mini/index.js +1 -75
- package/dist/validators/any.js +1 -37
- package/dist/validators/array.d.ts +7 -0
- package/dist/validators/array.js +1 -279
- package/dist/validators/base.d.ts +63 -8
- package/dist/validators/base.js +1 -868
- package/dist/validators/base64.js +1 -68
- package/dist/validators/bigint.js +1 -228
- package/dist/validators/boolean.js +1 -142
- package/dist/validators/codec.js +1 -200
- package/dist/validators/custom.js +1 -67
- package/dist/validators/date.js +1 -274
- package/dist/validators/discriminated-union.d.ts +2 -2
- package/dist/validators/discriminated-union.js +1 -123
- package/dist/validators/enum.js +1 -150
- package/dist/validators/file.js +1 -125
- package/dist/validators/function.js +1 -58
- package/dist/validators/hex.js +1 -65
- package/dist/validators/index.d.ts +1 -1
- package/dist/validators/index.js +1 -35
- package/dist/validators/intersection.js +1 -116
- package/dist/validators/json.js +1 -66
- package/dist/validators/lazy.js +1 -50
- package/dist/validators/literal.d.ts +7 -3
- package/dist/validators/literal.js +1 -60
- package/dist/validators/map.js +1 -214
- package/dist/validators/nan.js +1 -38
- package/dist/validators/never.js +1 -32
- package/dist/validators/null.js +1 -40
- package/dist/validators/number.d.ts +20 -1
- package/dist/validators/number.js +1 -423
- package/dist/validators/object.d.ts +12 -2
- package/dist/validators/object.js +1 -864
- package/dist/validators/promise.js +1 -111
- package/dist/validators/record.d.ts +11 -8
- package/dist/validators/record.js +1 -258
- package/dist/validators/set.js +1 -199
- package/dist/validators/string-bool.js +1 -120
- package/dist/validators/string-formats.d.ts +58 -62
- package/dist/validators/string-formats.js +1 -135
- package/dist/validators/string.d.ts +47 -10
- package/dist/validators/string.js +1 -4
- package/dist/validators/symbol.js +1 -58
- package/dist/validators/template-literal.js +1 -103
- package/dist/validators/tuple.d.ts +17 -13
- package/dist/validators/tuple.js +1 -183
- package/dist/validators/uint8array.js +1 -101
- package/dist/validators/undefined.js +1 -40
- package/dist/validators/union.js +1 -187
- package/dist/validators/unknown.js +1 -37
- package/dist/validators/void.js +1 -44
- package/dist/validators/xor.d.ts +2 -2
- package/dist/validators/xor.js +1 -66
- package/package.json +239 -238
- package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
- package/dist/chunks/index-0R2ntSud.js +0 -508
- package/dist/chunks/index-BvH403Yy.js +0 -88
- package/dist/chunks/security-B0Pd_vSY.js +0 -35
- package/dist/chunks/string-BJRQod3t.js +0 -530
- package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
- package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
- package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
- package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
- package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
|
@@ -1,67 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Immutable custom validator for user-defined validation logic
|
|
5
|
-
* Provides type safety while allowing complete control over validation
|
|
6
|
-
*/
|
|
7
|
-
class VldCustom extends VldBase {
|
|
8
|
-
constructor(options) {
|
|
9
|
-
super(VLD_VALIDATOR_TYPES.CUSTOM);
|
|
10
|
-
this._parseFn = options.parse;
|
|
11
|
-
this._parseAsyncFn = options.parseAsync || ((value) => Promise.resolve(this._parseFn(value)));
|
|
12
|
-
this._safeParseFn = options.safeParse || ((value) => {
|
|
13
|
-
try {
|
|
14
|
-
return { success: true, data: this._parseFn(value) };
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
return { success: false, error: error };
|
|
18
|
-
}
|
|
19
|
-
});
|
|
20
|
-
this._safeParseAsyncFn = options.safeParseAsync || (async (value) => {
|
|
21
|
-
try {
|
|
22
|
-
return { success: true, data: await this._parseAsyncFn(value) };
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
return { success: false, error: error };
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Create a new custom validator
|
|
31
|
-
*/
|
|
32
|
-
static create(options) {
|
|
33
|
-
return new VldCustom(options);
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Parse and validate using the custom parse function
|
|
37
|
-
*/
|
|
38
|
-
parse(value) {
|
|
39
|
-
return this._parseFn(value);
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Safely parse using the custom safeParse function or default implementation
|
|
43
|
-
*/
|
|
44
|
-
safeParse(value) {
|
|
45
|
-
return this._safeParseFn(value);
|
|
46
|
-
}
|
|
47
|
-
async parseAsync(value) {
|
|
48
|
-
return this._parseAsyncFn(value);
|
|
49
|
-
}
|
|
50
|
-
async safeParseAsync(value) {
|
|
51
|
-
return this._safeParseAsyncFn(value);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Helper function to create custom validators
|
|
56
|
-
* Usage: v.custom<number>({
|
|
57
|
-
* parse: (value) => {
|
|
58
|
-
* if (typeof value !== 'string') throw new Error('Expected string');
|
|
59
|
-
* return parseInt(value, 10);
|
|
60
|
-
* }
|
|
61
|
-
* })
|
|
62
|
-
*/
|
|
63
|
-
function custom(options) {
|
|
64
|
-
return VldCustom.create(options);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export { VldCustom, custom };
|
|
1
|
+
import{VldBase as s,VLD_VALIDATOR_TYPES as r}from"./base.js";class VldCustom extends s{constructor(s){super(r.CUSTOM),this._parseFn=s.parse,this._parseAsyncFn=s.parseAsync||(s=>Promise.resolve(this._parseFn(s))),this._safeParseFn=s.safeParse||(s=>{try{return{success:!0,data:this._parseFn(s)}}catch(s){return{success:!1,error:s}}}),this._safeParseAsyncFn=s.safeParseAsync||(async s=>{try{return{success:!0,data:await this._parseAsyncFn(s)}}catch(s){return{success:!1,error:s}}})}static create(s){return new VldCustom(s)}parse(s){return this._parseFn(s)}safeParse(s){return this._safeParseFn(s)}async parseAsync(s){return this._parseAsyncFn(s)}async safeParseAsync(s){return this._safeParseAsyncFn(s)}}function e(s){return VldCustom.create(s)}export{VldCustom,e as custom};
|
package/dist/validators/date.js
CHANGED
|
@@ -1,274 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createDateError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_date', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable date validator with chainable methods
|
|
10
|
-
*/
|
|
11
|
-
class VldDate extends VldBase {
|
|
12
|
-
/**
|
|
13
|
-
* Protected constructor to allow extension while maintaining immutability
|
|
14
|
-
*/
|
|
15
|
-
constructor(config) {
|
|
16
|
-
super(config?.validatorType || VLD_VALIDATOR_TYPES.DATE);
|
|
17
|
-
this.config = {
|
|
18
|
-
checks: config?.checks || [],
|
|
19
|
-
errorMessage: config?.errorMessage,
|
|
20
|
-
jsonSchema: config?.jsonSchema
|
|
21
|
-
};
|
|
22
|
-
this._checks = this.config.checks;
|
|
23
|
-
this._isSimple = this._checks.length === 0;
|
|
24
|
-
}
|
|
25
|
-
get jsonSchema() {
|
|
26
|
-
return this.config.jsonSchema;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Create a new date validator
|
|
30
|
-
*/
|
|
31
|
-
static create() {
|
|
32
|
-
return new VldDate();
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Parse and validate a date value
|
|
36
|
-
*/
|
|
37
|
-
parse(value) {
|
|
38
|
-
let date;
|
|
39
|
-
if (value instanceof Date) {
|
|
40
|
-
date = value;
|
|
41
|
-
}
|
|
42
|
-
else if (typeof value === 'string' || typeof value === 'number') {
|
|
43
|
-
date = new Date(value);
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
throw new Error(this.config.errorMessage || getMessages().invalidDate);
|
|
47
|
-
}
|
|
48
|
-
// Check if the date is valid
|
|
49
|
-
if (isNaN(date.getTime())) {
|
|
50
|
-
throw new Error(this.config.errorMessage || getMessages().invalidDate);
|
|
51
|
-
}
|
|
52
|
-
return this.parseValidDate(date);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Parse a Date instance that has already passed the Date validity check.
|
|
56
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
57
|
-
*/
|
|
58
|
-
parseKnownDate(value) {
|
|
59
|
-
if (isNaN(value.getTime())) {
|
|
60
|
-
throw new Error(this.config.errorMessage || getMessages().invalidDate);
|
|
61
|
-
}
|
|
62
|
-
return this.parseValidDate(value);
|
|
63
|
-
}
|
|
64
|
-
parseValidDate(date) {
|
|
65
|
-
if (this._isSimple) {
|
|
66
|
-
return date;
|
|
67
|
-
}
|
|
68
|
-
// Apply all checks
|
|
69
|
-
for (const check of this._checks) {
|
|
70
|
-
if (!check.fn(date)) {
|
|
71
|
-
throw new Error(check.message);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return date;
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Safely parse and validate a date value
|
|
78
|
-
*/
|
|
79
|
-
safeParse(value) {
|
|
80
|
-
if (value instanceof Date) {
|
|
81
|
-
try {
|
|
82
|
-
return { success: true, data: this.parseKnownDate(value) };
|
|
83
|
-
}
|
|
84
|
-
catch (error) {
|
|
85
|
-
return { success: false, error: createDateError(error.message) };
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
try {
|
|
89
|
-
return { success: true, data: this.parse(value) };
|
|
90
|
-
}
|
|
91
|
-
catch (error) {
|
|
92
|
-
return { success: false, error: createDateError(error.message) };
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Create a new validator with minimum date constraint
|
|
97
|
-
* BUG-NEW-009 FIX: Validate that min date is valid before creating validator
|
|
98
|
-
*/
|
|
99
|
-
min(date, message) {
|
|
100
|
-
const minDate = date instanceof Date ? date : new Date(date);
|
|
101
|
-
// Validate that minDate is valid
|
|
102
|
-
if (isNaN(minDate.getTime())) {
|
|
103
|
-
throw new Error(`Invalid date provided to min(): ${date}`);
|
|
104
|
-
}
|
|
105
|
-
return new VldDate({
|
|
106
|
-
...this.config,
|
|
107
|
-
checks: [...this.config.checks, {
|
|
108
|
-
fn: (v) => v >= minDate,
|
|
109
|
-
message: message || getMessages().dateMin(minDate)
|
|
110
|
-
}],
|
|
111
|
-
jsonSchema: { ...this.config.jsonSchema, formatMinimum: minDate.toISOString() }
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Create a new validator with maximum date constraint
|
|
116
|
-
* BUG-NEW-009 FIX: Validate that max date is valid before creating validator
|
|
117
|
-
*/
|
|
118
|
-
max(date, message) {
|
|
119
|
-
const maxDate = date instanceof Date ? date : new Date(date);
|
|
120
|
-
// Validate that maxDate is valid
|
|
121
|
-
if (isNaN(maxDate.getTime())) {
|
|
122
|
-
throw new Error(`Invalid date provided to max(): ${date}`);
|
|
123
|
-
}
|
|
124
|
-
return new VldDate({
|
|
125
|
-
...this.config,
|
|
126
|
-
checks: [...this.config.checks, {
|
|
127
|
-
fn: (v) => v <= maxDate,
|
|
128
|
-
message: message || getMessages().dateMax(maxDate)
|
|
129
|
-
}],
|
|
130
|
-
jsonSchema: { ...this.config.jsonSchema, formatMaximum: maxDate.toISOString() }
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Create a new validator with a range constraint
|
|
135
|
-
* BUG-NEW-019 FIX: Validate that min/max dates are valid before creating validator
|
|
136
|
-
*/
|
|
137
|
-
between(min, max, message) {
|
|
138
|
-
const minDate = min instanceof Date ? min : new Date(min);
|
|
139
|
-
const maxDate = max instanceof Date ? max : new Date(max);
|
|
140
|
-
// Validate that both dates are valid
|
|
141
|
-
if (isNaN(minDate.getTime())) {
|
|
142
|
-
throw new Error(`Invalid min date provided to between(): ${min}`);
|
|
143
|
-
}
|
|
144
|
-
if (isNaN(maxDate.getTime())) {
|
|
145
|
-
throw new Error(`Invalid max date provided to between(): ${max}`);
|
|
146
|
-
}
|
|
147
|
-
return new VldDate({
|
|
148
|
-
...this.config,
|
|
149
|
-
checks: [...this.config.checks, {
|
|
150
|
-
fn: (v) => v >= minDate && v <= maxDate,
|
|
151
|
-
message: message || `Date must be between ${minDate.toISOString()} and ${maxDate.toISOString()}`
|
|
152
|
-
}],
|
|
153
|
-
jsonSchema: {
|
|
154
|
-
...this.config.jsonSchema,
|
|
155
|
-
formatMinimum: minDate.toISOString(),
|
|
156
|
-
formatMaximum: maxDate.toISOString()
|
|
157
|
-
}
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Create a new validator that checks if date is in the past
|
|
162
|
-
* BUG-NEW-006 FIX: Capture reference date at validator creation time for deterministic behavior
|
|
163
|
-
*/
|
|
164
|
-
past(message) {
|
|
165
|
-
const referenceDate = new Date(); // Capture NOW at validator creation time
|
|
166
|
-
return new VldDate({
|
|
167
|
-
...this.config,
|
|
168
|
-
checks: [...this.config.checks, {
|
|
169
|
-
fn: (v) => v < referenceDate,
|
|
170
|
-
message: message || 'Date must be in the past'
|
|
171
|
-
}],
|
|
172
|
-
jsonSchema: { ...this.config.jsonSchema, formatExclusiveMaximum: referenceDate.toISOString() }
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Create a new validator that checks if date is in the future
|
|
177
|
-
* BUG-NEW-006 FIX: Capture reference date at validator creation time for deterministic behavior
|
|
178
|
-
*/
|
|
179
|
-
future(message) {
|
|
180
|
-
const referenceDate = new Date(); // Capture NOW at validator creation time
|
|
181
|
-
return new VldDate({
|
|
182
|
-
...this.config,
|
|
183
|
-
checks: [...this.config.checks, {
|
|
184
|
-
fn: (v) => v > referenceDate,
|
|
185
|
-
message: message || 'Date must be in the future'
|
|
186
|
-
}],
|
|
187
|
-
jsonSchema: { ...this.config.jsonSchema, formatExclusiveMinimum: referenceDate.toISOString() }
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* Create a new validator that checks if date is today
|
|
192
|
-
* BUG-NEW-008 FIX: Capture reference date at validator creation time for deterministic behavior
|
|
193
|
-
*/
|
|
194
|
-
today(message) {
|
|
195
|
-
// Capture the reference date (today) at validator creation time
|
|
196
|
-
const referenceDate = new Date();
|
|
197
|
-
const refYear = referenceDate.getFullYear();
|
|
198
|
-
const refMonth = referenceDate.getMonth();
|
|
199
|
-
const refDate = referenceDate.getDate();
|
|
200
|
-
return new VldDate({
|
|
201
|
-
...this.config,
|
|
202
|
-
checks: [...this.config.checks, {
|
|
203
|
-
fn: (v) => {
|
|
204
|
-
return v.getFullYear() === refYear &&
|
|
205
|
-
v.getMonth() === refMonth &&
|
|
206
|
-
v.getDate() === refDate;
|
|
207
|
-
},
|
|
208
|
-
message: message || 'Date must be today'
|
|
209
|
-
}]
|
|
210
|
-
});
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Create a new validator that checks if date is a weekday
|
|
214
|
-
*/
|
|
215
|
-
weekday(message) {
|
|
216
|
-
return new VldDate({
|
|
217
|
-
...this.config,
|
|
218
|
-
checks: [...this.config.checks, {
|
|
219
|
-
fn: (v) => {
|
|
220
|
-
const day = v.getDay();
|
|
221
|
-
return day >= 1 && day <= 5;
|
|
222
|
-
},
|
|
223
|
-
message: message || 'Date must be a weekday'
|
|
224
|
-
}]
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
/**
|
|
228
|
-
* Create a new validator that checks if date is a weekend
|
|
229
|
-
*/
|
|
230
|
-
weekend(message) {
|
|
231
|
-
return new VldDate({
|
|
232
|
-
...this.config,
|
|
233
|
-
checks: [...this.config.checks, {
|
|
234
|
-
fn: (v) => {
|
|
235
|
-
const day = v.getDay();
|
|
236
|
-
return day === 0 || day === 6;
|
|
237
|
-
},
|
|
238
|
-
message: message || 'Date must be a weekend'
|
|
239
|
-
}]
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Create a new validator with strict greater than constraint
|
|
244
|
-
* Zod 4 API parity - strictly greater than (not equal to)
|
|
245
|
-
*/
|
|
246
|
-
gt(value, message) {
|
|
247
|
-
const compareDate = value instanceof Date ? value : new Date(value);
|
|
248
|
-
return new VldDate({
|
|
249
|
-
...this.config,
|
|
250
|
-
checks: [...this.config.checks, {
|
|
251
|
-
fn: (v) => v.getTime() > compareDate.getTime(),
|
|
252
|
-
message: message || `Date must be greater than ${compareDate.toISOString()}`
|
|
253
|
-
}],
|
|
254
|
-
jsonSchema: { ...this.config.jsonSchema, formatExclusiveMinimum: compareDate.toISOString() }
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Create a new validator with strict less than constraint
|
|
259
|
-
* Zod 4 API parity - strictly less than (not equal to)
|
|
260
|
-
*/
|
|
261
|
-
lt(value, message) {
|
|
262
|
-
const compareDate = value instanceof Date ? value : new Date(value);
|
|
263
|
-
return new VldDate({
|
|
264
|
-
...this.config,
|
|
265
|
-
checks: [...this.config.checks, {
|
|
266
|
-
fn: (v) => v.getTime() < compareDate.getTime(),
|
|
267
|
-
message: message || `Date must be less than ${compareDate.toISOString()}`
|
|
268
|
-
}],
|
|
269
|
-
jsonSchema: { ...this.config.jsonSchema, formatExclusiveMaximum: compareDate.toISOString() }
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
export { VldDate };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as n}from"../chunks/errors-core-Cqi4-9ew.js";function i(e){return new n([{code:"invalid_date",path:[],message:e}])}class VldDate extends e{constructor(e){super(e?.validatorType||t.DATE),this.config={checks:e?.checks||[],errorMessage:e?.errorMessage,jsonSchema:e?.jsonSchema},this._checks=this.config.checks,this._isSimple=0===this._checks.length}get jsonSchema(){return this.config.jsonSchema}static create(){return new VldDate}parse(e){let t;if(e instanceof Date)t=e;else{if("string"!=typeof e&&"number"!=typeof e)throw new Error(this.config.errorMessage||s().invalidDate);t=new Date(e)}if(isNaN(t.getTime()))throw new Error(this.config.errorMessage||s().invalidDate);return this.parseValidDate(t)}parseKnownDate(e){if(isNaN(e.getTime()))throw new Error(this.config.errorMessage||s().invalidDate);return this.parseValidDate(e)}parseValidDate(e){if(this._isSimple)return e;for(const t of this._checks)if(!t.fn(e))throw new Error(t.message);return e}safeParse(e){if(e instanceof Date)try{return{success:!0,data:this.parseKnownDate(e)}}catch(e){return{success:!1,error:i(e.message)}}try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:i(e.message)}}}min(e,t){const n=e instanceof Date?e:new Date(e);if(isNaN(n.getTime()))throw new Error(`Invalid date provided to min(): ${e}`);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e>=n,message:t||s().dateMin(n)}],jsonSchema:{...this.config.jsonSchema,formatMinimum:n.toISOString()}})}max(e,t){const n=e instanceof Date?e:new Date(e);if(isNaN(n.getTime()))throw new Error(`Invalid date provided to max(): ${e}`);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e<=n,message:t||s().dateMax(n)}],jsonSchema:{...this.config.jsonSchema,formatMaximum:n.toISOString()}})}between(e,t,s){const n=e instanceof Date?e:new Date(e),i=t instanceof Date?t:new Date(t);if(isNaN(n.getTime()))throw new Error(`Invalid min date provided to between(): ${e}`);if(isNaN(i.getTime()))throw new Error(`Invalid max date provided to between(): ${t}`);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e>=n&&e<=i,message:s||`Date must be between ${n.toISOString()} and ${i.toISOString()}`}],jsonSchema:{...this.config.jsonSchema,formatMinimum:n.toISOString(),formatMaximum:i.toISOString()}})}past(e){const t=new Date;return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e<t,message:e||"Date must be in the past"}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMaximum:t.toISOString()}})}future(e){const t=new Date;return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e>t,message:e||"Date must be in the future"}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMinimum:t.toISOString()}})}today(e){const t=new Date,s=t.getFullYear(),n=t.getMonth(),i=t.getDate();return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e.getFullYear()===s&&e.getMonth()===n&&e.getDate()===i,message:e||"Date must be today"}]})}weekday(e){return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>{const t=e.getDay();return t>=1&&t<=5},message:e||"Date must be a weekday"}]})}weekend(e){return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>{const t=e.getDay();return 0===t||6===t},message:e||"Date must be a weekend"}]})}gt(e,t){const s=e instanceof Date?e:new Date(e);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e.getTime()>s.getTime(),message:t||`Date must be greater than ${s.toISOString()}`}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMinimum:s.toISOString()}})}lt(e,t){const s=e instanceof Date?e:new Date(e);return new VldDate({...this.config,checks:[...this.config.checks,{fn:e=>e.getTime()<s.getTime(),message:t||`Date must be less than ${s.toISOString()}`}],jsonSchema:{...this.config.jsonSchema,formatExclusiveMaximum:s.toISOString()}})}}export{VldDate};
|
|
@@ -9,14 +9,14 @@ import type { ParseResult } from './base.js';
|
|
|
9
9
|
* Discriminated union validator - validates union based on discriminator key
|
|
10
10
|
* Much faster than regular union when you have a discriminator field
|
|
11
11
|
*/
|
|
12
|
-
export declare class VldDiscriminatedUnion<K extends string, Options extends VldBase<any, any>[]> extends VldBase<unknown, Options[number] extends VldBase<any, infer T> ? T : never> {
|
|
12
|
+
export declare class VldDiscriminatedUnion<K extends string, Options extends readonly VldBase<any, any>[]> extends VldBase<unknown, Options[number] extends VldBase<any, infer T> ? T : never> {
|
|
13
13
|
private readonly _discriminator;
|
|
14
14
|
private readonly _options;
|
|
15
15
|
private readonly _discriminatorMap;
|
|
16
16
|
private readonly _stringDiscriminatorMap;
|
|
17
17
|
private readonly _validValues;
|
|
18
18
|
constructor(_discriminator: K, _options: Options);
|
|
19
|
-
static create<K extends string, Options extends VldBase<any, any>[]>(discriminator: K, options: Options): VldDiscriminatedUnion<K, Options>;
|
|
19
|
+
static create<K extends string, Options extends readonly VldBase<any, any>[]>(discriminator: K, options: Options): VldDiscriminatedUnion<K, Options>;
|
|
20
20
|
parse(value: unknown): Options[number] extends VldBase<any, infer T> ? T : never;
|
|
21
21
|
safeParse(value: unknown): ParseResult<Options[number] extends VldBase<any, infer T> ? T : never>;
|
|
22
22
|
/**
|
|
@@ -1,123 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { VldObject } from './object.js';
|
|
3
|
-
import { VldLiteral } from './literal.js';
|
|
4
|
-
import { VldEnum } from './enum.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* VldDiscriminatedUnion - Fast union validation using discriminator field
|
|
8
|
-
* Part of Zod 4 API parity implementation
|
|
9
|
-
* Provides O(1) lookup performance by using a discriminator key
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
* Extract literal values from a schema
|
|
13
|
-
*/
|
|
14
|
-
function extractLiteralValues(schema) {
|
|
15
|
-
if (schema instanceof VldLiteral) {
|
|
16
|
-
return [schema.literal];
|
|
17
|
-
}
|
|
18
|
-
if (schema instanceof VldEnum) {
|
|
19
|
-
return [...schema.values];
|
|
20
|
-
}
|
|
21
|
-
throw new Error('Discriminator must be a literal or enum schema');
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Discriminated union validator - validates union based on discriminator key
|
|
25
|
-
* Much faster than regular union when you have a discriminator field
|
|
26
|
-
*/
|
|
27
|
-
class VldDiscriminatedUnion extends VldBase {
|
|
28
|
-
constructor(_discriminator, _options) {
|
|
29
|
-
super(VLD_VALIDATOR_TYPES.DISCRIMINATED_UNION);
|
|
30
|
-
this._discriminator = _discriminator;
|
|
31
|
-
this._options = _options;
|
|
32
|
-
// Build discriminator map for O(1) lookup
|
|
33
|
-
this._discriminatorMap = new Map();
|
|
34
|
-
this._stringDiscriminatorMap = Object.create(null);
|
|
35
|
-
for (const option of _options) {
|
|
36
|
-
if (!(option instanceof VldObject)) {
|
|
37
|
-
throw new Error('All options in a discriminated union must be objects');
|
|
38
|
-
}
|
|
39
|
-
const discriminatorSchema = option.config?.shape?.[this._discriminator];
|
|
40
|
-
if (!discriminatorSchema) {
|
|
41
|
-
throw new Error(`Missing discriminator key "${this._discriminator}" in one of the options`);
|
|
42
|
-
}
|
|
43
|
-
const values = extractLiteralValues(discriminatorSchema);
|
|
44
|
-
for (const value of values) {
|
|
45
|
-
if (this._discriminatorMap.has(value)) {
|
|
46
|
-
throw new Error(`Duplicate discriminator value "${String(value)}" found in discriminated union`);
|
|
47
|
-
}
|
|
48
|
-
this._discriminatorMap.set(value, option);
|
|
49
|
-
if (typeof value === 'string') {
|
|
50
|
-
this._stringDiscriminatorMap[value] = option;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
this._validValues = Array.from(this._discriminatorMap.keys());
|
|
55
|
-
}
|
|
56
|
-
static create(discriminator, options) {
|
|
57
|
-
return new VldDiscriminatedUnion(discriminator, options);
|
|
58
|
-
}
|
|
59
|
-
parse(value) {
|
|
60
|
-
if (typeof value !== 'object' || value === null) {
|
|
61
|
-
throw new Error(`Expected object, received ${value === null ? 'null' : typeof value}`);
|
|
62
|
-
}
|
|
63
|
-
const discriminatorValue = value[this._discriminator];
|
|
64
|
-
const matchedSchema = typeof discriminatorValue === 'string'
|
|
65
|
-
? this._stringDiscriminatorMap[discriminatorValue]
|
|
66
|
-
: this._discriminatorMap.get(discriminatorValue);
|
|
67
|
-
if (!matchedSchema) {
|
|
68
|
-
throw new Error(`Invalid discriminator value for "${this._discriminator}". ` +
|
|
69
|
-
`Expected one of: ${JSON.stringify(this._validValues)}, ` +
|
|
70
|
-
`received: ${JSON.stringify(discriminatorValue)}`);
|
|
71
|
-
}
|
|
72
|
-
return matchedSchema.parseTrustedKnownObject(value, this._discriminator);
|
|
73
|
-
}
|
|
74
|
-
safeParse(value) {
|
|
75
|
-
// Check if input is an object
|
|
76
|
-
if (typeof value !== 'object' || value === null) {
|
|
77
|
-
return {
|
|
78
|
-
success: false,
|
|
79
|
-
error: new Error(`Expected object, received ${value === null ? 'null' : typeof value}`)
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
// Get discriminator value
|
|
83
|
-
const discriminatorValue = value[this._discriminator];
|
|
84
|
-
// Look up matching schema
|
|
85
|
-
const matchedSchema = typeof discriminatorValue === 'string'
|
|
86
|
-
? this._stringDiscriminatorMap[discriminatorValue]
|
|
87
|
-
: this._discriminatorMap.get(discriminatorValue);
|
|
88
|
-
if (!matchedSchema) {
|
|
89
|
-
return {
|
|
90
|
-
success: false,
|
|
91
|
-
error: new Error(`Invalid discriminator value for "${this._discriminator}". ` +
|
|
92
|
-
`Expected one of: ${JSON.stringify(this._validValues)}, ` +
|
|
93
|
-
`received: ${JSON.stringify(discriminatorValue)}`)
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
try {
|
|
97
|
-
return {
|
|
98
|
-
success: true,
|
|
99
|
-
data: matchedSchema.parseTrustedKnownObject(value, this._discriminator)
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
catch (error) {
|
|
103
|
-
return {
|
|
104
|
-
success: false,
|
|
105
|
-
error: error
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Get the discriminator key
|
|
111
|
-
*/
|
|
112
|
-
getDiscriminator() {
|
|
113
|
-
return this._discriminator;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Get all options
|
|
117
|
-
*/
|
|
118
|
-
getOptions() {
|
|
119
|
-
return this._options;
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export { VldDiscriminatedUnion };
|
|
1
|
+
import{VldBase as i,VLD_VALIDATOR_TYPES as r}from"./base.js";import{VldObject as t}from"./object.js";import{VldLiteral as s}from"./literal.js";import{VldEnum as n}from"./enum.js";function e(i){if(i instanceof s)return[...i.values];if(i instanceof n)return[...i.values];throw new Error("Discriminator must be a literal or enum schema")}class VldDiscriminatedUnion extends i{constructor(i,s){super(r.DISCRIMINATED_UNION),this._discriminator=i,this._options=s,this._discriminatorMap=new Map,this._stringDiscriminatorMap=Object.create(null);for(const i of s){if(!(i instanceof t))throw new Error("All options in a discriminated union must be objects");const r=i.config?.shape?.[this._discriminator];if(!r)throw new Error(`Missing discriminator key "${this._discriminator}" in one of the options`);const s=e(r);for(const r of s){if(this._discriminatorMap.has(r))throw new Error(`Duplicate discriminator value "${String(r)}" found in discriminated union`);this._discriminatorMap.set(r,i),"string"==typeof r&&(this._stringDiscriminatorMap[r]=i)}}this._validValues=Array.from(this._discriminatorMap.keys())}static create(i,r){return new VldDiscriminatedUnion(i,r)}parse(i){if("object"!=typeof i||null===i)throw new Error("Expected object, received "+(null===i?"null":typeof i));const r=i[this._discriminator],t="string"==typeof r?this._stringDiscriminatorMap[r]:this._discriminatorMap.get(r);if(!t)throw new Error(`Invalid discriminator value for "${this._discriminator}". Expected one of: ${JSON.stringify(this._validValues)}, received: ${JSON.stringify(r)}`);return t.parseTrustedKnownObject(i,this._discriminator)}safeParse(i){if("object"!=typeof i||null===i)return{success:!1,error:new Error("Expected object, received "+(null===i?"null":typeof i))};const r=i[this._discriminator],t="string"==typeof r?this._stringDiscriminatorMap[r]:this._discriminatorMap.get(r);if(!t)return{success:!1,error:new Error(`Invalid discriminator value for "${this._discriminator}". Expected one of: ${JSON.stringify(this._validValues)}, received: ${JSON.stringify(r)}`)};try{return{success:!0,data:t.parseTrustedKnownObject(i,this._discriminator)}}catch(i){return{success:!1,error:i}}}getDiscriminator(){return this._discriminator}getOptions(){return this._options}}export{VldDiscriminatedUnion};
|
package/dist/validators/enum.js
CHANGED
|
@@ -1,150 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
class VldEnum extends VldBase {
|
|
5
|
-
/**
|
|
6
|
-
* Private constructor to enforce immutability
|
|
7
|
-
*/
|
|
8
|
-
constructor(_values, errorMessage) {
|
|
9
|
-
super(VLD_VALIDATOR_TYPES.ENUM);
|
|
10
|
-
this._values = _values;
|
|
11
|
-
this.errorMessage = errorMessage;
|
|
12
|
-
this._valueSet = _values.length > 6 ? new Set(_values) : undefined;
|
|
13
|
-
}
|
|
14
|
-
createError(value) {
|
|
15
|
-
return new Error(this.errorMessage ||
|
|
16
|
-
getMessages().enumExpected([...this._values], JSON.stringify(value)));
|
|
17
|
-
}
|
|
18
|
-
/**
|
|
19
|
-
* Get the enum values
|
|
20
|
-
*/
|
|
21
|
-
get values() {
|
|
22
|
-
return this._values;
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Create a new enum validator
|
|
26
|
-
*/
|
|
27
|
-
static create(values) {
|
|
28
|
-
return new VldEnum(values);
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Parse and validate an enum value
|
|
32
|
-
*/
|
|
33
|
-
parse(value) {
|
|
34
|
-
// BUG-002 FIX: Add type check before includes() to prevent type confusion
|
|
35
|
-
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
36
|
-
throw this.createError(value);
|
|
37
|
-
}
|
|
38
|
-
const values = this._values;
|
|
39
|
-
switch (values.length) {
|
|
40
|
-
case 1:
|
|
41
|
-
if (value === values[0])
|
|
42
|
-
return value;
|
|
43
|
-
break;
|
|
44
|
-
case 2:
|
|
45
|
-
if (value === values[0] || value === values[1])
|
|
46
|
-
return value;
|
|
47
|
-
break;
|
|
48
|
-
case 3:
|
|
49
|
-
if (value === values[0] || value === values[1] || value === values[2])
|
|
50
|
-
return value;
|
|
51
|
-
break;
|
|
52
|
-
case 4:
|
|
53
|
-
if (value === values[0] || value === values[1] || value === values[2] || value === values[3]) {
|
|
54
|
-
return value;
|
|
55
|
-
}
|
|
56
|
-
break;
|
|
57
|
-
case 5:
|
|
58
|
-
if (value === values[0] || value === values[1] || value === values[2] || value === values[3] || value === values[4]) {
|
|
59
|
-
return value;
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
case 6:
|
|
63
|
-
if (value === values[0] || value === values[1] || value === values[2] || value === values[3] || value === values[4] || value === values[5]) {
|
|
64
|
-
return value;
|
|
65
|
-
}
|
|
66
|
-
break;
|
|
67
|
-
default:
|
|
68
|
-
if (this._valueSet.has(value))
|
|
69
|
-
return value;
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
throw this.createError(value);
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Safely parse and validate an enum value
|
|
76
|
-
*/
|
|
77
|
-
safeParse(value) {
|
|
78
|
-
// BUG-002 FIX: Add type check before includes() to prevent type confusion
|
|
79
|
-
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
80
|
-
return {
|
|
81
|
-
success: false,
|
|
82
|
-
error: this.createError(value)
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
const values = this._values;
|
|
86
|
-
switch (values.length) {
|
|
87
|
-
case 1:
|
|
88
|
-
if (value === values[0])
|
|
89
|
-
return { success: true, data: value };
|
|
90
|
-
break;
|
|
91
|
-
case 2:
|
|
92
|
-
if (value === values[0] || value === values[1])
|
|
93
|
-
return { success: true, data: value };
|
|
94
|
-
break;
|
|
95
|
-
case 3:
|
|
96
|
-
if (value === values[0] || value === values[1] || value === values[2])
|
|
97
|
-
return { success: true, data: value };
|
|
98
|
-
break;
|
|
99
|
-
case 4:
|
|
100
|
-
if (value === values[0] || value === values[1] || value === values[2] || value === values[3]) {
|
|
101
|
-
return { success: true, data: value };
|
|
102
|
-
}
|
|
103
|
-
break;
|
|
104
|
-
case 5:
|
|
105
|
-
if (value === values[0] || value === values[1] || value === values[2] || value === values[3] || value === values[4]) {
|
|
106
|
-
return { success: true, data: value };
|
|
107
|
-
}
|
|
108
|
-
break;
|
|
109
|
-
case 6:
|
|
110
|
-
if (value === values[0] || value === values[1] || value === values[2] || value === values[3] || value === values[4] || value === values[5]) {
|
|
111
|
-
return { success: true, data: value };
|
|
112
|
-
}
|
|
113
|
-
break;
|
|
114
|
-
default:
|
|
115
|
-
if (this._valueSet.has(value))
|
|
116
|
-
return { success: true, data: value };
|
|
117
|
-
break;
|
|
118
|
-
}
|
|
119
|
-
return {
|
|
120
|
-
success: false,
|
|
121
|
-
error: this.createError(value)
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Exclude specific values from the enum
|
|
126
|
-
* Creates a new enum validator without the specified values
|
|
127
|
-
*/
|
|
128
|
-
exclude(...excludeValues) {
|
|
129
|
-
const filtered = this._values.filter(v => !excludeValues.includes(v));
|
|
130
|
-
if (filtered.length === 0) {
|
|
131
|
-
throw new Error('Cannot exclude all enum values');
|
|
132
|
-
}
|
|
133
|
-
// Type assertion needed because TypeScript can't guarantee the filtered array maintains the required type
|
|
134
|
-
return new VldEnum(filtered, this.errorMessage);
|
|
135
|
-
}
|
|
136
|
-
/**
|
|
137
|
-
* Extract specific values from the enum
|
|
138
|
-
* Creates a new enum validator with only the specified values
|
|
139
|
-
*/
|
|
140
|
-
extract(...extractValues) {
|
|
141
|
-
const extracted = this._values.filter(v => extractValues.includes(v));
|
|
142
|
-
if (extracted.length === 0) {
|
|
143
|
-
throw new Error('Cannot extract non-existent enum values');
|
|
144
|
-
}
|
|
145
|
-
// Type assertion needed because TypeScript can't guarantee the extracted array has the required type
|
|
146
|
-
return new VldEnum(extracted, this.errorMessage);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export { VldEnum };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as t}from"../chunks/errors-core-Cqi4-9ew.js";class VldEnum extends e{constructor(e,s){super(r.ENUM),this._values=e,this.errorMessage=s,this._valueSet=e.length>6?new Set(e):void 0}createError(e){const r={code:"invalid_value",path:[],values:[...this._values],message:this.errorMessage||s().enumExpected([...this._values],JSON.stringify(e))};return new t([r])}get values(){return this._values}static create(e){return new VldEnum(e)}parse(e){if("string"!=typeof e&&"number"!=typeof e)throw this.createError(e);const r=this._values;switch(r.length){case 1:if(e===r[0])return e;break;case 2:if(e===r[0]||e===r[1])return e;break;case 3:if(e===r[0]||e===r[1]||e===r[2])return e;break;case 4:if(e===r[0]||e===r[1]||e===r[2]||e===r[3])return e;break;case 5:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4])return e;break;case 6:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4]||e===r[5])return e;break;default:if(this._valueSet.has(e))return e}throw this.createError(e)}safeParse(e){if("string"!=typeof e&&"number"!=typeof e)return{success:!1,error:this.createError(e)};const r=this._values;switch(r.length){case 1:if(e===r[0])return{success:!0,data:e};break;case 2:if(e===r[0]||e===r[1])return{success:!0,data:e};break;case 3:if(e===r[0]||e===r[1]||e===r[2])return{success:!0,data:e};break;case 4:if(e===r[0]||e===r[1]||e===r[2]||e===r[3])return{success:!0,data:e};break;case 5:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4])return{success:!0,data:e};break;case 6:if(e===r[0]||e===r[1]||e===r[2]||e===r[3]||e===r[4]||e===r[5])return{success:!0,data:e};break;default:if(this._valueSet.has(e))return{success:!0,data:e}}return{success:!1,error:this.createError(e)}}exclude(...e){const r=this._values.filter(r=>!e.includes(r));if(0===r.length)throw new Error("Cannot exclude all enum values");return new VldEnum(r,this.errorMessage)}extract(...e){const r=this._values.filter(r=>e.includes(r));if(0===r.length)throw new Error("Cannot extract non-existent enum values");return new VldEnum(r,this.errorMessage)}}export{VldEnum};
|