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