@oxog/vld 2.1.0 → 2.2.7
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 +113 -0
- package/README.md +413 -1235
- package/dist/chunks/errors-core-CerMg2rq.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-DrydF46s.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 +32 -2
- package/dist/errors.js +1 -171
- package/dist/index.d.ts +315 -361
- 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 +210 -17
- package/dist/validators/base.js +1 -868
- package/dist/validators/base64.js +1 -68
- package/dist/validators/bigint.d.ts +7 -0
- 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.d.ts +20 -10
- package/dist/validators/custom.js +1 -67
- package/dist/validators/date.d.ts +2 -0
- package/dist/validators/date.js +1 -274
- package/dist/validators/discriminated-union.d.ts +4 -2
- package/dist/validators/discriminated-union.js +1 -123
- package/dist/validators/enum.d.ts +8 -2
- 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 +8 -3
- package/dist/validators/literal.js +1 -60
- package/dist/validators/map.d.ts +7 -0
- 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 +25 -1
- package/dist/validators/number.js +1 -423
- package/dist/validators/object.d.ts +18 -2
- package/dist/validators/object.js +1 -864
- package/dist/validators/promise.d.ts +6 -7
- package/dist/validators/promise.js +1 -111
- package/dist/validators/record.d.ts +13 -8
- package/dist/validators/record.js +1 -258
- package/dist/validators/set.d.ts +5 -0
- 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 +49 -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.d.ts +1 -0
- 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 +240 -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
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-CerMg2rq.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}get options(){return this._values}get enum(){const e={};for(const r of this._values)e[r]=r;return e}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=1===e.length&&Array.isArray(e[0])?e[0]:e,s=this._values.filter(e=>!r.includes(e));if(0===s.length)throw new Error("Cannot exclude all enum values");return new VldEnum(s,this.errorMessage)}extract(...e){const r=1===e.length&&Array.isArray(e[0])?e[0]:e,s=this._values.filter(e=>r.includes(e));if(0===s.length)throw new Error("Cannot extract non-existent enum values");return new VldEnum(s,this.errorMessage)}}export{VldEnum};
|
package/dist/validators/file.js
CHANGED
|
@@ -1,125 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createFileError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable file validator for file uploads
|
|
10
|
-
* Supports size and MIME type validation
|
|
11
|
-
*/
|
|
12
|
-
class VldFile extends VldBase {
|
|
13
|
-
constructor(checks = []) {
|
|
14
|
-
super(VLD_VALIDATOR_TYPES.FILE);
|
|
15
|
-
this.checks = checks;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create a new file validator
|
|
19
|
-
*/
|
|
20
|
-
static create() {
|
|
21
|
-
return new VldFile();
|
|
22
|
-
}
|
|
23
|
-
isFileLike(value) {
|
|
24
|
-
if (typeof value !== 'object' || value === null) {
|
|
25
|
-
return false;
|
|
26
|
-
}
|
|
27
|
-
if (typeof File !== 'undefined' && value instanceof File) {
|
|
28
|
-
return true;
|
|
29
|
-
}
|
|
30
|
-
return 'size' in value && 'type' in value;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Parse and validate a file value
|
|
34
|
-
*/
|
|
35
|
-
parse(value) {
|
|
36
|
-
if (this.isFileLike(value)) {
|
|
37
|
-
return this.parseKnownFile(value);
|
|
38
|
-
}
|
|
39
|
-
if (typeof File === 'undefined') {
|
|
40
|
-
throw new Error(getMessages().fileNotSupported || 'File API not supported in this environment');
|
|
41
|
-
}
|
|
42
|
-
throw new Error(getMessages().invalidFile || 'Expected a File object');
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Parse a value that has already passed the file-like type guard.
|
|
46
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
47
|
-
*/
|
|
48
|
-
parseKnownFile(value) {
|
|
49
|
-
return this.runChecks(value);
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Run all validation checks on the file
|
|
53
|
-
*/
|
|
54
|
-
runChecks(file) {
|
|
55
|
-
const msgs = getMessages();
|
|
56
|
-
for (const check of this.checks) {
|
|
57
|
-
switch (check.type) {
|
|
58
|
-
case 'min':
|
|
59
|
-
if (file.size < check.value) {
|
|
60
|
-
throw new Error(check.message ||
|
|
61
|
-
(msgs.fileMinSize?.(check.value) || `File size must be at least ${check.value} bytes`));
|
|
62
|
-
}
|
|
63
|
-
break;
|
|
64
|
-
case 'max':
|
|
65
|
-
if (file.size > check.value) {
|
|
66
|
-
throw new Error(check.message ||
|
|
67
|
-
(msgs.fileMaxSize?.(check.value) || `File size must not exceed ${check.value} bytes`));
|
|
68
|
-
}
|
|
69
|
-
break;
|
|
70
|
-
case 'mime':
|
|
71
|
-
if (!check.value.includes(file.type)) {
|
|
72
|
-
throw new Error(check.message ||
|
|
73
|
-
(msgs.fileMimeType?.(check.value) || `Invalid file type. Expected: ${check.value.join(', ')}`));
|
|
74
|
-
}
|
|
75
|
-
break;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
return file;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Safely parse and validate a file value
|
|
82
|
-
*/
|
|
83
|
-
safeParse(value) {
|
|
84
|
-
try {
|
|
85
|
-
return { success: true, data: this.parse(value) };
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
return { success: false, error: createFileError(error.message) };
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Set minimum file size in bytes
|
|
93
|
-
*/
|
|
94
|
-
min(bytes, message) {
|
|
95
|
-
return new VldFile([...this.checks, { type: 'min', value: bytes, message }]);
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Set maximum file size in bytes
|
|
99
|
-
*/
|
|
100
|
-
max(bytes, message) {
|
|
101
|
-
return new VldFile([...this.checks, { type: 'max', value: bytes, message }]);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Set allowed MIME types
|
|
105
|
-
*/
|
|
106
|
-
mime(types, message) {
|
|
107
|
-
const typeArray = Array.isArray(types) ? types : [types];
|
|
108
|
-
return new VldFile([...this.checks, { type: 'mime', value: typeArray, message }]);
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Set both minimum and maximum file size
|
|
112
|
-
*/
|
|
113
|
-
size(min, max, message) {
|
|
114
|
-
return this.min(min, message).max(max, message);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Helper function to create file validators
|
|
119
|
-
* Usage: v.file().min(1024).max(10485760).mime(['image/jpeg', 'image/png'])
|
|
120
|
-
*/
|
|
121
|
-
function file() {
|
|
122
|
-
return VldFile.create();
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export { VldFile, file };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{getMessages as i}from"../locales/runtime.js";import{V as r}from"../chunks/errors-core-CerMg2rq.js";class VldFile extends e{constructor(e=[]){super(s.FILE),this.checks=e}static create(){return new VldFile}isFileLike(e){return"object"==typeof e&&null!==e&&("undefined"!=typeof File&&e instanceof File||"size"in e&&"type"in e)}parse(e){if(this.isFileLike(e))return this.parseKnownFile(e);if("undefined"==typeof File)throw new Error(i().fileNotSupported||"File API not supported in this environment");throw new Error(i().invalidFile||"Expected a File object")}parseKnownFile(e){return this.runChecks(e)}runChecks(e){const s=i();for(const i of this.checks)switch(i.type){case"min":if(e.size<i.value)throw new Error(i.message||s.fileMinSize?.(i.value)||`File size must be at least ${i.value} bytes`);break;case"max":if(e.size>i.value)throw new Error(i.message||s.fileMaxSize?.(i.value)||`File size must not exceed ${i.value} bytes`);break;case"mime":if(!i.value.includes(e.type))throw new Error(i.message||s.fileMimeType?.(i.value)||`Invalid file type. Expected: ${i.value.join(", ")}`)}return e}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(s=e.message,new r([{code:"invalid_type",path:[],message:s}]))}}var s}min(e,s){return new VldFile([...this.checks,{type:"min",value:e,message:s}])}max(e,s){return new VldFile([...this.checks,{type:"max",value:e,message:s}])}mime(e,s){const i=Array.isArray(e)?e:[e];return new VldFile([...this.checks,{type:"mime",value:i,message:s}])}size(e,s,i){return this.min(e,i).max(s,i)}}function t(){return VldFile.create()}export{VldFile,t as file};
|
|
@@ -1,58 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createFunctionError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable function validator
|
|
10
|
-
* Validates that a value is a function
|
|
11
|
-
*/
|
|
12
|
-
class VldFunction extends VldBase {
|
|
13
|
-
constructor() {
|
|
14
|
-
super(VLD_VALIDATOR_TYPES.FUNCTION);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Create a new function validator
|
|
18
|
-
*/
|
|
19
|
-
static create() {
|
|
20
|
-
return new VldFunction();
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Parse and validate a function value
|
|
24
|
-
*/
|
|
25
|
-
parse(value) {
|
|
26
|
-
if (typeof value !== 'function') {
|
|
27
|
-
throw new Error(getMessages().invalidFunction || 'Expected a function');
|
|
28
|
-
}
|
|
29
|
-
return value;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Parse a value that has already passed the function type guard.
|
|
33
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
34
|
-
*/
|
|
35
|
-
parseKnownFunction(value) {
|
|
36
|
-
return value;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Safely parse and validate a function value
|
|
40
|
-
*/
|
|
41
|
-
safeParse(value) {
|
|
42
|
-
try {
|
|
43
|
-
return { success: true, data: this.parse(value) };
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
return { success: false, error: createFunctionError(error.message) };
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Helper function to create function validators
|
|
52
|
-
* Usage: v.function()
|
|
53
|
-
*/
|
|
54
|
-
function functionValidator() {
|
|
55
|
-
return VldFunction.create();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export { VldFunction, functionValidator };
|
|
1
|
+
import{VldBase as r,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as n}from"../chunks/errors-core-CerMg2rq.js";class VldFunction extends r{constructor(){super(e.FUNCTION)}static create(){return new VldFunction}parse(r){if("function"!=typeof r)throw new Error(t().invalidFunction||"Expected a function");return r}parseKnownFunction(r){return r}safeParse(r){try{return{success:!0,data:this.parse(r)}}catch(r){return{success:!1,error:(e=r.message,new n([{code:"invalid_type",path:[],message:e}]))}}var e}}function s(){return VldFunction.create()}export{VldFunction,s as functionValidator};
|
package/dist/validators/hex.js
CHANGED
|
@@ -1,65 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { createIssue } from '../errors.js';
|
|
3
|
-
import { getMessages } from '../locales/runtime.js';
|
|
4
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Hexadecimal string validator
|
|
8
|
-
*/
|
|
9
|
-
class VldHex extends VldBase {
|
|
10
|
-
constructor(lowercase = false) {
|
|
11
|
-
super(VLD_VALIDATOR_TYPES.HEX);
|
|
12
|
-
this.lowercase = lowercase;
|
|
13
|
-
}
|
|
14
|
-
parse(value) {
|
|
15
|
-
const result = this.safeParse(value);
|
|
16
|
-
if (!result.success) {
|
|
17
|
-
throw result.error;
|
|
18
|
-
}
|
|
19
|
-
return result.data;
|
|
20
|
-
}
|
|
21
|
-
safeParse(value) {
|
|
22
|
-
if (typeof value !== 'string') {
|
|
23
|
-
return {
|
|
24
|
-
success: false,
|
|
25
|
-
error: new VldError([
|
|
26
|
-
createIssue('invalid_type', [], getMessages().expectedString)
|
|
27
|
-
])
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
// Check if it's a valid hex string
|
|
31
|
-
if (!VldHex.HEX_REGEX.test(value)) {
|
|
32
|
-
return {
|
|
33
|
-
success: false,
|
|
34
|
-
error: new VldError([
|
|
35
|
-
createIssue('custom', [], getMessages().invalidHex)
|
|
36
|
-
])
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
// Check if even length (full bytes)
|
|
40
|
-
if (value.length % 2 !== 0) {
|
|
41
|
-
return {
|
|
42
|
-
success: false,
|
|
43
|
-
error: new VldError([
|
|
44
|
-
createIssue('custom', [], getMessages().invalidHex)
|
|
45
|
-
])
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
// Normalize case if required
|
|
49
|
-
const normalizedValue = this.lowercase ? value.toLowerCase() : value;
|
|
50
|
-
return { success: true, data: normalizedValue };
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Require lowercase hex characters
|
|
54
|
-
*/
|
|
55
|
-
lowercaseMode() {
|
|
56
|
-
return new VldHex(true);
|
|
57
|
-
}
|
|
58
|
-
static create(lowercase = false) {
|
|
59
|
-
return new VldHex(lowercase);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
// BUG-008 FIX: Use * instead of + to allow empty hex strings (consistent with base64 validator)
|
|
63
|
-
VldHex.HEX_REGEX = /^[0-9a-fA-F]*$/;
|
|
64
|
-
|
|
65
|
-
export { VldHex };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{createIssue as s}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as o}from"../chunks/errors-core-CerMg2rq.js";class VldHex extends e{constructor(e=!1){super(r.HEX),this.lowercase=e}parse(e){const r=this.safeParse(e);if(!r.success)throw r.error;return r.data}safeParse(e){if("string"!=typeof e)return{success:!1,error:new o([s("invalid_type",[],t().expectedString)])};if(!VldHex.HEX_REGEX.test(e))return{success:!1,error:new o([s("custom",[],t().invalidHex)])};if(e.length%2!=0)return{success:!1,error:new o([s("custom",[],t().invalidHex)])};return{success:!0,data:this.lowercase?e.toLowerCase():e}}lowercaseMode(){return new VldHex(!0)}static create(e=!1){return new VldHex(e)}}VldHex.HEX_REGEX=/^[0-9a-fA-F]*$/;export{VldHex};
|
|
@@ -37,7 +37,7 @@ export { VldFile, file } from './file.js';
|
|
|
37
37
|
export type { VldFileValue } from './file.js';
|
|
38
38
|
export { VldFunction, functionValidator } from './function.js';
|
|
39
39
|
export { VldPromise, promise } from './promise.js';
|
|
40
|
-
export { email, uuid, uuidv4, hostname, emoji, base64, base64url, hex, jwt, nanoid, cuid, cuid2, ulid, ipv4, ipv6, mac, cidrv4, cidrv6, e164, hash, iso, stringFormat, regexes, xid, guid, httpUrl } from './string-formats.js';
|
|
40
|
+
export { email, url, uuid, uuidv4, uuidv6, uuidv7, hostname, emoji, base64, base64url, hex, jwt, nanoid, cuid, cuid2, ulid, ipv4, ipv6, mac, cidrv4, cidrv6, e164, hash, iso, stringFormat, regexes, xid, guid, httpUrl, ksuid, type ISODateTimeOptions, type ISOTimeOptions, type URLFormatOptions, type UUIDVersion } from './string-formats.js';
|
|
41
41
|
import { VldBase } from './base.js';
|
|
42
42
|
export type Infer<T extends VldBase<any, any>> = T extends VldBase<any, infer U> ? U : never;
|
|
43
43
|
export type Input<T extends VldBase<any, any>> = T extends VldBase<infer I, any> ? I : never;
|
package/dist/validators/index.js
CHANGED
|
@@ -1,35 +1 @@
|
|
|
1
|
-
export
|
|
2
|
-
export { V as VldString } from '../chunks/string-BJRQod3t.js';
|
|
3
|
-
export { VldNumber } from './number.js';
|
|
4
|
-
export { VldBoolean } from './boolean.js';
|
|
5
|
-
export { VldDate } from './date.js';
|
|
6
|
-
export { VldStringBool } from './string-bool.js';
|
|
7
|
-
export { VldArray } from './array.js';
|
|
8
|
-
export { VldObject } from './object.js';
|
|
9
|
-
export { VldUnion } from './union.js';
|
|
10
|
-
export { VldIntersection } from './intersection.js';
|
|
11
|
-
export { VldLiteral } from './literal.js';
|
|
12
|
-
export { VldEnum } from './enum.js';
|
|
13
|
-
export { VldTuple } from './tuple.js';
|
|
14
|
-
export { VldRecord } from './record.js';
|
|
15
|
-
export { VldSet } from './set.js';
|
|
16
|
-
export { VldMap } from './map.js';
|
|
17
|
-
export { VldBigInt } from './bigint.js';
|
|
18
|
-
export { VldSymbol } from './symbol.js';
|
|
19
|
-
export { VldAny } from './any.js';
|
|
20
|
-
export { VldUnknown } from './unknown.js';
|
|
21
|
-
export { VldVoid } from './void.js';
|
|
22
|
-
export { VldNever } from './never.js';
|
|
23
|
-
export { VldNull } from './null.js';
|
|
24
|
-
export { VldUndefined } from './undefined.js';
|
|
25
|
-
export { VldNan } from './nan.js';
|
|
26
|
-
export { VldLazy } from './lazy.js';
|
|
27
|
-
export { VldDiscriminatedUnion } from './discriminated-union.js';
|
|
28
|
-
export { VldXor } from './xor.js';
|
|
29
|
-
export { VldJson } from './json.js';
|
|
30
|
-
export { VldTemplateLiteral, templateLiteral } from './template-literal.js';
|
|
31
|
-
export { VldCustom, custom } from './custom.js';
|
|
32
|
-
export { VldFile, file } from './file.js';
|
|
33
|
-
export { VldFunction, functionValidator } from './function.js';
|
|
34
|
-
export { VldPromise, promise } from './promise.js';
|
|
35
|
-
export { base64, base64url, cidrv4, cidrv6, cuid, cuid2, e164, email, emoji, guid, hash, hex, hostname, httpUrl, ipv4, ipv6, iso, jwt, mac, nanoid, regexes, stringFormat, ulid, uuid, uuidv4, xid } from './string-formats.js';
|
|
1
|
+
export{VldBase,VldBrand,VldCatch,VldDefault,VldExactOptional,VldNullable,VldNullish,VldOptional,VldPipe,VldPreprocess,VldReadonly,VldRefine,VldSuperRefine,VldTransform}from"./base.js";export{VldString}from"./string.js";export{VldNumber}from"./number.js";export{VldBoolean}from"./boolean.js";export{VldDate}from"./date.js";export{VldStringBool}from"./string-bool.js";export{VldArray}from"./array.js";export{VldObject}from"./object.js";export{VldUnion}from"./union.js";export{VldIntersection}from"./intersection.js";export{VldLiteral}from"./literal.js";export{VldEnum}from"./enum.js";export{VldTuple}from"./tuple.js";export{VldRecord}from"./record.js";export{VldSet}from"./set.js";export{VldMap}from"./map.js";export{VldBigInt}from"./bigint.js";export{VldSymbol}from"./symbol.js";export{VldAny}from"./any.js";export{VldUnknown}from"./unknown.js";export{VldVoid}from"./void.js";export{VldNever}from"./never.js";export{VldNull}from"./null.js";export{VldUndefined}from"./undefined.js";export{VldNan}from"./nan.js";export{VldLazy}from"./lazy.js";export{VldDiscriminatedUnion}from"./discriminated-union.js";export{VldXor}from"./xor.js";export{VldJson}from"./json.js";export{VldTemplateLiteral,templateLiteral}from"./template-literal.js";export{VldCustom,custom}from"./custom.js";export{VldFile,file}from"./file.js";export{VldFunction,functionValidator}from"./function.js";export{VldPromise,promise}from"./promise.js";export{base64,base64url,cidrv4,cidrv6,cuid,cuid2,e164,email,emoji,guid,hash,hex,hostname,httpUrl,ipv4,ipv6,iso,jwt,ksuid,mac,nanoid,regexes,stringFormat,ulid,url,uuid,uuidv4,uuidv6,uuidv7,xid}from"./string-formats.js";
|
|
@@ -1,116 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { i as isDangerousKey } from '../chunks/security-B0Pd_vSY.js';
|
|
3
|
-
import { getMessages } from '../locales/runtime.js';
|
|
4
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Type guard to check if a value is a plain object
|
|
8
|
-
*/
|
|
9
|
-
function isPlainObject(obj) {
|
|
10
|
-
return (obj !== null &&
|
|
11
|
-
typeof obj === 'object' &&
|
|
12
|
-
obj.constructor === Object &&
|
|
13
|
-
Object.prototype.toString.call(obj) === '[object Object]');
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Safely deep merge two objects without prototype pollution vulnerability
|
|
17
|
-
* Optimized for performance with minimal object allocations
|
|
18
|
-
* BUG-NEW-020 FIX: Use comprehensive dangerous key protection from shared utility
|
|
19
|
-
* @param target The target object
|
|
20
|
-
* @param source The source object to merge from
|
|
21
|
-
* @returns A new merged object
|
|
22
|
-
*/
|
|
23
|
-
function deepMerge(target, source) {
|
|
24
|
-
// Create a new object to avoid mutations (only once)
|
|
25
|
-
const result = { ...target };
|
|
26
|
-
// Get source keys directly for better performance
|
|
27
|
-
const sourceKeys = Object.keys(source);
|
|
28
|
-
for (let i = 0; i < sourceKeys.length; i++) {
|
|
29
|
-
const key = sourceKeys[i];
|
|
30
|
-
// Skip dangerous keys that could lead to prototype pollution
|
|
31
|
-
if (isDangerousKey(key)) {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
// Only process own properties (redundant with Object.keys but safe)
|
|
35
|
-
if (!Object.prototype.hasOwnProperty.call(source, key)) {
|
|
36
|
-
continue;
|
|
37
|
-
}
|
|
38
|
-
const sourceValue = source[key];
|
|
39
|
-
const targetValue = target[key];
|
|
40
|
-
// If both values are plain objects, merge them recursively
|
|
41
|
-
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
42
|
-
result[key] = deepMerge(targetValue, sourceValue);
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
// Otherwise, use the source value (including undefined)
|
|
46
|
-
result[key] = sourceValue;
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
return result;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function createIntersectionError(message) {
|
|
53
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Immutable intersection validator for combining validators
|
|
57
|
-
*/
|
|
58
|
-
class VldIntersection extends VldBase {
|
|
59
|
-
/**
|
|
60
|
-
* Private constructor to enforce immutability
|
|
61
|
-
*/
|
|
62
|
-
constructor(validatorA, validatorB) {
|
|
63
|
-
super(VLD_VALIDATOR_TYPES.INTERSECTION);
|
|
64
|
-
this.validatorA = validatorA;
|
|
65
|
-
this.validatorB = validatorB;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Create a new intersection validator
|
|
69
|
-
*/
|
|
70
|
-
static create(validatorA, validatorB) {
|
|
71
|
-
return new VldIntersection(validatorA, validatorB);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Parse and validate a value against both validators
|
|
75
|
-
*/
|
|
76
|
-
parse(value) {
|
|
77
|
-
try {
|
|
78
|
-
// Both validators must pass
|
|
79
|
-
const resultA = this.validatorA.parse(value);
|
|
80
|
-
const resultB = this.validatorB.parse(value);
|
|
81
|
-
// BUG-NEW-015 FIX: Check type consistency before merging
|
|
82
|
-
const aIsObject = isPlainObject(resultA);
|
|
83
|
-
const bIsObject = isPlainObject(resultB);
|
|
84
|
-
// Both are objects - safe to merge
|
|
85
|
-
if (aIsObject && bIsObject) {
|
|
86
|
-
return deepMerge(resultA, resultB);
|
|
87
|
-
}
|
|
88
|
-
// Neither are objects - must be identical primitives
|
|
89
|
-
if (!aIsObject && !bIsObject) {
|
|
90
|
-
if (resultA === resultB) {
|
|
91
|
-
return resultA;
|
|
92
|
-
}
|
|
93
|
-
throw new Error('Values must be identical for intersection of primitive types');
|
|
94
|
-
}
|
|
95
|
-
// One is object, one is primitive - invalid intersection
|
|
96
|
-
throw new Error('Cannot create intersection of object and primitive types. ' +
|
|
97
|
-
'Both validators must produce the same type category.');
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
throw new Error(getMessages().intersectionError(error.message));
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Safely parse and validate a value against both validators
|
|
105
|
-
*/
|
|
106
|
-
safeParse(value) {
|
|
107
|
-
try {
|
|
108
|
-
return { success: true, data: this.parse(value) };
|
|
109
|
-
}
|
|
110
|
-
catch (error) {
|
|
111
|
-
return { success: false, error: createIntersectionError(error.message) };
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
export { VldIntersection };
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as e}from"./base.js";import{i as r}from"../chunks/security-Dy24T8N7.js";import{getMessages as o}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-CerMg2rq.js";function n(t){return null!==t&&"object"==typeof t&&t.constructor===Object&&"[object Object]"===Object.prototype.toString.call(t)}function c(t,e){const o={...t},s=Object.keys(e);for(let i=0;i<s.length;i++){const a=s[i];if(r(a))continue;if(!Object.prototype.hasOwnProperty.call(e,a))continue;const u=e[a],p=t[a];n(u)&&n(p)?o[a]=c(p,u):o[a]=u}return o}class VldIntersection extends t{constructor(t,r){super(e.INTERSECTION),this.validatorA=t,this.validatorB=r}static create(t,e){return new VldIntersection(t,e)}parse(t){try{const e=this.validatorA.parse(t),r=this.validatorB.parse(t),o=n(e),s=n(r);if(o&&s)return c(e,r);if(!o&&!s){if(e===r)return e;throw new Error("Values must be identical for intersection of primitive types")}throw new Error("Cannot create intersection of object and primitive types. Both validators must produce the same type category.")}catch(t){throw new Error(o().intersectionError(t.message))}}safeParse(t){try{return{success:!0,data:this.parse(t)}}catch(t){return{success:!1,error:(e=t.message,new s([{code:"invalid_type",path:[],message:e}]))}}var e}}export{VldIntersection};
|
package/dist/validators/json.js
CHANGED
|
@@ -1,66 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Immutable JSON validator that validates and parses JSON strings
|
|
6
|
-
* Accepts both strings (which are parsed) and already-parsed objects
|
|
7
|
-
*/
|
|
8
|
-
class VldJson extends VldBase {
|
|
9
|
-
constructor(schema) {
|
|
10
|
-
super(VLD_VALIDATOR_TYPES.JSON);
|
|
11
|
-
this.schema = schema;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Create a new JSON validator
|
|
15
|
-
*/
|
|
16
|
-
static create(schema) {
|
|
17
|
-
return new VldJson(schema);
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Parse and validate a JSON value
|
|
21
|
-
*/
|
|
22
|
-
parse(value) {
|
|
23
|
-
let parsed;
|
|
24
|
-
// If it's a string, try to parse it as JSON first
|
|
25
|
-
if (typeof value === 'string') {
|
|
26
|
-
try {
|
|
27
|
-
parsed = JSON.parse(value);
|
|
28
|
-
}
|
|
29
|
-
catch {
|
|
30
|
-
throw new Error(getMessages().invalidJson);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
// Not a string - assume it's already parsed
|
|
35
|
-
parsed = value;
|
|
36
|
-
}
|
|
37
|
-
// If we have a schema, validate against it
|
|
38
|
-
if (this.schema) {
|
|
39
|
-
return this.schema.parse(parsed);
|
|
40
|
-
}
|
|
41
|
-
// No schema - just return the parsed value
|
|
42
|
-
return parsed;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Safely parse and validate a JSON value
|
|
46
|
-
*/
|
|
47
|
-
safeParse(value) {
|
|
48
|
-
try {
|
|
49
|
-
return { success: true, data: this.parse(value) };
|
|
50
|
-
}
|
|
51
|
-
catch (error) {
|
|
52
|
-
return {
|
|
53
|
-
success: false,
|
|
54
|
-
error: error
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Create a new JSON validator with a schema for validation
|
|
60
|
-
*/
|
|
61
|
-
withSchema(schema) {
|
|
62
|
-
return new VldJson(schema);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export { VldJson };
|
|
1
|
+
import{VldBase as s,VLD_VALIDATOR_TYPES as r,ensureVldError as e}from"./base.js";import{getMessages as t}from"../locales/runtime.js";class VldJson extends s{constructor(s){super(r.JSON),this.schema=s}static create(s){return new VldJson(s)}parse(s){let r;if("string"==typeof s)try{r=JSON.parse(s)}catch{throw new Error(t().invalidJson)}else r=s;return this.schema?this.schema.parse(r):r}safeParse(s){try{return{success:!0,data:this.parse(s)}}catch(s){return{success:!1,error:e(s)}}}withSchema(s){return new VldJson(s)}}export{VldJson};
|