@oxog/vld 2.1.0 → 2.2.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/README.md +1296 -1235
- package/dist/chunks/errors-core-Cqi4-9ew.js +1 -0
- package/dist/chunks/index-CkbfQQ4n.js +1 -0
- package/dist/chunks/index-yjpbvFgw.js +1 -0
- package/dist/chunks/ip-validation-g5LWgy9t.js +1 -0
- package/dist/chunks/security-Dy24T8N7.js +1 -0
- package/dist/cjs/chunks/errors-core-DKPohm9I.cjs +1 -0
- package/dist/cjs/chunks/index-BEV9nG0N.cjs +1 -0
- package/dist/cjs/chunks/index-ZjHAxYdA.cjs +1 -0
- package/dist/cjs/chunks/ip-validation-BZqPamPC.cjs +1 -0
- package/dist/cjs/chunks/security-aXZVI7qW.cjs +1 -0
- package/dist/cjs/cli/bin.cjs +1 -505
- package/dist/cjs/cli/index.cjs +1 -209
- package/dist/cjs/codecs/index.cjs +1 -45
- package/dist/cjs/coercion/index.cjs +1 -588
- package/dist/cjs/compat/emitter.cjs +1 -184
- package/dist/cjs/compat/result.cjs +1 -189
- package/dist/cjs/errors.cjs +1 -180
- package/dist/cjs/index.cjs +1 -1894
- package/dist/cjs/kernel.cjs +1 -415
- package/dist/cjs/locales/af.cjs +1 -108
- package/dist/cjs/locales/ar.cjs +1 -108
- package/dist/cjs/locales/bn.cjs +1 -108
- package/dist/cjs/locales/da.cjs +1 -108
- package/dist/cjs/locales/de.cjs +1 -108
- package/dist/cjs/locales/en.cjs +1 -108
- package/dist/cjs/locales/es-MX.cjs +1 -107
- package/dist/cjs/locales/es.cjs +1 -96
- package/dist/cjs/locales/fi.cjs +1 -108
- package/dist/cjs/locales/fr.cjs +1 -96
- package/dist/cjs/locales/hi.cjs +1 -108
- package/dist/cjs/locales/id.cjs +1 -108
- package/dist/cjs/locales/index.cjs +1 -43
- package/dist/cjs/locales/it.cjs +1 -108
- package/dist/cjs/locales/ja.cjs +1 -108
- package/dist/cjs/locales/ko.cjs +1 -108
- package/dist/cjs/locales/lazy.cjs +1 -188
- package/dist/cjs/locales/nl.cjs +1 -108
- package/dist/cjs/locales/no.cjs +1 -108
- package/dist/cjs/locales/pl.cjs +1 -108
- package/dist/cjs/locales/pt-BR.cjs +1 -108
- package/dist/cjs/locales/pt.cjs +1 -108
- package/dist/cjs/locales/ru.cjs +1 -108
- package/dist/cjs/locales/runtime.cjs +1 -51
- package/dist/cjs/locales/sv.cjs +1 -108
- package/dist/cjs/locales/sw.cjs +1 -108
- package/dist/cjs/locales/th.cjs +1 -108
- package/dist/cjs/locales/tr.cjs +1 -108
- package/dist/cjs/locales/vi.cjs +1 -108
- package/dist/cjs/locales/zh.cjs +1 -108
- package/dist/cjs/mini.cjs +1 -242
- package/dist/cjs/pigment.cjs +1 -207
- package/dist/cjs/plugins/index.cjs +1 -12
- package/dist/cjs/registry.cjs +1 -37
- package/dist/cjs/v3/index.cjs +1 -425
- package/dist/cjs/v4/core/index.cjs +1 -894
- package/dist/cjs/v4/index.cjs +1 -425
- package/dist/cjs/v4/locales/index.cjs +1 -130
- package/dist/cjs/v4/mini/index.cjs +1 -509
- package/dist/cjs/v4-mini/index.cjs +1 -532
- package/dist/cjs/validators/any.cjs +1 -40
- package/dist/cjs/validators/array.cjs +1 -283
- package/dist/cjs/validators/base.cjs +1 -887
- package/dist/cjs/validators/base64.cjs +1 -73
- package/dist/cjs/validators/bigint.cjs +1 -232
- package/dist/cjs/validators/boolean.cjs +1 -146
- package/dist/cjs/validators/codec.cjs +1 -205
- package/dist/cjs/validators/custom.cjs +1 -71
- package/dist/cjs/validators/date.cjs +1 -278
- package/dist/cjs/validators/discriminated-union.cjs +1 -130
- package/dist/cjs/validators/enum.cjs +1 -154
- package/dist/cjs/validators/file.cjs +1 -130
- package/dist/cjs/validators/function.cjs +1 -63
- package/dist/cjs/validators/hex.cjs +1 -70
- package/dist/cjs/validators/index.cjs +1 -123
- package/dist/cjs/validators/intersection.cjs +1 -120
- package/dist/cjs/validators/json.cjs +1 -70
- package/dist/cjs/validators/lazy.cjs +1 -53
- package/dist/cjs/validators/literal.cjs +1 -64
- package/dist/cjs/validators/map.cjs +1 -218
- package/dist/cjs/validators/nan.cjs +1 -41
- package/dist/cjs/validators/never.cjs +1 -36
- package/dist/cjs/validators/null.cjs +1 -43
- package/dist/cjs/validators/number.cjs +1 -427
- package/dist/cjs/validators/object.cjs +1 -868
- package/dist/cjs/validators/promise.cjs +1 -115
- package/dist/cjs/validators/record.cjs +1 -262
- package/dist/cjs/validators/set.cjs +1 -203
- package/dist/cjs/validators/string-bool.cjs +1 -124
- package/dist/cjs/validators/string-formats.cjs +1 -168
- package/dist/cjs/validators/string.cjs +1 -12
- package/dist/cjs/validators/symbol.cjs +1 -62
- package/dist/cjs/validators/template-literal.cjs +1 -108
- package/dist/cjs/validators/tuple.cjs +1 -187
- package/dist/cjs/validators/uint8array.cjs +1 -106
- package/dist/cjs/validators/undefined.cjs +1 -43
- package/dist/cjs/validators/union.cjs +1 -191
- package/dist/cjs/validators/unknown.cjs +1 -40
- package/dist/cjs/validators/void.cjs +1 -48
- package/dist/cjs/validators/xor.cjs +1 -69
- package/dist/cli/bin.js +1 -406
- package/dist/cli/index.js +1 -205
- package/dist/codecs/index.js +1 -11
- package/dist/coercion/index.js +1 -579
- package/dist/compat/emitter.js +1 -180
- package/dist/compat/result.js +1 -170
- package/dist/errors-core.d.ts +20 -1
- package/dist/errors.js +1 -171
- package/dist/index.d.ts +248 -357
- package/dist/index.js +1 -1534
- package/dist/kernel.js +1 -409
- package/dist/locales/af.js +1 -106
- package/dist/locales/ar.js +1 -106
- package/dist/locales/bn.js +1 -106
- package/dist/locales/da.js +1 -106
- package/dist/locales/de.js +1 -106
- package/dist/locales/en.js +1 -106
- package/dist/locales/es-MX.js +1 -105
- package/dist/locales/es.js +1 -94
- package/dist/locales/fi.js +1 -106
- package/dist/locales/fr.js +1 -94
- package/dist/locales/hi.js +1 -106
- package/dist/locales/id.js +1 -106
- package/dist/locales/index.js +1 -29
- package/dist/locales/it.js +1 -106
- package/dist/locales/ja.js +1 -106
- package/dist/locales/ko.js +1 -106
- package/dist/locales/lazy.js +1 -177
- package/dist/locales/nl.js +1 -106
- package/dist/locales/no.js +1 -106
- package/dist/locales/pl.js +1 -106
- package/dist/locales/pt-BR.js +1 -106
- package/dist/locales/pt.js +1 -106
- package/dist/locales/ru.js +1 -106
- package/dist/locales/runtime.js +1 -41
- package/dist/locales/sv.js +1 -106
- package/dist/locales/sw.js +1 -106
- package/dist/locales/th.js +1 -106
- package/dist/locales/tr.js +1 -106
- package/dist/locales/vi.js +1 -106
- package/dist/locales/zh.js +1 -106
- package/dist/mini.d.ts +3 -3
- package/dist/mini.js +1 -156
- package/dist/pigment.js +1 -180
- package/dist/plugins/index.js +1 -1
- package/dist/registry.js +1 -34
- package/dist/utils/json-schema.d.ts +10 -1
- package/dist/v3/index.js +1 -46
- package/dist/v4/core/index.d.ts +28 -16
- package/dist/v4/core/index.js +1 -277
- package/dist/v4/index.js +1 -50
- package/dist/v4/locales/index.d.ts +4 -0
- package/dist/v4/locales/index.js +1 -77
- package/dist/v4/mini/index.js +1 -47
- package/dist/v4-mini/index.d.ts +1 -1
- package/dist/v4-mini/index.js +1 -75
- package/dist/validators/any.js +1 -37
- package/dist/validators/array.d.ts +7 -0
- package/dist/validators/array.js +1 -279
- package/dist/validators/base.d.ts +63 -8
- package/dist/validators/base.js +1 -868
- package/dist/validators/base64.js +1 -68
- package/dist/validators/bigint.js +1 -228
- package/dist/validators/boolean.js +1 -142
- package/dist/validators/codec.js +1 -200
- package/dist/validators/custom.js +1 -67
- package/dist/validators/date.js +1 -274
- package/dist/validators/discriminated-union.d.ts +2 -2
- package/dist/validators/discriminated-union.js +1 -123
- package/dist/validators/enum.js +1 -150
- package/dist/validators/file.js +1 -125
- package/dist/validators/function.js +1 -58
- package/dist/validators/hex.js +1 -65
- package/dist/validators/index.d.ts +1 -1
- package/dist/validators/index.js +1 -35
- package/dist/validators/intersection.js +1 -116
- package/dist/validators/json.js +1 -66
- package/dist/validators/lazy.js +1 -50
- package/dist/validators/literal.d.ts +7 -3
- package/dist/validators/literal.js +1 -60
- package/dist/validators/map.js +1 -214
- package/dist/validators/nan.js +1 -38
- package/dist/validators/never.js +1 -32
- package/dist/validators/null.js +1 -40
- package/dist/validators/number.d.ts +20 -1
- package/dist/validators/number.js +1 -423
- package/dist/validators/object.d.ts +12 -2
- package/dist/validators/object.js +1 -864
- package/dist/validators/promise.js +1 -111
- package/dist/validators/record.d.ts +11 -8
- package/dist/validators/record.js +1 -258
- package/dist/validators/set.js +1 -199
- package/dist/validators/string-bool.js +1 -120
- package/dist/validators/string-formats.d.ts +58 -62
- package/dist/validators/string-formats.js +1 -135
- package/dist/validators/string.d.ts +47 -10
- package/dist/validators/string.js +1 -4
- package/dist/validators/symbol.js +1 -58
- package/dist/validators/template-literal.js +1 -103
- package/dist/validators/tuple.d.ts +17 -13
- package/dist/validators/tuple.js +1 -183
- package/dist/validators/uint8array.js +1 -101
- package/dist/validators/undefined.js +1 -40
- package/dist/validators/union.js +1 -187
- package/dist/validators/unknown.js +1 -37
- package/dist/validators/void.js +1 -44
- package/dist/validators/xor.d.ts +2 -2
- package/dist/validators/xor.js +1 -66
- package/package.json +239 -238
- package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
- package/dist/chunks/index-0R2ntSud.js +0 -508
- package/dist/chunks/index-BvH403Yy.js +0 -88
- package/dist/chunks/security-B0Pd_vSY.js +0 -35
- package/dist/chunks/string-BJRQod3t.js +0 -530
- package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
- package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
- package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
- package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
- package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
|
@@ -1,103 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Immutable template literal validator
|
|
6
|
-
* Validates strings matching a template pattern
|
|
7
|
-
*/
|
|
8
|
-
class VldTemplateLiteral extends VldBase {
|
|
9
|
-
constructor(pattern) {
|
|
10
|
-
super(VLD_VALIDATOR_TYPES.TEMPLATE_LITERAL);
|
|
11
|
-
this.pattern = pattern;
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Create a template literal validator from components
|
|
15
|
-
*/
|
|
16
|
-
static create(...components) {
|
|
17
|
-
// Build regex pattern from components
|
|
18
|
-
let pattern = '^';
|
|
19
|
-
for (const comp of components) {
|
|
20
|
-
if (typeof comp === 'string') {
|
|
21
|
-
// Escape special regex characters
|
|
22
|
-
pattern += comp.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
// Add appropriate pattern based on validator type
|
|
26
|
-
pattern += getPatternForValidator(comp);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
pattern += '$';
|
|
30
|
-
return new VldTemplateLiteral(new RegExp(pattern));
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Parse and validate a template literal string
|
|
34
|
-
*/
|
|
35
|
-
parse(value) {
|
|
36
|
-
if (typeof value !== 'string') {
|
|
37
|
-
throw new Error(getMessages().invalidString);
|
|
38
|
-
}
|
|
39
|
-
if (!this.pattern.test(value)) {
|
|
40
|
-
throw new Error(getMessages().stringPatternInvalid);
|
|
41
|
-
}
|
|
42
|
-
return value;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Safely parse and validate a template literal string
|
|
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
|
-
/**
|
|
60
|
-
* Get regex pattern for a validator type
|
|
61
|
-
*/
|
|
62
|
-
function getPatternForValidator(validator) {
|
|
63
|
-
// For template literals, we need to identify the validator type
|
|
64
|
-
// and return an appropriate capture group pattern
|
|
65
|
-
switch (validator.validatorType) {
|
|
66
|
-
case VLD_VALIDATOR_TYPES.STRING:
|
|
67
|
-
case VLD_VALIDATOR_TYPES.STRING_FORMAT:
|
|
68
|
-
return '(.+)';
|
|
69
|
-
case VLD_VALIDATOR_TYPES.NUMBER:
|
|
70
|
-
case VLD_VALIDATOR_TYPES.COERCE_NUMBER:
|
|
71
|
-
return '(-?\\d+(?:\\.\\d+)?)';
|
|
72
|
-
case VLD_VALIDATOR_TYPES.BIGINT:
|
|
73
|
-
case VLD_VALIDATOR_TYPES.COERCE_BIGINT:
|
|
74
|
-
return '(-?\\d+)';
|
|
75
|
-
case VLD_VALIDATOR_TYPES.BOOLEAN:
|
|
76
|
-
case VLD_VALIDATOR_TYPES.COERCE_BOOLEAN:
|
|
77
|
-
return '(true|false)';
|
|
78
|
-
case VLD_VALIDATOR_TYPES.NULL:
|
|
79
|
-
return '(null)';
|
|
80
|
-
case VLD_VALIDATOR_TYPES.UNDEFINED:
|
|
81
|
-
case VLD_VALIDATOR_TYPES.VOID:
|
|
82
|
-
return '(undefined)';
|
|
83
|
-
case VLD_VALIDATOR_TYPES.LITERAL: {
|
|
84
|
-
const literalValidator = validator;
|
|
85
|
-
if (literalValidator.literal !== undefined) {
|
|
86
|
-
const valueStr = String(literalValidator.literal);
|
|
87
|
-
return '(' + valueStr.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + ')';
|
|
88
|
-
}
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
// Default: match any string
|
|
93
|
-
return '(.+)';
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Helper function to create template literal validators
|
|
97
|
-
* Usage: v.templateLiteral(v.string(), '-', v.number())
|
|
98
|
-
*/
|
|
99
|
-
function templateLiteral(...components) {
|
|
100
|
-
return VldTemplateLiteral.create(...components);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export { VldTemplateLiteral, templateLiteral };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";class VldTemplateLiteral extends e{constructor(e){super(r.TEMPLATE_LITERAL),this.pattern=e}static create(...e){let r="^";for(const t of e)r+="string"==typeof t?t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):a(t);return r+="$",new VldTemplateLiteral(new RegExp(r))}parse(e){if("string"!=typeof e)throw new Error(t().invalidString);if(!this.pattern.test(e))throw new Error(t().stringPatternInvalid);return e}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:e}}}}function a(e){switch(e.validatorType){case r.STRING:case r.STRING_FORMAT:return"(.+)";case r.NUMBER:case r.COERCE_NUMBER:return"(-?\\d+(?:\\.\\d+)?)";case r.BIGINT:case r.COERCE_BIGINT:return"(-?\\d+)";case r.BOOLEAN:case r.COERCE_BOOLEAN:return"(true|false)";case r.NULL:return"(null)";case r.UNDEFINED:case r.VOID:return"(undefined)";case r.LITERAL:{const r=e;if(void 0!==r.literal){return"("+String(r.literal).replace(/[.*+?^${}()|[\]\\]/g,"\\$&")+")"}break}}return"(.+)"}function s(...e){return VldTemplateLiteral.create(...e)}export{VldTemplateLiteral,s as templateLiteral};
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { VldBase, ParseResult } from './base.js';
|
|
2
|
+
type FixedTupleOutput<T extends readonly VldBase<any, any>[]> = {
|
|
3
|
+
-readonly [K in keyof T]: ReturnType<T[K]['parse']>;
|
|
4
|
+
};
|
|
5
|
+
type TupleOutput<T extends readonly VldBase<any, any>[], TRest extends VldBase<any, any> | null> = TRest extends VldBase<any, any> ? [...FixedTupleOutput<T>, ...ReturnType<TRest['parse']>[]] : FixedTupleOutput<T>;
|
|
2
6
|
/**
|
|
3
7
|
* Immutable tuple validator for fixed-length arrays
|
|
4
8
|
*/
|
|
5
|
-
export declare class VldTuple<T extends readonly VldBase<any, any>[]> extends VldBase<unknown, {
|
|
6
|
-
[K in keyof T]: T[K] extends VldBase<any, infer U> ? U : never;
|
|
7
|
-
}> {
|
|
9
|
+
export declare class VldTuple<T extends readonly VldBase<any, any>[], TRest extends VldBase<any, any> | null = null> extends VldBase<unknown, TupleOutput<T, TRest>> {
|
|
8
10
|
private readonly validators;
|
|
9
11
|
private readonly errorMessage?;
|
|
12
|
+
private readonly restValidator;
|
|
10
13
|
/**
|
|
11
14
|
* Private constructor to enforce immutability
|
|
12
15
|
*/
|
|
@@ -21,24 +24,25 @@ export declare class VldTuple<T extends readonly VldBase<any, any>[]> extends Vl
|
|
|
21
24
|
/**
|
|
22
25
|
* Create a new tuple validator
|
|
23
26
|
*/
|
|
24
|
-
static create<T extends readonly VldBase<any, any>[]>(...validators: T): VldTuple<T>;
|
|
27
|
+
static create<T extends readonly VldBase<any, any>[]>(...validators: T): VldTuple<T, null>;
|
|
28
|
+
rest<TSchema extends VldBase<any, any>>(validator: TSchema): VldTuple<T, TSchema>;
|
|
25
29
|
/**
|
|
26
30
|
* Parse and validate a tuple value
|
|
27
31
|
*/
|
|
28
|
-
parse(value: unknown):
|
|
29
|
-
[K in keyof T]: T[K] extends VldBase<any, infer U> ? U : never;
|
|
30
|
-
};
|
|
32
|
+
parse(value: unknown): TupleOutput<T, TRest>;
|
|
31
33
|
/**
|
|
32
34
|
* Parse a value that has already passed the tuple array type guard.
|
|
33
35
|
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
34
36
|
*/
|
|
35
|
-
parseKnownTuple(value: unknown[]):
|
|
36
|
-
[K in keyof T]: T[K] extends VldBase<any, infer U> ? U : never;
|
|
37
|
-
};
|
|
37
|
+
parseKnownTuple(value: unknown[]): TupleOutput<T, TRest>;
|
|
38
38
|
/**
|
|
39
39
|
* Safely parse and validate a tuple value
|
|
40
40
|
*/
|
|
41
|
-
safeParse(value: unknown): ParseResult<
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
safeParse(value: unknown): ParseResult<TupleOutput<T, TRest>>;
|
|
42
|
+
encode(value: TupleOutput<T, TRest>): unknown;
|
|
43
|
+
safeEncode(value: TupleOutput<T, TRest>): ParseResult<unknown>;
|
|
44
|
+
encodeAsync(value: TupleOutput<T, TRest>): Promise<unknown>;
|
|
45
|
+
safeEncodeAsync(value: TupleOutput<T, TRest>): Promise<ParseResult<unknown>>;
|
|
46
|
+
private validateEncodedLength;
|
|
44
47
|
}
|
|
48
|
+
export {};
|
package/dist/validators/tuple.js
CHANGED
|
@@ -1,183 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createTupleError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_array', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable tuple validator for fixed-length arrays
|
|
10
|
-
*/
|
|
11
|
-
class VldTuple extends VldBase {
|
|
12
|
-
constructor(validators, errorMessage) {
|
|
13
|
-
super(VLD_VALIDATOR_TYPES.TUPLE);
|
|
14
|
-
this.validators = validators;
|
|
15
|
-
this.errorMessage = errorMessage;
|
|
16
|
-
this._length = validators.length;
|
|
17
|
-
this._validatorTypes = validators.map(validator => validator.validatorType);
|
|
18
|
-
this._simpleItemModes = validators.map((validator, index) => this.getSimpleItemMode(validator, this._validatorTypes[index]));
|
|
19
|
-
this._simpleItemValues = validators.map((validator, index) => this._simpleItemModes[index] === 'literal' ? validator.literal : undefined);
|
|
20
|
-
}
|
|
21
|
-
get items() {
|
|
22
|
-
return this.validators;
|
|
23
|
-
}
|
|
24
|
-
getSimpleItemMode(validator, type) {
|
|
25
|
-
if (validator.isSimple !== true) {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
switch (type) {
|
|
29
|
-
case VLD_VALIDATOR_TYPES.STRING:
|
|
30
|
-
return 'string';
|
|
31
|
-
case VLD_VALIDATOR_TYPES.NUMBER:
|
|
32
|
-
return 'number';
|
|
33
|
-
case VLD_VALIDATOR_TYPES.BOOLEAN:
|
|
34
|
-
return 'boolean';
|
|
35
|
-
case VLD_VALIDATOR_TYPES.BIGINT:
|
|
36
|
-
return 'bigint';
|
|
37
|
-
case VLD_VALIDATOR_TYPES.SYMBOL:
|
|
38
|
-
return 'symbol';
|
|
39
|
-
case VLD_VALIDATOR_TYPES.NULL:
|
|
40
|
-
return 'null';
|
|
41
|
-
case VLD_VALIDATOR_TYPES.UNDEFINED:
|
|
42
|
-
case VLD_VALIDATOR_TYPES.VOID:
|
|
43
|
-
return 'undefinedValue';
|
|
44
|
-
case VLD_VALIDATOR_TYPES.LITERAL:
|
|
45
|
-
return 'literal';
|
|
46
|
-
case VLD_VALIDATOR_TYPES.ANY:
|
|
47
|
-
case VLD_VALIDATOR_TYPES.UNKNOWN:
|
|
48
|
-
return 'passthrough';
|
|
49
|
-
default:
|
|
50
|
-
return undefined;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
getSimpleItemError(mode, expected, received) {
|
|
54
|
-
switch (mode) {
|
|
55
|
-
case 'string':
|
|
56
|
-
return getMessages().invalidString;
|
|
57
|
-
case 'number':
|
|
58
|
-
return getMessages().invalidNumber;
|
|
59
|
-
case 'boolean':
|
|
60
|
-
return getMessages().invalidBoolean;
|
|
61
|
-
case 'bigint':
|
|
62
|
-
return getMessages().invalidBigint;
|
|
63
|
-
case 'symbol':
|
|
64
|
-
return getMessages().invalidSymbol;
|
|
65
|
-
case 'null':
|
|
66
|
-
return `Expected null, received ${typeof received}`;
|
|
67
|
-
case 'undefinedValue':
|
|
68
|
-
return getMessages().expectedUndefined;
|
|
69
|
-
case 'literal':
|
|
70
|
-
return getMessages().literalExpected(JSON.stringify(expected), JSON.stringify(received));
|
|
71
|
-
default:
|
|
72
|
-
return getMessages().invalidTuple;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Create a new tuple validator
|
|
77
|
-
*/
|
|
78
|
-
static create(...validators) {
|
|
79
|
-
return new VldTuple(validators);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Parse and validate a tuple value
|
|
83
|
-
*/
|
|
84
|
-
parse(value) {
|
|
85
|
-
if (!Array.isArray(value)) {
|
|
86
|
-
throw new Error(this.errorMessage || getMessages().invalidTuple);
|
|
87
|
-
}
|
|
88
|
-
return this.parseKnownTuple(value);
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Parse a value that has already passed the tuple array type guard.
|
|
92
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
93
|
-
*/
|
|
94
|
-
parseKnownTuple(value) {
|
|
95
|
-
if (value.length !== this._length) {
|
|
96
|
-
throw new Error(this.errorMessage ||
|
|
97
|
-
getMessages().tupleLength(this._length, value.length));
|
|
98
|
-
}
|
|
99
|
-
const result = new Array(this._length);
|
|
100
|
-
for (let i = 0; i < this._length; i++) {
|
|
101
|
-
const simpleMode = this._simpleItemModes[i];
|
|
102
|
-
const item = value[i];
|
|
103
|
-
if (simpleMode !== undefined) {
|
|
104
|
-
switch (simpleMode) {
|
|
105
|
-
case 'string':
|
|
106
|
-
if (typeof item !== 'string') {
|
|
107
|
-
throw new Error(getMessages().arrayItem(i, getMessages().invalidString));
|
|
108
|
-
}
|
|
109
|
-
result[i] = item;
|
|
110
|
-
continue;
|
|
111
|
-
case 'number':
|
|
112
|
-
if (typeof item !== 'number' || isNaN(item)) {
|
|
113
|
-
throw new Error(getMessages().arrayItem(i, getMessages().invalidNumber));
|
|
114
|
-
}
|
|
115
|
-
result[i] = item;
|
|
116
|
-
continue;
|
|
117
|
-
case 'boolean':
|
|
118
|
-
if (typeof item !== 'boolean') {
|
|
119
|
-
throw new Error(getMessages().arrayItem(i, getMessages().invalidBoolean));
|
|
120
|
-
}
|
|
121
|
-
result[i] = item;
|
|
122
|
-
continue;
|
|
123
|
-
case 'bigint':
|
|
124
|
-
if (typeof item !== 'bigint') {
|
|
125
|
-
throw new Error(getMessages().arrayItem(i, getMessages().invalidBigint));
|
|
126
|
-
}
|
|
127
|
-
result[i] = item;
|
|
128
|
-
continue;
|
|
129
|
-
case 'symbol':
|
|
130
|
-
if (typeof item !== 'symbol') {
|
|
131
|
-
throw new Error(getMessages().arrayItem(i, getMessages().invalidSymbol));
|
|
132
|
-
}
|
|
133
|
-
result[i] = item;
|
|
134
|
-
continue;
|
|
135
|
-
case 'null':
|
|
136
|
-
if (item !== null) {
|
|
137
|
-
throw new Error(getMessages().arrayItem(i, this.getSimpleItemError(simpleMode, undefined, item)));
|
|
138
|
-
}
|
|
139
|
-
result[i] = null;
|
|
140
|
-
continue;
|
|
141
|
-
case 'undefinedValue':
|
|
142
|
-
if (item !== undefined) {
|
|
143
|
-
throw new Error(getMessages().arrayItem(i, getMessages().expectedUndefined));
|
|
144
|
-
}
|
|
145
|
-
result[i] = undefined;
|
|
146
|
-
continue;
|
|
147
|
-
case 'literal': {
|
|
148
|
-
const literal = this._simpleItemValues[i];
|
|
149
|
-
if (item !== literal) {
|
|
150
|
-
throw new Error(getMessages().arrayItem(i, this.getSimpleItemError(simpleMode, literal, item)));
|
|
151
|
-
}
|
|
152
|
-
result[i] = literal;
|
|
153
|
-
continue;
|
|
154
|
-
}
|
|
155
|
-
case 'passthrough':
|
|
156
|
-
result[i] = item;
|
|
157
|
-
continue;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
try {
|
|
161
|
-
const validator = this.validators[i];
|
|
162
|
-
result[i] = validator.parse(item);
|
|
163
|
-
}
|
|
164
|
-
catch (error) {
|
|
165
|
-
throw new Error(getMessages().arrayItem(i, error.message));
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
return result;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Safely parse and validate a tuple value
|
|
172
|
-
*/
|
|
173
|
-
safeParse(value) {
|
|
174
|
-
try {
|
|
175
|
-
return { success: true, data: this.parse(value) };
|
|
176
|
-
}
|
|
177
|
-
catch (error) {
|
|
178
|
-
return { success: false, error: createTupleError(error.message) };
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
export { VldTuple };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{getMessages as t}from"../locales/runtime.js";import{V as s}from"../chunks/errors-core-Cqi4-9ew.js";function i(e){return new s([{code:"invalid_array",path:[],message:e}])}class VldTuple extends e{constructor(e,t,s=null){super(r.TUPLE),this.validators=e,this.errorMessage=t,this.restValidator=s,this._length=e.length,this._validatorTypes=e.map(e=>e.validatorType),this._simpleItemModes=e.map((e,r)=>this.getSimpleItemMode(e,this._validatorTypes[r])),this._simpleItemValues=e.map((e,r)=>"literal"===this._simpleItemModes[r]?e.literal:void 0)}get items(){return this.validators}getSimpleItemMode(e,t){if(!0===e.isSimple)switch(t){case r.STRING:return"string";case r.NUMBER:return"number";case r.BOOLEAN:return"boolean";case r.BIGINT:return"bigint";case r.SYMBOL:return"symbol";case r.NULL:return"null";case r.UNDEFINED:case r.VOID:return"undefinedValue";case r.LITERAL:return"literal";case r.ANY:case r.UNKNOWN:return"passthrough";default:return}}getSimpleItemError(e,r,s){switch(e){case"string":return t().invalidString;case"number":return t().invalidNumber;case"boolean":return t().invalidBoolean;case"bigint":return t().invalidBigint;case"symbol":return t().invalidSymbol;case"null":return"Expected null, received "+typeof s;case"undefinedValue":return t().expectedUndefined;case"literal":return t().literalExpected(JSON.stringify(r),JSON.stringify(s));default:return t().invalidTuple}}static create(...e){return new VldTuple(e)}rest(e){return new VldTuple(this.validators,this.errorMessage,e)}parse(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);return this.parseKnownTuple(e)}parseKnownTuple(e){if(null===this.restValidator&&e.length!==this._length||null!==this.restValidator&&e.length<this._length)throw new Error(this.errorMessage||t().tupleLength(this._length,e.length));const r=new Array(e.length);for(let s=0;s<this._length;s++){const i=this._simpleItemModes[s],n=e[s];if(void 0!==i)switch(i){case"string":if("string"!=typeof n)throw new Error(t().arrayItem(s,t().invalidString));r[s]=n;continue;case"number":if("number"!=typeof n||isNaN(n))throw new Error(t().arrayItem(s,t().invalidNumber));r[s]=n;continue;case"boolean":if("boolean"!=typeof n)throw new Error(t().arrayItem(s,t().invalidBoolean));r[s]=n;continue;case"bigint":if("bigint"!=typeof n)throw new Error(t().arrayItem(s,t().invalidBigint));r[s]=n;continue;case"symbol":if("symbol"!=typeof n)throw new Error(t().arrayItem(s,t().invalidSymbol));r[s]=n;continue;case"null":if(null!==n)throw new Error(t().arrayItem(s,this.getSimpleItemError(i,void 0,n)));r[s]=null;continue;case"undefinedValue":if(void 0!==n)throw new Error(t().arrayItem(s,t().expectedUndefined));r[s]=void 0;continue;case"literal":{const e=this._simpleItemValues[s];if(n!==e)throw new Error(t().arrayItem(s,this.getSimpleItemError(i,e,n)));r[s]=e;continue}case"passthrough":r[s]=n;continue}try{const e=this.validators[s];r[s]=e.parse(n)}catch(e){throw new Error(t().arrayItem(s,e.message))}}if(this.restValidator)for(let s=this._length;s<e.length;s++)try{r[s]=this.restValidator.parse(e[s])}catch(e){throw new Error(t().arrayItem(s,e.message))}return r}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:i(e.message)}}}encode(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);this.validateEncodedLength(e.length);const r=new Array(e.length);for(let t=0;t<this._length;t++)r[t]=this.validators[t].encode(e[t]);if(this.restValidator)for(let t=this._length;t<e.length;t++)r[t]=this.restValidator.encode(e[t]);return r}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:i(e.message)}}}async encodeAsync(e){if(!Array.isArray(e))throw new Error(this.errorMessage||t().invalidTuple);this.validateEncodedLength(e.length);const r=new Array(e.length);for(let t=0;t<this._length;t++)r[t]=await this.validators[t].encodeAsync(e[t]);if(this.restValidator)for(let t=this._length;t<e.length;t++)r[t]=await this.restValidator.encodeAsync(e[t]);return r}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:i(e.message)}}}validateEncodedLength(e){if(null===this.restValidator&&e!==this._length||null!==this.restValidator&&e<this._length)throw new Error(this.errorMessage||t().tupleLength(this._length,e))}}export{VldTuple};
|
|
@@ -1,101 +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
|
-
* Uint8Array validator with immutable configuration
|
|
8
|
-
* BUG-NEW-010 FIX: Refactored to follow immutability pattern
|
|
9
|
-
*/
|
|
10
|
-
class VldUint8Array extends VldBase {
|
|
11
|
-
/**
|
|
12
|
-
* Protected constructor to maintain immutability
|
|
13
|
-
*/
|
|
14
|
-
constructor(config) {
|
|
15
|
-
super(VLD_VALIDATOR_TYPES.UINT8_ARRAY);
|
|
16
|
-
this.config = config || {};
|
|
17
|
-
}
|
|
18
|
-
parse(value) {
|
|
19
|
-
const result = this.safeParse(value);
|
|
20
|
-
if (!result.success) {
|
|
21
|
-
throw result.error;
|
|
22
|
-
}
|
|
23
|
-
return result.data;
|
|
24
|
-
}
|
|
25
|
-
safeParse(value) {
|
|
26
|
-
if (!(value instanceof Uint8Array)) {
|
|
27
|
-
return {
|
|
28
|
-
success: false,
|
|
29
|
-
error: new VldError([
|
|
30
|
-
createIssue('invalid_type', [], getMessages().expectedUint8Array)
|
|
31
|
-
])
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
// Check exact length if specified
|
|
35
|
-
if (this.config.exactLength !== undefined && value.length !== this.config.exactLength) {
|
|
36
|
-
return {
|
|
37
|
-
success: false,
|
|
38
|
-
error: new VldError([
|
|
39
|
-
createIssue('custom', [], getMessages().uint8ArrayExactLength(this.config.exactLength))
|
|
40
|
-
])
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
// Check minimum length
|
|
44
|
-
if (this.config.minLength !== undefined && value.length < this.config.minLength) {
|
|
45
|
-
return {
|
|
46
|
-
success: false,
|
|
47
|
-
error: new VldError([
|
|
48
|
-
createIssue('custom', [], getMessages().uint8ArrayMinLength(this.config.minLength))
|
|
49
|
-
])
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
// Check maximum length
|
|
53
|
-
if (this.config.maxLength !== undefined && value.length > this.config.maxLength) {
|
|
54
|
-
return {
|
|
55
|
-
success: false,
|
|
56
|
-
error: new VldError([
|
|
57
|
-
createIssue('custom', [], getMessages().uint8ArrayMaxLength(this.config.maxLength))
|
|
58
|
-
])
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
return { success: true, data: value };
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Set minimum byte length
|
|
65
|
-
* Returns new immutable validator instance
|
|
66
|
-
*/
|
|
67
|
-
min(length) {
|
|
68
|
-
return new VldUint8Array({
|
|
69
|
-
...this.config,
|
|
70
|
-
minLength: length
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Set maximum byte length
|
|
75
|
-
* Returns new immutable validator instance
|
|
76
|
-
*/
|
|
77
|
-
max(length) {
|
|
78
|
-
return new VldUint8Array({
|
|
79
|
-
...this.config,
|
|
80
|
-
maxLength: length
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Set exact byte length
|
|
85
|
-
* Returns new immutable validator instance
|
|
86
|
-
* Clears min/max when setting exact length
|
|
87
|
-
*/
|
|
88
|
-
length(length) {
|
|
89
|
-
return new VldUint8Array({
|
|
90
|
-
exactLength: length,
|
|
91
|
-
// Clear min/max when exact length is set
|
|
92
|
-
minLength: undefined,
|
|
93
|
-
maxLength: undefined
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
static create() {
|
|
97
|
-
return new VldUint8Array();
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { VldUint8Array };
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as r}from"./base.js";import{createIssue as n}from"../errors.js";import{getMessages as e}from"../locales/runtime.js";import{V as i}from"../chunks/errors-core-Cqi4-9ew.js";class VldUint8Array extends t{constructor(t){super(r.UINT8_ARRAY),this.config=t||{}}parse(t){const r=this.safeParse(t);if(!r.success)throw r.error;return r.data}safeParse(t){return t instanceof Uint8Array?void 0!==this.config.exactLength&&t.length!==this.config.exactLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayExactLength(this.config.exactLength))])}:void 0!==this.config.minLength&&t.length<this.config.minLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayMinLength(this.config.minLength))])}:void 0!==this.config.maxLength&&t.length>this.config.maxLength?{success:!1,error:new i([n("custom",[],e().uint8ArrayMaxLength(this.config.maxLength))])}:{success:!0,data:t}:{success:!1,error:new i([n("invalid_type",[],e().expectedUint8Array)])}}min(t){return new VldUint8Array({...this.config,minLength:t})}max(t){return new VldUint8Array({...this.config,maxLength:t})}length(t){return new VldUint8Array({exactLength:t,minLength:void 0,maxLength:void 0})}static create(){return new VldUint8Array}}export{VldUint8Array};
|
|
@@ -1,40 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* VldUndefined - Validates that a value is exactly undefined
|
|
6
|
-
* Part of Zod 4 API parity implementation
|
|
7
|
-
*/
|
|
8
|
-
function createUndefinedError(message) {
|
|
9
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Undefined validator - validates that a value is exactly undefined
|
|
13
|
-
*/
|
|
14
|
-
class VldUndefined extends VldBase {
|
|
15
|
-
constructor() {
|
|
16
|
-
super(VLD_VALIDATOR_TYPES.UNDEFINED);
|
|
17
|
-
}
|
|
18
|
-
static create() {
|
|
19
|
-
return new VldUndefined();
|
|
20
|
-
}
|
|
21
|
-
get isSimple() {
|
|
22
|
-
return true;
|
|
23
|
-
}
|
|
24
|
-
parse(value) {
|
|
25
|
-
if (value !== undefined) {
|
|
26
|
-
throw new Error(`Expected undefined, received ${typeof value}`);
|
|
27
|
-
}
|
|
28
|
-
return undefined;
|
|
29
|
-
}
|
|
30
|
-
safeParse(value) {
|
|
31
|
-
try {
|
|
32
|
-
return { success: true, data: this.parse(value) };
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
return { success: false, error: createUndefinedError(error.message) };
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export { VldUndefined };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as r}from"./base.js";import{V as s}from"../chunks/errors-core-Cqi4-9ew.js";class VldUndefined extends e{constructor(){super(r.UNDEFINED)}static create(){return new VldUndefined}get isSimple(){return!0}parse(e){if(void 0!==e)throw new Error("Expected undefined, received "+typeof e)}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return{success:!1,error:(r=e.message,new s([{code:"invalid_type",path:[],message:r}]))}}var r}}export{VldUndefined};
|