@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/set.js
CHANGED
|
@@ -1,199 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createSetError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable Set validator
|
|
10
|
-
*/
|
|
11
|
-
class VldSet extends VldBase {
|
|
12
|
-
/**
|
|
13
|
-
* Private constructor to enforce immutability
|
|
14
|
-
*/
|
|
15
|
-
constructor(itemValidator, errorMessage) {
|
|
16
|
-
super(VLD_VALIDATOR_TYPES.SET);
|
|
17
|
-
this.itemValidator = itemValidator;
|
|
18
|
-
this.errorMessage = errorMessage;
|
|
19
|
-
this._simpleItemMode = this.getSimpleItemMode(itemValidator);
|
|
20
|
-
this._simpleItemValue = this._simpleItemMode === 'literal' ? itemValidator.literal : undefined;
|
|
21
|
-
}
|
|
22
|
-
get itemSchema() {
|
|
23
|
-
return this.itemValidator;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Create a new Set validator
|
|
27
|
-
*/
|
|
28
|
-
static create(itemValidator) {
|
|
29
|
-
return new VldSet(itemValidator);
|
|
30
|
-
}
|
|
31
|
-
getSimpleItemMode(itemValidator) {
|
|
32
|
-
if (itemValidator.isSimple !== true) {
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
switch (itemValidator.validatorType) {
|
|
36
|
-
case VLD_VALIDATOR_TYPES.STRING:
|
|
37
|
-
return 'string';
|
|
38
|
-
case VLD_VALIDATOR_TYPES.NUMBER:
|
|
39
|
-
return 'number';
|
|
40
|
-
case VLD_VALIDATOR_TYPES.BOOLEAN:
|
|
41
|
-
return 'boolean';
|
|
42
|
-
case VLD_VALIDATOR_TYPES.BIGINT:
|
|
43
|
-
return 'bigint';
|
|
44
|
-
case VLD_VALIDATOR_TYPES.SYMBOL:
|
|
45
|
-
return 'symbol';
|
|
46
|
-
case VLD_VALIDATOR_TYPES.NULL:
|
|
47
|
-
return 'null';
|
|
48
|
-
case VLD_VALIDATOR_TYPES.UNDEFINED:
|
|
49
|
-
case VLD_VALIDATOR_TYPES.VOID:
|
|
50
|
-
return 'undefinedValue';
|
|
51
|
-
case VLD_VALIDATOR_TYPES.LITERAL:
|
|
52
|
-
return 'literal';
|
|
53
|
-
case VLD_VALIDATOR_TYPES.ANY:
|
|
54
|
-
case VLD_VALIDATOR_TYPES.UNKNOWN:
|
|
55
|
-
return 'passthrough';
|
|
56
|
-
default:
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
getSimpleItemError(received) {
|
|
61
|
-
switch (this._simpleItemMode) {
|
|
62
|
-
case 'string':
|
|
63
|
-
return getMessages().invalidString;
|
|
64
|
-
case 'number':
|
|
65
|
-
return getMessages().invalidNumber;
|
|
66
|
-
case 'boolean':
|
|
67
|
-
return getMessages().invalidBoolean;
|
|
68
|
-
case 'bigint':
|
|
69
|
-
return getMessages().invalidBigint;
|
|
70
|
-
case 'symbol':
|
|
71
|
-
return getMessages().invalidSymbol;
|
|
72
|
-
case 'null':
|
|
73
|
-
return `Expected null, received ${typeof received}`;
|
|
74
|
-
case 'undefinedValue':
|
|
75
|
-
return getMessages().expectedUndefined;
|
|
76
|
-
case 'literal':
|
|
77
|
-
return getMessages().literalExpected(JSON.stringify(this._simpleItemValue), JSON.stringify(received));
|
|
78
|
-
default:
|
|
79
|
-
return getMessages().invalidSet;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Parse and validate a Set value
|
|
84
|
-
*/
|
|
85
|
-
parse(value) {
|
|
86
|
-
if (!(value instanceof Set)) {
|
|
87
|
-
throw new Error(this.errorMessage || getMessages().invalidSet);
|
|
88
|
-
}
|
|
89
|
-
return this.parseKnownSet(value);
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Parse a value that has already passed the Set instance guard.
|
|
93
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
94
|
-
*/
|
|
95
|
-
parseKnownSet(value) {
|
|
96
|
-
const result = new Set();
|
|
97
|
-
const simpleMode = this._simpleItemMode;
|
|
98
|
-
if (simpleMode === 'string') {
|
|
99
|
-
for (const item of value) {
|
|
100
|
-
if (typeof item !== 'string') {
|
|
101
|
-
throw new Error(this.getSimpleItemError(item));
|
|
102
|
-
}
|
|
103
|
-
result.add(item);
|
|
104
|
-
}
|
|
105
|
-
return result;
|
|
106
|
-
}
|
|
107
|
-
if (simpleMode === 'number') {
|
|
108
|
-
for (const item of value) {
|
|
109
|
-
if (typeof item !== 'number' || isNaN(item)) {
|
|
110
|
-
throw new Error(this.getSimpleItemError(item));
|
|
111
|
-
}
|
|
112
|
-
result.add(item);
|
|
113
|
-
}
|
|
114
|
-
return result;
|
|
115
|
-
}
|
|
116
|
-
if (simpleMode === 'boolean') {
|
|
117
|
-
for (const item of value) {
|
|
118
|
-
if (typeof item !== 'boolean') {
|
|
119
|
-
throw new Error(this.getSimpleItemError(item));
|
|
120
|
-
}
|
|
121
|
-
result.add(item);
|
|
122
|
-
}
|
|
123
|
-
return result;
|
|
124
|
-
}
|
|
125
|
-
if (simpleMode === 'bigint') {
|
|
126
|
-
for (const item of value) {
|
|
127
|
-
if (typeof item !== 'bigint') {
|
|
128
|
-
throw new Error(this.getSimpleItemError(item));
|
|
129
|
-
}
|
|
130
|
-
result.add(item);
|
|
131
|
-
}
|
|
132
|
-
return result;
|
|
133
|
-
}
|
|
134
|
-
if (simpleMode === 'symbol') {
|
|
135
|
-
for (const item of value) {
|
|
136
|
-
if (typeof item !== 'symbol') {
|
|
137
|
-
throw new Error(this.getSimpleItemError(item));
|
|
138
|
-
}
|
|
139
|
-
result.add(item);
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
142
|
-
}
|
|
143
|
-
if (simpleMode === 'null') {
|
|
144
|
-
for (const item of value) {
|
|
145
|
-
if (item !== null) {
|
|
146
|
-
throw new Error(this.getSimpleItemError(item));
|
|
147
|
-
}
|
|
148
|
-
result.add(null);
|
|
149
|
-
}
|
|
150
|
-
return result;
|
|
151
|
-
}
|
|
152
|
-
if (simpleMode === 'undefinedValue') {
|
|
153
|
-
for (const item of value) {
|
|
154
|
-
if (item !== undefined) {
|
|
155
|
-
throw new Error(this.getSimpleItemError(item));
|
|
156
|
-
}
|
|
157
|
-
result.add(undefined);
|
|
158
|
-
}
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
if (simpleMode === 'literal') {
|
|
162
|
-
for (const item of value) {
|
|
163
|
-
if (item !== this._simpleItemValue) {
|
|
164
|
-
throw new Error(this.getSimpleItemError(item));
|
|
165
|
-
}
|
|
166
|
-
result.add(this._simpleItemValue);
|
|
167
|
-
}
|
|
168
|
-
return result;
|
|
169
|
-
}
|
|
170
|
-
if (simpleMode === 'passthrough') {
|
|
171
|
-
for (const item of value) {
|
|
172
|
-
result.add(item);
|
|
173
|
-
}
|
|
174
|
-
return result;
|
|
175
|
-
}
|
|
176
|
-
for (const item of value) {
|
|
177
|
-
try {
|
|
178
|
-
result.add(this.itemValidator.parse(item));
|
|
179
|
-
}
|
|
180
|
-
catch (error) {
|
|
181
|
-
throw new Error(error.message);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
return result;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Safely parse and validate a Set value
|
|
188
|
-
*/
|
|
189
|
-
safeParse(value) {
|
|
190
|
-
try {
|
|
191
|
-
return { success: true, data: this.parse(value) };
|
|
192
|
-
}
|
|
193
|
-
catch (error) {
|
|
194
|
-
return { success: false, error: createSetError(error.message) };
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export { VldSet };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as t}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-CerMg2rq.js";class VldSet extends e{constructor(e,r,s=[]){super(t.SET),this.itemValidator=e,this.errorMessage=r,this._checks=s,this._simpleItemMode=this.getSimpleItemMode(e),this._simpleItemValue="literal"===this._simpleItemMode?e.literal:void 0}get itemSchema(){return this.itemValidator}static create(e){return new VldSet(e)}getSimpleItemMode(e){if(!0===e.isSimple)switch(e.validatorType){case t.STRING:return"string";case t.NUMBER:return"number";case t.BOOLEAN:return"boolean";case t.BIGINT:return"bigint";case t.SYMBOL:return"symbol";case t.NULL:return"null";case t.UNDEFINED:case t.VOID:return"undefinedValue";case t.LITERAL:return"literal";case t.ANY:case t.UNKNOWN:return"passthrough";default:return}}getSimpleItemError(e){switch(this._simpleItemMode){case"string":return r().invalidString;case"number":return r().invalidNumber;case"boolean":return r().invalidBoolean;case"bigint":return r().invalidBigint;case"symbol":return r().invalidSymbol;case"null":return"Expected null, received "+typeof e;case"undefinedValue":return r().expectedUndefined;case"literal":return r().literalExpected(JSON.stringify(this._simpleItemValue),JSON.stringify(e));default:return r().invalidSet}}parse(e){if(!(e instanceof Set))throw new Error(this.errorMessage||r().invalidSet);return this.parseKnownSet(e)}parseKnownSet(e){const t=new Set,r=this._simpleItemMode;if("string"===r)for(const r of e){if("string"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("number"===r)for(const r of e){if("number"!=typeof r||isNaN(r))throw new Error(this.getSimpleItemError(r));t.add(r)}else if("boolean"===r)for(const r of e){if("boolean"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("bigint"===r)for(const r of e){if("bigint"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("symbol"===r)for(const r of e){if("symbol"!=typeof r)throw new Error(this.getSimpleItemError(r));t.add(r)}else if("null"===r)for(const r of e){if(null!==r)throw new Error(this.getSimpleItemError(r));t.add(null)}else if("undefinedValue"===r)for(const r of e){if(void 0!==r)throw new Error(this.getSimpleItemError(r));t.add(void 0)}else if("literal"===r)for(const r of e){if(r!==this._simpleItemValue)throw new Error(this.getSimpleItemError(r));t.add(this._simpleItemValue)}else if("passthrough"===r)for(const r of e)t.add(r);else for(const r of e)try{t.add(this.itemValidator.parse(r))}catch(e){throw new Error(e.message)}for(const e of this._checks)if(!e.fn(t))throw new Error(e.message);return t}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(t=e.message,new s([{code:"invalid_type",path:[],message:t}]))}}var t}min(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size>=e,message:t||`Set must contain at least ${e} elements`}])}max(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size<=e,message:t||`Set must contain at most ${e} elements`}])}size(e,t){return new VldSet(this.itemValidator,this.errorMessage,[...this._checks,{fn:t=>t.size===e,message:t||`Set must contain exactly ${e} elements`}])}nonempty(e){return this.min(1,e||"Set cannot be empty")}}export{VldSet};
|
|
@@ -1,120 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Default truthy and falsy value sets
|
|
6
|
-
*/
|
|
7
|
-
const DEFAULT_TRUTHY = ['true', '1', 'yes', 'on', 'y', 'enabled'];
|
|
8
|
-
const DEFAULT_FALSY = ['false', '0', 'no', 'off', 'n', 'disabled'];
|
|
9
|
-
/**
|
|
10
|
-
* Immutable string boolean validator for flexible boolean parsing
|
|
11
|
-
* Supports string representations like "true", "yes", "on", "1", etc.
|
|
12
|
-
*/
|
|
13
|
-
class VldStringBool extends VldBase {
|
|
14
|
-
constructor(options) {
|
|
15
|
-
super(VLD_VALIDATOR_TYPES.STRING_BOOL);
|
|
16
|
-
this.options = options;
|
|
17
|
-
this.truthyValues = options.truthy ?? DEFAULT_TRUTHY;
|
|
18
|
-
this.falsyValues = options.falsy ?? DEFAULT_FALSY;
|
|
19
|
-
const normalizedTruthy = this.normalizeValues(this.truthyValues);
|
|
20
|
-
const normalizedFalsy = this.normalizeValues(this.falsyValues);
|
|
21
|
-
this.normalizedTruthySet = new Set(normalizedTruthy);
|
|
22
|
-
this.normalizedFalsySet = new Set(normalizedFalsy);
|
|
23
|
-
this.validValuesText = [...normalizedTruthy, ...normalizedFalsy].join(', ');
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Create a new string boolean validator
|
|
27
|
-
*/
|
|
28
|
-
static create(options = {}) {
|
|
29
|
-
return new VldStringBool(options);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Normalize a string value based on case sensitivity setting
|
|
33
|
-
*/
|
|
34
|
-
normalizeValue(value) {
|
|
35
|
-
return this.options.caseSensitive ? value : value.toLowerCase();
|
|
36
|
-
}
|
|
37
|
-
normalizeValues(values) {
|
|
38
|
-
if (this.options.caseSensitive) {
|
|
39
|
-
return values;
|
|
40
|
-
}
|
|
41
|
-
return values.map(value => value.toLowerCase());
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Parse and validate a string boolean value
|
|
45
|
-
*/
|
|
46
|
-
parse(value) {
|
|
47
|
-
// First, ensure we have a string
|
|
48
|
-
if (typeof value !== 'string') {
|
|
49
|
-
// Also accept actual boolean values for convenience
|
|
50
|
-
if (typeof value === 'boolean') {
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
throw new Error(getMessages().stringExpected(typeof value, 'string'));
|
|
54
|
-
}
|
|
55
|
-
const normalized = this.normalizeValue(value);
|
|
56
|
-
// Check if value is in truthy set
|
|
57
|
-
if (this.normalizedTruthySet.has(normalized)) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
// Check if value is in falsy set
|
|
61
|
-
if (this.normalizedFalsySet.has(normalized)) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
// Value is not recognized
|
|
65
|
-
throw new Error(getMessages().stringBoolExpected(this.validValuesText, value));
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Safely parse and validate a string boolean value
|
|
69
|
-
*/
|
|
70
|
-
safeParse(value) {
|
|
71
|
-
try {
|
|
72
|
-
const result = this.parse(value);
|
|
73
|
-
return { success: true, data: result };
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
return {
|
|
77
|
-
success: false,
|
|
78
|
-
error: error
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Create a new validator with custom truthy values
|
|
84
|
-
*/
|
|
85
|
-
withTruthy(values) {
|
|
86
|
-
return new VldStringBool({
|
|
87
|
-
...this.options,
|
|
88
|
-
truthy: values
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Create a new validator with custom falsy values
|
|
93
|
-
*/
|
|
94
|
-
withFalsy(values) {
|
|
95
|
-
return new VldStringBool({
|
|
96
|
-
...this.options,
|
|
97
|
-
falsy: values
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Create a new validator with case-sensitive matching
|
|
102
|
-
*/
|
|
103
|
-
caseSensitive() {
|
|
104
|
-
return new VldStringBool({
|
|
105
|
-
...this.options,
|
|
106
|
-
caseSensitive: true
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Create a new validator with case-insensitive matching (default)
|
|
111
|
-
*/
|
|
112
|
-
caseInsensitive() {
|
|
113
|
-
return new VldStringBool({
|
|
114
|
-
...this.options,
|
|
115
|
-
caseSensitive: false
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export { VldStringBool };
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as e,ensureVldError as s}from"./base.js";import{getMessages as r}from"../locales/runtime.js";const i=["true","1","yes","on","y","enabled"],o=["false","0","no","off","n","disabled"];class VldStringBool extends t{constructor(t){super(e.STRING_BOOL),this.options=t,this.truthyValues=t.truthy??i,this.falsyValues=t.falsy??o;const s=this.normalizeValues(this.truthyValues),r=this.normalizeValues(this.falsyValues);this.normalizedTruthySet=new Set(s),this.normalizedFalsySet=new Set(r),this.validValuesText=[...s,...r].join(", ")}static create(t={}){return new VldStringBool(t)}normalizeValue(t){return this.options.caseSensitive?t:t.toLowerCase()}normalizeValues(t){return this.options.caseSensitive?t:t.map(t=>t.toLowerCase())}parse(t){if("string"!=typeof t){if("boolean"==typeof t)return t;throw new Error(r().stringExpected(typeof t,"string"))}const e=this.normalizeValue(t);if(this.normalizedTruthySet.has(e))return!0;if(this.normalizedFalsySet.has(e))return!1;throw new Error(r().stringBoolExpected(this.validValuesText,t))}safeParse(t){try{return{success:!0,data:this.parse(t)}}catch(t){return{success:!1,error:s(t)}}}withTruthy(t){return new VldStringBool({...this.options,truthy:t})}withFalsy(t){return new VldStringBool({...this.options,falsy:t})}caseSensitive(){return new VldStringBool({...this.options,caseSensitive:!0})}caseInsensitive(){return new VldStringBool({...this.options,caseSensitive:!1})}}export{VldStringBool};
|
|
@@ -5,64 +5,57 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { VldBase } from './base.js';
|
|
7
7
|
import type { ParseResult } from './base.js';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
readonly xid: RegExp;
|
|
42
|
-
readonly guid: RegExp;
|
|
43
|
-
readonly httpUrl: RegExp;
|
|
44
|
-
readonly ksuid: RegExp;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Generic string format validator
|
|
48
|
-
*/
|
|
8
|
+
export type UUIDVersion = 'v1' | 'v2' | 'v3' | 'v4' | 'v5' | 'v6' | 'v7' | 'v8';
|
|
9
|
+
export interface URLFormatOptions {
|
|
10
|
+
hostname?: RegExp;
|
|
11
|
+
normalize?: boolean;
|
|
12
|
+
protocol?: RegExp;
|
|
13
|
+
}
|
|
14
|
+
export interface ISOTimeOptions {
|
|
15
|
+
precision?: number | null;
|
|
16
|
+
}
|
|
17
|
+
export interface ISODateTimeOptions extends ISOTimeOptions {
|
|
18
|
+
local?: boolean;
|
|
19
|
+
offset?: boolean;
|
|
20
|
+
}
|
|
21
|
+
declare function uuidRegex(version?: number): RegExp;
|
|
22
|
+
declare function timeRegex(options?: ISOTimeOptions): RegExp;
|
|
23
|
+
declare function dateTimeRegex(options?: ISODateTimeOptions): RegExp;
|
|
24
|
+
declare function stringRegex(options?: {
|
|
25
|
+
minimum?: number;
|
|
26
|
+
maximum?: number;
|
|
27
|
+
}): RegExp;
|
|
28
|
+
declare function macRegex(delimiter?: string): RegExp;
|
|
29
|
+
type StaticRegexName = 'base64' | 'base64url' | 'bigint' | 'boolean' | 'browserEmail' | 'cidrv4' | 'cidrv6' | 'creditCard' | 'cuid' | 'cuid2' | 'date' | 'domain' | 'duration' | 'e164' | 'email' | 'extendedDuration' | 'guid' | 'hex' | 'hostname' | 'html5Email' | 'httpProtocol' | 'httpUrl' | 'idnEmail' | 'integer' | 'ipv4' | 'ipv6' | 'jwt' | 'ksuid' | 'lowercase' | 'md5' | 'md5_base64' | 'md5_base64url' | 'md5_hex' | 'nanoid' | 'null' | 'number' | 'rfc5322Email' | 'sha1' | 'sha1_base64' | 'sha1_base64url' | 'sha1_hex' | 'sha256' | 'sha256_base64' | 'sha256_base64url' | 'sha256_hex' | 'sha384' | 'sha384_base64' | 'sha384_base64url' | 'sha384_hex' | 'sha512' | 'sha512_base64' | 'sha512_base64url' | 'sha512_hex' | 'ulid' | 'undefined' | 'unicodeEmail' | 'uppercase' | 'uuid4' | 'uuid6' | 'uuid7' | 'xid';
|
|
30
|
+
export type RegexNamespace = Readonly<Record<StaticRegexName, RegExp> & {
|
|
31
|
+
datetime: typeof dateTimeRegex;
|
|
32
|
+
emoji: () => RegExp;
|
|
33
|
+
mac: typeof macRegex;
|
|
34
|
+
string: typeof stringRegex;
|
|
35
|
+
time: typeof timeRegex;
|
|
36
|
+
uuid: typeof uuidRegex;
|
|
37
|
+
}>;
|
|
38
|
+
/** Public regex helpers matching the current Zod 4 namespace. */
|
|
39
|
+
declare const REGEXES: RegexNamespace;
|
|
40
|
+
/** Generic string format validator. */
|
|
49
41
|
export declare class VldStringFormat extends VldBase<unknown, string> {
|
|
50
42
|
readonly _format: string;
|
|
51
|
-
readonly _validator: (
|
|
43
|
+
readonly _validator: (value: string) => boolean;
|
|
52
44
|
readonly _errorMessage?: string | undefined;
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
private readonly _normalize;
|
|
46
|
+
private readonly _patternSource?;
|
|
47
|
+
constructor(_format: string, _validator: (value: string) => boolean, _errorMessage?: string | undefined, _normalize?: (value: string) => string, _patternSource?: string | undefined);
|
|
48
|
+
static create(format: string, validator: (value: string) => boolean, errorMessage?: string, normalize?: (value: string) => string, patternSource?: string): VldStringFormat;
|
|
49
|
+
private _createFormatIssue;
|
|
55
50
|
parse(value: unknown): string;
|
|
56
51
|
safeParse(value: unknown): ParseResult<string>;
|
|
57
52
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Top-level string format validators
|
|
60
|
-
*/
|
|
61
53
|
export declare const email: (options?: {
|
|
62
54
|
pattern?: RegExp;
|
|
63
55
|
}) => VldStringFormat;
|
|
56
|
+
export declare const url: (options?: URLFormatOptions) => VldStringFormat;
|
|
64
57
|
export declare const uuid: (options?: {
|
|
65
|
-
version?:
|
|
58
|
+
version?: UUIDVersion;
|
|
66
59
|
}) => VldStringFormat;
|
|
67
60
|
export declare const uuidv4: () => VldStringFormat;
|
|
68
61
|
export declare const uuidv6: () => VldStringFormat;
|
|
@@ -79,7 +72,9 @@ export declare const cuid2: () => VldStringFormat;
|
|
|
79
72
|
export declare const ulid: () => VldStringFormat;
|
|
80
73
|
export declare const ipv4: () => VldStringFormat;
|
|
81
74
|
export declare const ipv6: () => VldStringFormat;
|
|
82
|
-
export declare const mac: (
|
|
75
|
+
export declare const mac: (options?: {
|
|
76
|
+
delimiter?: string;
|
|
77
|
+
}) => VldStringFormat;
|
|
83
78
|
export declare const cidrv4: () => VldStringFormat;
|
|
84
79
|
export declare const cidrv6: () => VldStringFormat;
|
|
85
80
|
export declare const e164: () => VldStringFormat;
|
|
@@ -88,19 +83,20 @@ export declare const guid: () => VldStringFormat;
|
|
|
88
83
|
export declare const httpUrl: () => VldStringFormat;
|
|
89
84
|
export declare const ksuid: () => VldStringFormat;
|
|
90
85
|
export declare const hash: (algorithm: "md5" | "sha1" | "sha256" | "sha384" | "sha512") => VldStringFormat;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
86
|
+
export declare function isValidCreditCard(input: string): boolean;
|
|
87
|
+
export declare const creditCard: (params?: {
|
|
88
|
+
message?: string;
|
|
89
|
+
}) => VldStringFormat;
|
|
94
90
|
export declare const iso: {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
91
|
+
readonly ZodISODate: typeof VldStringFormat;
|
|
92
|
+
readonly ZodISODateTime: typeof VldStringFormat;
|
|
93
|
+
readonly ZodISODuration: typeof VldStringFormat;
|
|
94
|
+
readonly ZodISOTime: typeof VldStringFormat;
|
|
95
|
+
readonly date: () => VldStringFormat;
|
|
96
|
+
readonly time: (options?: ISOTimeOptions) => VldStringFormat;
|
|
97
|
+
readonly datetime: (options?: ISODateTimeOptions) => VldStringFormat;
|
|
98
|
+
readonly dateTime: (options?: ISODateTimeOptions) => VldStringFormat;
|
|
99
|
+
readonly duration: () => VldStringFormat;
|
|
101
100
|
};
|
|
102
|
-
|
|
103
|
-
* Custom format validator
|
|
104
|
-
*/
|
|
105
|
-
export declare const stringFormat: (name: string, validator: ((val: string) => boolean) | RegExp) => VldStringFormat;
|
|
101
|
+
export declare const stringFormat: (name: string, validator: ((value: string) => boolean) | RegExp) => VldStringFormat;
|
|
106
102
|
export { REGEXES as regexes };
|
|
@@ -1,135 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Top-level string format validators
|
|
6
|
-
* Part of Zod 4 API parity implementation
|
|
7
|
-
* Provides convenient validators for common string formats
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Pre-compiled regex patterns for performance
|
|
11
|
-
*/
|
|
12
|
-
const REGEXES = {
|
|
13
|
-
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
14
|
-
uuid: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
15
|
-
uuidv4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
16
|
-
uuidv6: /^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
17
|
-
uuidv7: /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
18
|
-
hostname: /^(?=.{1,253}$)(?:(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$/,
|
|
19
|
-
emoji: /^(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200D(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*$/u,
|
|
20
|
-
base64: /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
|
|
21
|
-
base64url: /^[A-Za-z0-9_-]*$/,
|
|
22
|
-
hex: /^[0-9a-fA-F]*$/,
|
|
23
|
-
jwt: /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,
|
|
24
|
-
nanoid: /^[A-Za-z0-9_-]{21}$/,
|
|
25
|
-
cuid: /^c[^\s-]{8,}$/i,
|
|
26
|
-
cuid2: /^[0-9a-z]+$/,
|
|
27
|
-
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
28
|
-
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
|
|
29
|
-
ipv6: /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
|
|
30
|
-
mac: /^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$/,
|
|
31
|
-
cidrv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/,
|
|
32
|
-
cidrv6: /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^(?:[0-9a-fA-F]{1,4}:){1,7}:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
|
|
33
|
-
e164: /^\+[1-9]\d{1,14}$/,
|
|
34
|
-
isoDate: /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/,
|
|
35
|
-
isoTime: /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$/,
|
|
36
|
-
isoDateTime: /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z?$/,
|
|
37
|
-
md5: /^[a-f0-9]{32}$/i,
|
|
38
|
-
sha1: /^[a-f0-9]{40}$/i,
|
|
39
|
-
sha256: /^[a-f0-9]{64}$/i,
|
|
40
|
-
sha384: /^[a-f0-9]{96}$/i,
|
|
41
|
-
sha512: /^[a-f0-9]{128}$/i,
|
|
42
|
-
// Additional formats (Zod v4 parity)
|
|
43
|
-
xid: /^[A-HJKMNP-TV-Z0-9]{20}$/, // ksort's XID format
|
|
44
|
-
guid: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i, // Alias for UUID
|
|
45
|
-
httpUrl: /^https?:\/\/[^\s/$.?#].[^\s]*$/, // HTTP/HTTPS URL
|
|
46
|
-
ksuid: /^[0-9A-Za-z]{27}$/,
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Generic string format validator
|
|
50
|
-
*/
|
|
51
|
-
class VldStringFormat extends VldBase {
|
|
52
|
-
constructor(_format, _validator, _errorMessage) {
|
|
53
|
-
super(VLD_VALIDATOR_TYPES.STRING_FORMAT);
|
|
54
|
-
this._format = _format;
|
|
55
|
-
this._validator = _validator;
|
|
56
|
-
this._errorMessage = _errorMessage;
|
|
57
|
-
}
|
|
58
|
-
static create(format, validator, errorMessage) {
|
|
59
|
-
return new VldStringFormat(format, validator, errorMessage);
|
|
60
|
-
}
|
|
61
|
-
parse(value) {
|
|
62
|
-
if (typeof value !== 'string') {
|
|
63
|
-
throw new Error(getMessages().invalidString);
|
|
64
|
-
}
|
|
65
|
-
if (!this._validator(value)) {
|
|
66
|
-
throw new Error(this._errorMessage || `Invalid ${this._format} format`);
|
|
67
|
-
}
|
|
68
|
-
return value;
|
|
69
|
-
}
|
|
70
|
-
safeParse(value) {
|
|
71
|
-
if (typeof value !== 'string') {
|
|
72
|
-
return {
|
|
73
|
-
success: false,
|
|
74
|
-
error: new Error(getMessages().invalidString)
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
if (!this._validator(value)) {
|
|
78
|
-
return {
|
|
79
|
-
success: false,
|
|
80
|
-
error: new Error(this._errorMessage || `Invalid ${this._format} format`)
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
return { success: true, data: value };
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Top-level string format validators
|
|
88
|
-
*/
|
|
89
|
-
const email = (options) => VldStringFormat.create('email', (val) => (options?.pattern ?? REGEXES.email).test(val));
|
|
90
|
-
const uuid = (options) => {
|
|
91
|
-
const pattern = options?.version ? REGEXES[`uuid${options.version}`] : REGEXES.uuid;
|
|
92
|
-
return VldStringFormat.create('uuid', (val) => pattern.test(val));
|
|
93
|
-
};
|
|
94
|
-
const uuidv4 = () => VldStringFormat.create('uuid', (val) => REGEXES.uuidv4.test(val));
|
|
95
|
-
const uuidv6 = () => VldStringFormat.create('uuid', (val) => REGEXES.uuidv6.test(val));
|
|
96
|
-
const uuidv7 = () => VldStringFormat.create('uuid', (val) => REGEXES.uuidv7.test(val));
|
|
97
|
-
const hostname = () => VldStringFormat.create('hostname', (val) => REGEXES.hostname.test(val));
|
|
98
|
-
const emoji = () => VldStringFormat.create('emoji', (val) => REGEXES.emoji.test(val));
|
|
99
|
-
const base64 = () => VldStringFormat.create('base64', (val) => REGEXES.base64.test(val));
|
|
100
|
-
const base64url = () => VldStringFormat.create('base64url', (val) => REGEXES.base64url.test(val));
|
|
101
|
-
const hex = () => VldStringFormat.create('hex', (val) => REGEXES.hex.test(val));
|
|
102
|
-
const jwt = () => VldStringFormat.create('jwt', (val) => REGEXES.jwt.test(val));
|
|
103
|
-
const nanoid = () => VldStringFormat.create('nanoid', (val) => REGEXES.nanoid.test(val));
|
|
104
|
-
const cuid = () => VldStringFormat.create('cuid', (val) => REGEXES.cuid.test(val));
|
|
105
|
-
const cuid2 = () => VldStringFormat.create('cuid2', (val) => REGEXES.cuid2.test(val));
|
|
106
|
-
const ulid = () => VldStringFormat.create('ulid', (val) => REGEXES.ulid.test(val));
|
|
107
|
-
const ipv4 = () => VldStringFormat.create('ipv4', (val) => REGEXES.ipv4.test(val));
|
|
108
|
-
const ipv6 = () => VldStringFormat.create('ipv6', (val) => REGEXES.ipv6.test(val));
|
|
109
|
-
const mac = () => VldStringFormat.create('mac', (val) => REGEXES.mac.test(val));
|
|
110
|
-
const cidrv4 = () => VldStringFormat.create('cidrv4', (val) => REGEXES.cidrv4.test(val));
|
|
111
|
-
const cidrv6 = () => VldStringFormat.create('cidrv6', (val) => REGEXES.cidrv6.test(val));
|
|
112
|
-
const e164 = () => VldStringFormat.create('e164', (val) => REGEXES.e164.test(val));
|
|
113
|
-
const xid = () => VldStringFormat.create('xid', (val) => REGEXES.xid.test(val));
|
|
114
|
-
const guid = () => VldStringFormat.create('guid', (val) => REGEXES.guid.test(val));
|
|
115
|
-
const httpUrl = () => VldStringFormat.create('httpUrl', (val) => REGEXES.httpUrl.test(val));
|
|
116
|
-
const ksuid = () => VldStringFormat.create('ksuid', (val) => REGEXES.ksuid.test(val));
|
|
117
|
-
const hash = (algorithm) => VldStringFormat.create('hash', (val) => REGEXES[algorithm]?.test(val) ?? false, `Invalid ${algorithm} hash`);
|
|
118
|
-
/**
|
|
119
|
-
* ISO date/time validators
|
|
120
|
-
*/
|
|
121
|
-
const iso = {
|
|
122
|
-
date: () => VldStringFormat.create('date', (val) => REGEXES.isoDate.test(val)),
|
|
123
|
-
time: () => VldStringFormat.create('time', (val) => REGEXES.isoTime.test(val)),
|
|
124
|
-
dateTime: (_options) => VldStringFormat.create('datetime', (val) => REGEXES.isoDateTime.test(val)),
|
|
125
|
-
duration: () => VldStringFormat.create('duration', (val) => /^-?P(?!$)(?:\d+(?:\.\d+)?Y)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?W)?(?:\d+(?:\.\d+)?D)?(?:T(?=\d)(?:\d+(?:\.\d+)?H)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?S)?)?$/.test(val)),
|
|
126
|
-
};
|
|
127
|
-
/**
|
|
128
|
-
* Custom format validator
|
|
129
|
-
*/
|
|
130
|
-
const stringFormat = (name, validator) => {
|
|
131
|
-
const fn = validator instanceof RegExp ? (val) => validator.test(val) : validator;
|
|
132
|
-
return VldStringFormat.create(name, fn);
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
export { VldStringFormat, base64, base64url, cidrv4, cidrv6, cuid, cuid2, e164, email, emoji, guid, hash, hex, hostname, httpUrl, ipv4, ipv6, iso, jwt, ksuid, mac, nanoid, REGEXES as regexes, stringFormat, ulid, uuid, uuidv4, uuidv6, uuidv7, xid };
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as a}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{i as e}from"../chunks/ip-validation-g5LWgy9t.js";import{V as i,c as n,g as d}from"../chunks/errors-core-CerMg2rq.js";const o="(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))";function s(t,a){return t.lastIndex=0,t.test(a)}function c(t,a){return new RegExp(`^[A-Za-z0-9+/]{${t}}${a}$`)}function u(t){return new RegExp(`^[A-Za-z0-9_-]{${t}}$`)}function m(t){const a="(?:[01]\\d|2[0-3]):[0-5]\\d";return"number"!=typeof t.precision?`${a}(?::[0-5]\\d(?:\\.\\d+)?)?`:-1===t.precision?a:0===t.precision?`${a}:[0-5]\\d`:`${a}:[0-5]\\d\\.\\d{${t.precision}}`}function f(t){return void 0===t?/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/:new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${t}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)}const l={cuid:/^[cC][0-9a-z]{6,}$/,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/,xid:/^[0-9a-vA-V]{20}$/,ksuid:/^[A-Za-z0-9]{27}$/,nanoid:/^[a-zA-Z0-9_-]{21}$/,duration:/^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/,extendedDuration:/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/,guid:/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/,creditCard:/^\d(?:[ -]?\d){11,18}$/,uuid:f,uuid4:f(4),uuid6:f(6),uuid7:f(7),email:/^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+.-]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9-]*\.)+[A-Za-z]{2,}$/,html5Email:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,rfc5322Email:/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}))$/,unicodeEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,idnEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,browserEmail:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,emoji:()=>/^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,ipv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/,mac:function(t=":"){const a=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`^(?:[0-9A-F]{2}${a}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${a}){5}[0-9a-f]{2}$`)},cidrv4:/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/,cidrv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,base64:/^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/,base64url:/^[A-Za-z0-9_-]*$/,hostname:/^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/,domain:/^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,httpProtocol:/^https?$/,e164:/^\+[1-9]\d{6,14}$/,date:new RegExp(`^${o}$`),time:function(t={}){return new RegExp(`^${m(t)}$`)},datetime:function(t={}){const a=["Z"];return t.local&&a.push(""),t.offset&&a.push("([+-](?:[01]\\d|2[0-3]):[0-5]\\d)"),new RegExp(`^${o}T${m(t)}(?:${a.join("|")})$`)},string:function(t){const a=t?`[\\s\\S]{${t.minimum??0},${t.maximum??""}}`:"[\\s\\S]*";return new RegExp(`^${a}$`)},bigint:/^-?\d+n?$/,integer:/^-?\d+$/,number:/^-?\d+(?:\.\d+)?$/,boolean:/^(?:true|false)$/i,null:/^null$/i,undefined:/^undefined$/i,lowercase:/^[^A-Z]*$/,uppercase:/^[^a-z]*$/,hex:/^[0-9a-fA-F]*$/,md5_hex:/^[0-9a-fA-F]{32}$/,md5_base64:c(22,"=="),md5_base64url:u(22),sha1_hex:/^[0-9a-fA-F]{40}$/,sha1_base64:c(27,"="),sha1_base64url:u(27),sha256_hex:/^[0-9a-fA-F]{64}$/,sha256_base64:c(43,"="),sha256_base64url:u(43),sha384_hex:/^[0-9a-fA-F]{96}$/,sha384_base64:c(64,""),sha384_base64url:u(64),sha512_hex:/^[0-9a-fA-F]{128}$/,sha512_base64:c(86,"=="),sha512_base64url:u(86),jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,httpUrl:/^https?:\/\/[^\s/$.?#].[^\s]*$/,md5:/^[a-f0-9]{32}$/i,sha1:/^[a-f0-9]{40}$/i,sha256:/^[a-f0-9]{64}$/i,sha384:/^[a-f0-9]{96}$/i,sha512:/^[a-f0-9]{128}$/i};class VldStringFormat extends t{constructor(t,r,e,i=t=>t,n){super(a.STRING_FORMAT),this._format=t,this._validator=r,this._errorMessage=e,this._normalize=i,this._patternSource=n}static create(t,a,r,e,i){return new VldStringFormat(t,a,r,e,i)}_createFormatIssue(){const t={code:"invalid_format",path:[],origin:"string",format:this._format,message:this._errorMessage||`Invalid ${this._format}`};return this._patternSource&&(t.pattern=this._patternSource),new i([t])}parse(t){if("string"!=typeof t)throw new i([n("string",d(t),r().invalidString)]);if(!this._validator(t))throw this._createFormatIssue();return this._normalize(t)}safeParse(t){return"string"!=typeof t?{success:!1,error:new i([n("string",d(t),r().invalidString)])}:this._validator(t)?{success:!0,data:this._normalize(t)}:{success:!1,error:this._createFormatIssue()}}}const A=t=>{const a=t?.pattern??l.email;return VldStringFormat.create("email",t=>s(a,t),void 0,void 0,a.source)},$=(t={})=>VldStringFormat.create("url",a=>{const r=(t=>{try{return new URL(t)}catch{return}})(a);return void 0!==r&&(!t.hostname||s(t.hostname,r.hostname))&&(!t.protocol||s(t.protocol,r.protocol.slice(0,-1)))},void 0,t.normalize?t=>new URL(t).href:void 0),F=t=>{const a=f(void 0===t?.version?void 0:Number(t.version.slice(1)));return VldStringFormat.create("uuid",t=>s(a,t))},g=()=>VldStringFormat.create("uuid",t=>s(l.uuid4,t)),h=()=>VldStringFormat.create("uuid",t=>s(l.uuid6,t)),p=()=>VldStringFormat.create("uuid",t=>s(l.uuid7,t)),S=()=>VldStringFormat.create("hostname",t=>s(l.hostname,t)),z=()=>VldStringFormat.create("emoji",t=>s(l.emoji(),t)),Z=()=>VldStringFormat.create("base64",t=>s(l.base64,t)),_=()=>VldStringFormat.create("base64url",t=>s(l.base64url,t)),V=()=>VldStringFormat.create("hex",t=>s(l.hex,t)),v=()=>VldStringFormat.create("jwt",t=>s(l.jwt,t)),x=()=>VldStringFormat.create("nanoid",t=>s(l.nanoid,t)),b=()=>VldStringFormat.create("cuid",t=>s(l.cuid,t)),w=()=>VldStringFormat.create("cuid2",t=>s(l.cuid2,t)),E=()=>VldStringFormat.create("ulid",t=>s(l.ulid,t)),j=()=>VldStringFormat.create("ipv4",t=>s(l.ipv4,t)),I=()=>VldStringFormat.create("ipv6",t=>s(l.ipv6,t)),R=t=>{const a=l.mac(t?.delimiter);return VldStringFormat.create("mac",t=>s(a,t))},M=()=>VldStringFormat.create("cidrv4",t=>s(l.cidrv4,t)),T=()=>VldStringFormat.create("cidrv6",t=>{const a=t.lastIndexOf("/");if(a<=0)return!1;const r=Number(t.slice(a+1));return Number.isInteger(r)&&r>=0&&r<=128&&e(t.slice(0,a))}),D=()=>VldStringFormat.create("e164",t=>s(l.e164,t)),k=()=>VldStringFormat.create("xid",t=>s(l.xid,t)),C=()=>VldStringFormat.create("guid",t=>s(l.guid,t)),O=()=>VldStringFormat.create("httpUrl",t=>s(l.httpUrl,t)),P=()=>VldStringFormat.create("ksuid",t=>s(l.ksuid,t)),N=t=>VldStringFormat.create("hash",a=>l[t]?.test(a)??!1,`Invalid ${t} hash`),U=/[ -]/g;function y(t){return!!l.creditCard.test(t)&&function(t){let a=0,r=!1;for(let e=t.length-1;e>=0;e--){let i=t.charCodeAt(e)-48;r&&(i*=2,i>9&&(i-=9)),a+=i,r=!r}return a%10==0}(t.replace(U,""))}const H=t=>VldStringFormat.create("credit_card",y,t?.message,void 0,l.creditCard.source),W={ZodISODate:VldStringFormat,ZodISODateTime:VldStringFormat,ZodISODuration:VldStringFormat,ZodISOTime:VldStringFormat,date:()=>VldStringFormat.create("date",t=>s(l.date,t)),time:(t={})=>{const a=l.time(t);return VldStringFormat.create("time",t=>s(a,t))},datetime:(t={})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},dateTime:(t={local:!0})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},duration:()=>VldStringFormat.create("duration",t=>s(l.duration,t))},Y=(t,a)=>{const r=a instanceof RegExp?t=>s(a,t):a;return VldStringFormat.create(t,r)};export{VldStringFormat,Z as base64,_ as base64url,M as cidrv4,T as cidrv6,H as creditCard,b as cuid,w as cuid2,D as e164,A as email,z as emoji,C as guid,N as hash,V as hex,S as hostname,O as httpUrl,j as ipv4,I as ipv6,y as isValidCreditCard,W as iso,v as jwt,P as ksuid,R as mac,x as nanoid,l as regexes,Y as stringFormat,E as ulid,$ as url,F as uuid,g as uuidv4,h as uuidv6,p as uuidv7,k as xid};
|