@oxog/vld 1.4.0 → 2.0.1
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 +449 -333
- package/README.md +1171 -1130
- package/dist/chunks/bigint-ZKkWLdmi.js +1628 -0
- package/dist/chunks/bigint-ZKkWLdmi.js.map +1 -0
- package/dist/{coercion/string.js → chunks/date-7Iz2BGsL.js} +271 -5
- package/dist/chunks/date-7Iz2BGsL.js.map +1 -0
- package/dist/chunks/index-BoV-4S1F.js +894 -0
- package/dist/chunks/index-BoV-4S1F.js.map +1 -0
- package/dist/chunks/index-eWXdjyyy.js +426 -0
- package/dist/chunks/index-eWXdjyyy.js.map +1 -0
- package/dist/chunks/json-nIhyRPOU.js +1934 -0
- package/dist/chunks/json-nIhyRPOU.js.map +1 -0
- package/dist/{validators/unknown.js → chunks/unknown-CjSRcELI.js} +6 -3
- package/dist/chunks/unknown-CjSRcELI.js.map +1 -0
- package/dist/cjs/errors.cjs +387 -0
- package/dist/cjs/errors.cjs.map +1 -0
- package/dist/cjs/index.cjs +9815 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/locales/index.cjs +2877 -0
- package/dist/cjs/locales/index.cjs.map +1 -0
- package/dist/cjs/locales/lazy.cjs +3169 -0
- package/dist/cjs/locales/lazy.cjs.map +1 -0
- package/dist/cjs/mini.cjs +7161 -0
- package/dist/cjs/mini.cjs.map +1 -0
- package/dist/cli/bin.d.ts +8 -0
- package/dist/cli/bin.d.ts.map +1 -0
- package/dist/cli/commands/benchmark.d.ts +11 -0
- package/dist/cli/commands/benchmark.d.ts.map +1 -0
- package/dist/cli/commands/validate.d.ts +11 -0
- package/dist/cli/commands/validate.d.ts.map +1 -0
- package/dist/cli/index.d.ts +59 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/codecs/index.d.ts.map +1 -1
- package/dist/codecs/index.js +4 -256
- package/dist/codecs/index.js.map +1 -1
- package/dist/coercion/boolean.d.ts.map +1 -1
- package/dist/coercion/index.d.ts +11 -0
- package/dist/coercion/index.d.ts.map +1 -0
- package/dist/coercion/{bigint.js → index.js} +7 -4
- package/dist/coercion/index.js.map +1 -0
- package/dist/compat/emitter.d.ts +73 -0
- package/dist/compat/emitter.d.ts.map +1 -0
- package/dist/compat/emitter.js +181 -0
- package/dist/compat/emitter.js.map +1 -0
- package/dist/compat/index.d.ts +15 -0
- package/dist/compat/index.d.ts.map +1 -0
- package/dist/compat/result.d.ts +128 -0
- package/dist/compat/result.d.ts.map +1 -0
- package/dist/compat/result.js +171 -0
- package/dist/compat/result.js.map +1 -0
- package/dist/errors.d.ts +58 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +106 -15
- package/dist/errors.js.map +1 -1
- package/dist/events.d.ts +140 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/index.d.ts +13 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +230 -103
- package/dist/index.js.map +1 -1
- package/dist/kernel.d.ts +29 -0
- package/dist/kernel.d.ts.map +1 -0
- package/dist/kernel.js +405 -0
- package/dist/kernel.js.map +1 -0
- package/dist/locales/af.js +4 -2
- package/dist/locales/af.js.map +1 -1
- package/dist/locales/ar.js +4 -2
- package/dist/locales/ar.js.map +1 -1
- package/dist/locales/bn.js +4 -2
- package/dist/locales/bn.js.map +1 -1
- package/dist/locales/da.js +4 -2
- package/dist/locales/da.js.map +1 -1
- package/dist/locales/de.js +4 -2
- package/dist/locales/de.js.map +1 -1
- package/dist/locales/en.js +4 -2
- package/dist/locales/en.js.map +1 -1
- package/dist/locales/es-MX.js +4 -2
- package/dist/locales/es-MX.js.map +1 -1
- package/dist/locales/es.js +4 -2
- package/dist/locales/es.js.map +1 -1
- package/dist/locales/fi.js +4 -2
- package/dist/locales/fi.js.map +1 -1
- package/dist/locales/fr.js +4 -2
- package/dist/locales/fr.js.map +1 -1
- package/dist/locales/hi.js +4 -2
- package/dist/locales/hi.js.map +1 -1
- package/dist/locales/id.js +4 -2
- package/dist/locales/id.js.map +1 -1
- package/dist/locales/index.js +3 -91
- package/dist/locales/index.js.map +1 -1
- package/dist/locales/it.js +4 -2
- package/dist/locales/it.js.map +1 -1
- package/dist/locales/ja.js +4 -2
- package/dist/locales/ja.js.map +1 -1
- package/dist/locales/ko.js +4 -2
- package/dist/locales/ko.js.map +1 -1
- package/dist/locales/lazy.d.ts +111 -0
- package/dist/locales/lazy.d.ts.map +1 -0
- package/dist/locales/lazy.js +220 -0
- package/dist/locales/lazy.js.map +1 -0
- package/dist/locales/nl.js +4 -2
- package/dist/locales/nl.js.map +1 -1
- package/dist/locales/no.js +4 -2
- package/dist/locales/no.js.map +1 -1
- package/dist/locales/pl.js +4 -2
- package/dist/locales/pl.js.map +1 -1
- package/dist/locales/pt-BR.js +4 -2
- package/dist/locales/pt-BR.js.map +1 -1
- package/dist/locales/pt.js +4 -2
- package/dist/locales/pt.js.map +1 -1
- package/dist/locales/ru.js +4 -2
- package/dist/locales/ru.js.map +1 -1
- package/dist/locales/sv.js +4 -2
- package/dist/locales/sv.js.map +1 -1
- package/dist/locales/sw.js +4 -2
- package/dist/locales/sw.js.map +1 -1
- package/dist/locales/th.js +4 -2
- package/dist/locales/th.js.map +1 -1
- package/dist/locales/tr.js +4 -2
- package/dist/locales/tr.js.map +1 -1
- package/dist/locales/vi.js +4 -2
- package/dist/locales/vi.js.map +1 -1
- package/dist/locales/zh.js +4 -2
- package/dist/locales/zh.js.map +1 -1
- package/dist/logger.d.ts +94 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/mini.d.ts +182 -0
- package/dist/mini.d.ts.map +1 -0
- package/dist/mini.js +132 -0
- package/dist/mini.js.map +1 -0
- package/dist/pigment.d.ts +124 -0
- package/dist/pigment.d.ts.map +1 -0
- package/dist/pigment.js +178 -0
- package/dist/pigment.js.map +1 -0
- package/dist/plugins/index.d.ts +8 -0
- package/dist/plugins/index.d.ts.map +1 -0
- package/dist/plugins/types.d.ts +186 -0
- package/dist/plugins/types.d.ts.map +1 -0
- package/dist/utils/codec-utils.d.ts.map +1 -1
- package/dist/utils/ip-validation.d.ts.map +1 -1
- package/dist/validators/base.d.ts +3 -0
- package/dist/validators/base.d.ts.map +1 -1
- package/dist/validators/index.js +5 -45
- package/dist/validators/index.js.map +1 -1
- package/dist/validators/object.d.ts.map +1 -1
- package/dist/validators/template-literal.d.ts.map +1 -1
- package/package.json +153 -79
- package/dist/coercion/bigint.js.map +0 -1
- package/dist/coercion/boolean.js +0 -67
- package/dist/coercion/boolean.js.map +0 -1
- package/dist/coercion/date.js +0 -52
- package/dist/coercion/date.js.map +0 -1
- package/dist/coercion/number.js +0 -160
- package/dist/coercion/number.js.map +0 -1
- package/dist/coercion/string.js.map +0 -1
- package/dist/locales/backup-en.js +0 -105
- package/dist/locales/backup-en.js.map +0 -1
- package/dist/locales/types.js +0 -2
- package/dist/locales/types.js.map +0 -1
- package/dist/utils/codec-utils.js +0 -223
- package/dist/utils/codec-utils.js.map +0 -1
- package/dist/utils/deep-merge.js +0 -76
- package/dist/utils/deep-merge.js.map +0 -1
- package/dist/utils/ip-validation.js +0 -78
- package/dist/utils/ip-validation.js.map +0 -1
- package/dist/utils/security.js +0 -61
- package/dist/utils/security.js.map +0 -1
- package/dist/validators/any.js +0 -25
- package/dist/validators/any.js.map +0 -1
- package/dist/validators/array.js +0 -190
- package/dist/validators/array.js.map +0 -1
- package/dist/validators/base.js +0 -491
- package/dist/validators/base.js.map +0 -1
- package/dist/validators/base64.js +0 -65
- package/dist/validators/base64.js.map +0 -1
- package/dist/validators/bigint.js +0 -142
- package/dist/validators/bigint.js.map +0 -1
- package/dist/validators/boolean.js +0 -114
- package/dist/validators/boolean.js.map +0 -1
- package/dist/validators/codec.js +0 -182
- package/dist/validators/codec.js.map +0 -1
- package/dist/validators/custom.js +0 -50
- package/dist/validators/custom.js.map +0 -1
- package/dist/validators/date.js +0 -227
- package/dist/validators/date.js.map +0 -1
- package/dist/validators/discriminated-union.js +0 -98
- package/dist/validators/discriminated-union.js.map +0 -1
- package/dist/validators/enum.js +0 -82
- package/dist/validators/enum.js.map +0 -1
- package/dist/validators/file.js +0 -114
- package/dist/validators/file.js.map +0 -1
- package/dist/validators/function.js +0 -45
- package/dist/validators/function.js.map +0 -1
- package/dist/validators/hex.js +0 -62
- package/dist/validators/hex.js.map +0 -1
- package/dist/validators/intersection.js +0 -64
- package/dist/validators/intersection.js.map +0 -1
- package/dist/validators/json.js +0 -64
- package/dist/validators/json.js.map +0 -1
- package/dist/validators/lazy.js +0 -42
- package/dist/validators/lazy.js.map +0 -1
- package/dist/validators/literal.js +0 -45
- package/dist/validators/literal.js.map +0 -1
- package/dist/validators/map.js +0 -54
- package/dist/validators/map.js.map +0 -1
- package/dist/validators/nan.js +0 -32
- package/dist/validators/nan.js.map +0 -1
- package/dist/validators/never.js +0 -29
- package/dist/validators/never.js.map +0 -1
- package/dist/validators/null.js +0 -31
- package/dist/validators/null.js.map +0 -1
- package/dist/validators/number.js +0 -225
- package/dist/validators/number.js.map +0 -1
- package/dist/validators/object.js +0 -458
- package/dist/validators/object.js.map +0 -1
- package/dist/validators/record.js +0 -128
- package/dist/validators/record.js.map +0 -1
- package/dist/validators/set.js +0 -51
- package/dist/validators/set.js.map +0 -1
- package/dist/validators/string-bool.js +0 -127
- package/dist/validators/string-bool.js.map +0 -1
- package/dist/validators/string-formats.js +0 -120
- package/dist/validators/string-formats.js.map +0 -1
- package/dist/validators/string.js +0 -253
- package/dist/validators/string.js.map +0 -1
- package/dist/validators/symbol.js +0 -42
- package/dist/validators/symbol.js.map +0 -1
- package/dist/validators/template-literal.js +0 -101
- package/dist/validators/template-literal.js.map +0 -1
- package/dist/validators/tuple.js +0 -55
- package/dist/validators/tuple.js.map +0 -1
- package/dist/validators/uint8array.js +0 -98
- package/dist/validators/uint8array.js.map +0 -1
- package/dist/validators/undefined.js +0 -31
- package/dist/validators/undefined.js.map +0 -1
- package/dist/validators/union.js +0 -126
- package/dist/validators/union.js.map +0 -1
- package/dist/validators/unknown.js.map +0 -1
- package/dist/validators/void.js +0 -35
- package/dist/validators/void.js.map +0 -1
- package/dist/validators/xor.js +0 -64
- package/dist/validators/xor.js.map +0 -1
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { VldBase } from './base.js';
|
|
2
|
-
import { getMessages } from '../locales/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* Immutable bigint validator
|
|
5
|
-
*/
|
|
6
|
-
export class VldBigInt extends VldBase {
|
|
7
|
-
/**
|
|
8
|
-
* Protected constructor to allow extension while maintaining immutability
|
|
9
|
-
*/
|
|
10
|
-
constructor(config) {
|
|
11
|
-
super();
|
|
12
|
-
this.config = {
|
|
13
|
-
checks: config?.checks || [],
|
|
14
|
-
errorMessage: config?.errorMessage
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create a new bigint validator
|
|
19
|
-
*/
|
|
20
|
-
static create() {
|
|
21
|
-
return new VldBigInt();
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Parse and validate a bigint value
|
|
25
|
-
*/
|
|
26
|
-
parse(value) {
|
|
27
|
-
if (typeof value !== 'bigint') {
|
|
28
|
-
throw new Error(this.config.errorMessage || getMessages().invalidBigint);
|
|
29
|
-
}
|
|
30
|
-
// Apply all checks
|
|
31
|
-
for (const check of this.config.checks) {
|
|
32
|
-
if (!check(value)) {
|
|
33
|
-
throw new Error(this.config.errorMessage || getMessages().invalidBigint);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return value;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Safely parse and validate a bigint value
|
|
40
|
-
*/
|
|
41
|
-
safeParse(value) {
|
|
42
|
-
try {
|
|
43
|
-
return { success: true, data: this.parse(value) };
|
|
44
|
-
}
|
|
45
|
-
catch (error) {
|
|
46
|
-
return { success: false, error: error };
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Create a new validator with minimum value constraint
|
|
51
|
-
*/
|
|
52
|
-
min(value, message) {
|
|
53
|
-
return new VldBigInt({
|
|
54
|
-
checks: [...this.config.checks, (v) => v >= value],
|
|
55
|
-
errorMessage: message || `BigInt must be at least ${value}`
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Create a new validator with maximum value constraint
|
|
60
|
-
*/
|
|
61
|
-
max(value, message) {
|
|
62
|
-
return new VldBigInt({
|
|
63
|
-
checks: [...this.config.checks, (v) => v <= value],
|
|
64
|
-
errorMessage: message || `BigInt must be at most ${value}`
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Create a new validator that checks for positive values
|
|
69
|
-
*/
|
|
70
|
-
positive(message) {
|
|
71
|
-
return new VldBigInt({
|
|
72
|
-
checks: [...this.config.checks, (v) => v > 0n],
|
|
73
|
-
errorMessage: message || 'BigInt must be positive'
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Create a new validator that checks for negative values
|
|
78
|
-
*/
|
|
79
|
-
negative(message) {
|
|
80
|
-
return new VldBigInt({
|
|
81
|
-
checks: [...this.config.checks, (v) => v < 0n],
|
|
82
|
-
errorMessage: message || 'BigInt must be negative'
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Create a new validator that checks for non-negative values
|
|
87
|
-
*/
|
|
88
|
-
nonnegative(message) {
|
|
89
|
-
return new VldBigInt({
|
|
90
|
-
checks: [...this.config.checks, (v) => v >= 0n],
|
|
91
|
-
errorMessage: message || 'BigInt must be non-negative'
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* Create a new validator that checks for non-positive values
|
|
96
|
-
*/
|
|
97
|
-
nonpositive(message) {
|
|
98
|
-
return new VldBigInt({
|
|
99
|
-
checks: [...this.config.checks, (v) => v <= 0n],
|
|
100
|
-
errorMessage: message || 'BigInt must be non-positive'
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Create a new validator with strict greater than constraint
|
|
105
|
-
* Zod 4 API parity - strictly greater than (not equal to)
|
|
106
|
-
*/
|
|
107
|
-
gt(value, message) {
|
|
108
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
109
|
-
return new VldBigInt({
|
|
110
|
-
checks: [...this.config.checks, (v) => v > compareValue],
|
|
111
|
-
errorMessage: message || `BigInt must be greater than ${compareValue}`
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Create a new validator with strict less than constraint
|
|
116
|
-
* Zod 4 API parity - strictly less than (not equal to)
|
|
117
|
-
*/
|
|
118
|
-
lt(value, message) {
|
|
119
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
120
|
-
return new VldBigInt({
|
|
121
|
-
checks: [...this.config.checks, (v) => v < compareValue],
|
|
122
|
-
errorMessage: message || `BigInt must be less than ${compareValue}`
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
/**
|
|
126
|
-
* Create a new validator with greater than or equal constraint
|
|
127
|
-
* Zod 4 API parity - alias for min()
|
|
128
|
-
*/
|
|
129
|
-
gte(value, message) {
|
|
130
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
131
|
-
return this.min(compareValue, message);
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Create a new validator with less than or equal constraint
|
|
135
|
-
* Zod 4 API parity - alias for max()
|
|
136
|
-
*/
|
|
137
|
-
lte(value, message) {
|
|
138
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
139
|
-
return this.max(compareValue, message);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
//# sourceMappingURL=bigint.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bigint.js","sourceRoot":"","sources":["../../src/validators/bigint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OA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,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;IAED;;;OAGG;IACH,GAAG,CAAC,KAAsB,EAAE,OAAgB;QAC1C,MAAM,YAAY,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACvE,OAAO,IAAI,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IACzC,CAAC;CACF","sourcesContent":["import { VldBase, ParseResult } from './base';\nimport { getMessages } from '../locales';\n\n/**\n * Type for bigint validation check functions\n */\ntype BigIntCheck = (value: bigint) => boolean;\n\n/**\n * Configuration for bigint validator\n */\ninterface BigIntValidatorConfig {\n readonly checks: ReadonlyArray<BigIntCheck>;\n readonly errorMessage?: string;\n}\n\n/**\n * Immutable bigint validator\n */\nexport class VldBigInt extends VldBase<bigint, bigint> {\n protected readonly config: BigIntValidatorConfig;\n \n /**\n * Protected constructor to allow extension while maintaining immutability\n */\n protected constructor(config?: Partial<BigIntValidatorConfig>) {\n super();\n this.config = {\n checks: config?.checks || [],\n errorMessage: config?.errorMessage\n };\n }\n \n /**\n * Create a new bigint validator\n */\n static create(): VldBigInt {\n return new VldBigInt();\n }\n \n /**\n * Parse and validate a bigint value\n */\n parse(value: unknown): bigint {\n if (typeof value !== 'bigint') {\n throw new Error(this.config.errorMessage || getMessages().invalidBigint);\n }\n \n // Apply all checks\n for (const check of this.config.checks) {\n if (!check(value)) {\n throw new Error(this.config.errorMessage || getMessages().invalidBigint);\n }\n }\n \n return value;\n }\n \n /**\n * Safely parse and validate a bigint value\n */\n safeParse(value: unknown): ParseResult<bigint> {\n try {\n return { success: true, data: this.parse(value) };\n } catch (error) {\n return { success: false, error: error as Error };\n }\n }\n \n /**\n * Create a new validator with minimum value constraint\n */\n min(value: bigint, message?: string): VldBigInt {\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v >= value],\n errorMessage: message || `BigInt must be at least ${value}`\n });\n }\n \n /**\n * Create a new validator with maximum value constraint\n */\n max(value: bigint, message?: string): VldBigInt {\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v <= value],\n errorMessage: message || `BigInt must be at most ${value}`\n });\n }\n \n /**\n * Create a new validator that checks for positive values\n */\n positive(message?: string): VldBigInt {\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v > 0n],\n errorMessage: message || 'BigInt must be positive'\n });\n }\n \n /**\n * Create a new validator that checks for negative values\n */\n negative(message?: string): VldBigInt {\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v < 0n],\n errorMessage: message || 'BigInt must be negative'\n });\n }\n \n /**\n * Create a new validator that checks for non-negative values\n */\n nonnegative(message?: string): VldBigInt {\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v >= 0n],\n errorMessage: message || 'BigInt must be non-negative'\n });\n }\n \n /**\n * Create a new validator that checks for non-positive values\n */\n nonpositive(message?: string): VldBigInt {\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v <= 0n],\n errorMessage: message || 'BigInt must be non-positive'\n });\n }\n\n /**\n * Create a new validator with strict greater than constraint\n * Zod 4 API parity - strictly greater than (not equal to)\n */\n gt(value: bigint | number, message?: string): VldBigInt {\n const compareValue = typeof value === 'bigint' ? value : BigInt(value);\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v > compareValue],\n errorMessage: message || `BigInt must be greater than ${compareValue}`\n });\n }\n\n /**\n * Create a new validator with strict less than constraint\n * Zod 4 API parity - strictly less than (not equal to)\n */\n lt(value: bigint | number, message?: string): VldBigInt {\n const compareValue = typeof value === 'bigint' ? value : BigInt(value);\n return new VldBigInt({\n checks: [...this.config.checks, (v: bigint) => v < compareValue],\n errorMessage: message || `BigInt must be less than ${compareValue}`\n });\n }\n\n /**\n * Create a new validator with greater than or equal constraint\n * Zod 4 API parity - alias for min()\n */\n gte(value: bigint | number, message?: string): VldBigInt {\n const compareValue = typeof value === 'bigint' ? value : BigInt(value);\n return this.min(compareValue, message);\n }\n\n /**\n * Create a new validator with less than or equal constraint\n * Zod 4 API parity - alias for max()\n */\n lte(value: bigint | number, message?: string): VldBigInt {\n const compareValue = typeof value === 'bigint' ? value : BigInt(value);\n return this.max(compareValue, message);\n }\n}"]}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { VldBase } from './base.js';
|
|
2
|
-
import { getMessages } from '../locales/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* Immutable boolean validator
|
|
5
|
-
*/
|
|
6
|
-
export class VldBoolean extends VldBase {
|
|
7
|
-
/**
|
|
8
|
-
* Protected constructor to allow extension while maintaining immutability
|
|
9
|
-
*/
|
|
10
|
-
constructor(errorMessage) {
|
|
11
|
-
super();
|
|
12
|
-
this.errorMessage = errorMessage;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* Create a new boolean validator
|
|
16
|
-
*/
|
|
17
|
-
static create() {
|
|
18
|
-
return new VldBoolean();
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Parse and validate a boolean value
|
|
22
|
-
*/
|
|
23
|
-
parse(value) {
|
|
24
|
-
if (typeof value !== 'boolean') {
|
|
25
|
-
throw new Error(this.errorMessage || getMessages().invalidBoolean);
|
|
26
|
-
}
|
|
27
|
-
return value;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Safely parse and validate a boolean value
|
|
31
|
-
*/
|
|
32
|
-
safeParse(value) {
|
|
33
|
-
if (typeof value === 'boolean') {
|
|
34
|
-
return { success: true, data: value };
|
|
35
|
-
}
|
|
36
|
-
return {
|
|
37
|
-
success: false,
|
|
38
|
-
error: new Error(this.errorMessage || getMessages().invalidBoolean)
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Create a new validator that only accepts true
|
|
43
|
-
*/
|
|
44
|
-
true(message) {
|
|
45
|
-
return new VldTrue(message || 'Value must be true');
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Create a new validator that only accepts false
|
|
49
|
-
*/
|
|
50
|
-
false(message) {
|
|
51
|
-
return new VldFalse(message || 'Value must be false');
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Validator that only accepts true
|
|
56
|
-
*/
|
|
57
|
-
class VldTrue extends VldBoolean {
|
|
58
|
-
constructor(message) {
|
|
59
|
-
super(message);
|
|
60
|
-
this.message = message;
|
|
61
|
-
}
|
|
62
|
-
parse(value) {
|
|
63
|
-
const result = super.parse(value);
|
|
64
|
-
if (result !== true) {
|
|
65
|
-
throw new Error(this.message);
|
|
66
|
-
}
|
|
67
|
-
return result;
|
|
68
|
-
}
|
|
69
|
-
// BUG-003 FIX: Override safeParse to ensure true validation
|
|
70
|
-
safeParse(value) {
|
|
71
|
-
const result = super.safeParse(value);
|
|
72
|
-
if (!result.success) {
|
|
73
|
-
return result;
|
|
74
|
-
}
|
|
75
|
-
if (result.data !== true) {
|
|
76
|
-
return {
|
|
77
|
-
success: false,
|
|
78
|
-
error: new Error(this.message)
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
return result;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Validator that only accepts false
|
|
86
|
-
*/
|
|
87
|
-
class VldFalse extends VldBoolean {
|
|
88
|
-
constructor(message) {
|
|
89
|
-
super(message);
|
|
90
|
-
this.message = message;
|
|
91
|
-
}
|
|
92
|
-
parse(value) {
|
|
93
|
-
const result = super.parse(value);
|
|
94
|
-
if (result !== false) {
|
|
95
|
-
throw new Error(this.message);
|
|
96
|
-
}
|
|
97
|
-
return result;
|
|
98
|
-
}
|
|
99
|
-
// BUG-003 FIX: Override safeParse to ensure false validation
|
|
100
|
-
safeParse(value) {
|
|
101
|
-
const result = super.safeParse(value);
|
|
102
|
-
if (!result.success) {
|
|
103
|
-
return result;
|
|
104
|
-
}
|
|
105
|
-
if (result.data !== false) {
|
|
106
|
-
return {
|
|
107
|
-
success: false,
|
|
108
|
-
error: new Error(this.message)
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
return result;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
//# sourceMappingURL=boolean.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"boolean.js","sourceRoot":"","sources":["../../src/validators/boolean.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzC;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,OAAyB;IAGvD;;OAEG;IACH,YAAsB,YAAqB;QACzC,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;IACnC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,MAAM;QACX,OAAO,IAAI,UAAU,EAAE,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAc;QAClB,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/B,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,YAAY,IAAI,WAAW,EAAE,CAAC,cAAc,CAAC,CAAC;QACrE,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,KAAc;QACtB,IAAI,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;QACxC,CAAC;QACD,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,YAAY,IAAI,WAAW,EAAE,CAAC,cAAc,CAAC;SACpE,CAAC;IACJ,CAAC;IAED;;OAE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AC;QAClC,IAAI,MAAM,KAAK,KAAK,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAChC,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,6DAA6D;IAC7D,SAAS,CAAC,KAAc;QACtB,MAAM,MAAM,GAAG,KAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACtC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,IAAI,MAAM,CAAC,IAAI,KAAK,KAAK,EAAE,CAAC;YAC1B,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC;aAC/B,CAAC;QACJ,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;CACF","sourcesContent":["import { VldBase, ParseResult } from './base';\r\nimport { getMessages } from '../locales';\r\n\r\n/**\r\n * Immutable boolean validator\r\n */\r\nexport class VldBoolean extends VldBase<boolean, boolean> {\r\n private readonly errorMessage?: string;\r\n \r\n /**\r\n * Protected constructor to allow extension while maintaining immutability\r\n */\r\n protected constructor(errorMessage?: string) {\r\n super();\r\n this.errorMessage = errorMessage;\r\n }\r\n \r\n /**\r\n * Create a new boolean validator\r\n */\r\n static create(): VldBoolean {\r\n return new VldBoolean();\r\n }\r\n \r\n /**\r\n * Parse and validate a boolean value\r\n */\r\n parse(value: unknown): boolean {\r\n if (typeof value !== 'boolean') {\r\n throw new Error(this.errorMessage || getMessages().invalidBoolean);\r\n }\r\n return value;\r\n }\r\n \r\n /**\r\n * Safely parse and validate a boolean value\r\n */\r\n safeParse(value: unknown): ParseResult<boolean> {\r\n if (typeof value === 'boolean') {\r\n return { success: true, data: value };\r\n }\r\n return { \r\n success: false, \r\n error: new Error(this.errorMessage || getMessages().invalidBoolean) \r\n };\r\n }\r\n \r\n /**\r\n * Create a new validator that only accepts true\r\n */\r\n true(message?: string): VldBoolean {\r\n return new VldTrue(message || 'Value must be true');\r\n }\r\n \r\n /**\r\n * Create a new validator that only accepts false\r\n */\r\n false(message?: string): VldBoolean {\r\n return new VldFalse(message || 'Value must be false');\r\n }\r\n}\r\n\r\n/**\r\n * Validator that only accepts true\r\n */\r\nclass VldTrue extends VldBoolean {\r\n constructor(private readonly message: string) {\r\n super(message);\r\n }\r\n\r\n parse(value: unknown): boolean {\r\n const result = super.parse(value);\r\n if (result !== true) {\r\n throw new Error(this.message);\r\n }\r\n return result;\r\n }\r\n\r\n // BUG-003 FIX: Override safeParse to ensure true validation\r\n safeParse(value: unknown): ParseResult<boolean> {\r\n const result = super.safeParse(value);\r\n if (!result.success) {\r\n return result;\r\n }\r\n if (result.data !== true) {\r\n return {\r\n success: false,\r\n error: new Error(this.message)\r\n };\r\n }\r\n return result;\r\n }\r\n}\r\n\r\n/**\r\n * Validator that only accepts false\r\n */\r\nclass VldFalse extends VldBoolean {\r\n constructor(private readonly message: string) {\r\n super(message);\r\n }\r\n\r\n parse(value: unknown): boolean {\r\n const result = super.parse(value);\r\n if (result !== false) {\r\n throw new Error(this.message);\r\n }\r\n return result;\r\n }\r\n\r\n // BUG-003 FIX: Override safeParse to ensure false validation\r\n safeParse(value: unknown): ParseResult<boolean> {\r\n const result = super.safeParse(value);\r\n if (!result.success) {\r\n return result;\r\n }\r\n if (result.data !== false) {\r\n return {\r\n success: false,\r\n error: new Error(this.message)\r\n };\r\n }\r\n return result;\r\n }\r\n}"]}
|
package/dist/validators/codec.js
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
import { VldBase } from './base.js';
|
|
2
|
-
import { VldError, createIssue } from '../errors.js';
|
|
3
|
-
import { getMessages } from '../locales/index.js';
|
|
4
|
-
/**
|
|
5
|
-
* Codec validator for bidirectional transformations
|
|
6
|
-
* Enables converting between different data representations
|
|
7
|
-
*/
|
|
8
|
-
export class VldCodec extends VldBase {
|
|
9
|
-
constructor(inputValidator, outputValidator, codecTransform) {
|
|
10
|
-
super();
|
|
11
|
-
this.inputValidator = inputValidator;
|
|
12
|
-
this.outputValidator = outputValidator;
|
|
13
|
-
this.codecTransform = codecTransform;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Parse (decode) a value from input to output format
|
|
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
|
-
/**
|
|
26
|
-
* Safely parse (decode) a value from input to output format
|
|
27
|
-
*/
|
|
28
|
-
safeParse(value) {
|
|
29
|
-
// First validate the input
|
|
30
|
-
const inputResult = this.inputValidator.safeParse(value);
|
|
31
|
-
if (!inputResult.success) {
|
|
32
|
-
return inputResult;
|
|
33
|
-
}
|
|
34
|
-
try {
|
|
35
|
-
// Then decode to output
|
|
36
|
-
const decoded = this.codecTransform.decode(inputResult.data);
|
|
37
|
-
// Handle async decode
|
|
38
|
-
if (decoded instanceof Promise) {
|
|
39
|
-
throw new VldError([
|
|
40
|
-
createIssue('custom', [], getMessages().codecAsyncNotSupported)
|
|
41
|
-
]);
|
|
42
|
-
}
|
|
43
|
-
// Validate the decoded output
|
|
44
|
-
return this.outputValidator.safeParse(decoded);
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
if (error instanceof VldError) {
|
|
48
|
-
return { success: false, error };
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
success: false,
|
|
52
|
-
error: new VldError([
|
|
53
|
-
createIssue('custom', [], getMessages().codecDecodeFailed)
|
|
54
|
-
])
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Encode a value from output back to input format
|
|
60
|
-
*/
|
|
61
|
-
encode(value) {
|
|
62
|
-
const result = this.safeEncode(value);
|
|
63
|
-
if (!result.success) {
|
|
64
|
-
throw result.error;
|
|
65
|
-
}
|
|
66
|
-
return result.data;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Safely encode a value from output back to input format
|
|
70
|
-
*/
|
|
71
|
-
safeEncode(value) {
|
|
72
|
-
// First validate the output
|
|
73
|
-
const outputResult = this.outputValidator.safeParse(value);
|
|
74
|
-
if (!outputResult.success) {
|
|
75
|
-
return outputResult;
|
|
76
|
-
}
|
|
77
|
-
try {
|
|
78
|
-
// Then encode to input
|
|
79
|
-
const encoded = this.codecTransform.encode(outputResult.data);
|
|
80
|
-
// Handle async encode
|
|
81
|
-
if (encoded instanceof Promise) {
|
|
82
|
-
throw new VldError([
|
|
83
|
-
createIssue('custom', [], getMessages().codecAsyncNotSupported)
|
|
84
|
-
]);
|
|
85
|
-
}
|
|
86
|
-
// Validate the encoded input
|
|
87
|
-
return this.inputValidator.safeParse(encoded);
|
|
88
|
-
}
|
|
89
|
-
catch (error) {
|
|
90
|
-
if (error instanceof VldError) {
|
|
91
|
-
return { success: false, error };
|
|
92
|
-
}
|
|
93
|
-
return {
|
|
94
|
-
success: false,
|
|
95
|
-
error: new VldError([
|
|
96
|
-
createIssue('custom', [], getMessages().codecEncodeFailed)
|
|
97
|
-
])
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Async version of parse (decode)
|
|
103
|
-
*/
|
|
104
|
-
async parseAsync(value) {
|
|
105
|
-
const result = await this.safeParseAsync(value);
|
|
106
|
-
if (!result.success) {
|
|
107
|
-
throw result.error;
|
|
108
|
-
}
|
|
109
|
-
return result.data;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Async version of safeParse (decode)
|
|
113
|
-
*/
|
|
114
|
-
async safeParseAsync(value) {
|
|
115
|
-
// First validate the input
|
|
116
|
-
const inputResult = this.inputValidator.safeParse(value);
|
|
117
|
-
if (!inputResult.success) {
|
|
118
|
-
return inputResult;
|
|
119
|
-
}
|
|
120
|
-
try {
|
|
121
|
-
// Then decode to output (supports async)
|
|
122
|
-
const decoded = await this.codecTransform.decode(inputResult.data);
|
|
123
|
-
// Validate the decoded output
|
|
124
|
-
return this.outputValidator.safeParse(decoded);
|
|
125
|
-
}
|
|
126
|
-
catch (error) {
|
|
127
|
-
if (error instanceof VldError) {
|
|
128
|
-
return { success: false, error };
|
|
129
|
-
}
|
|
130
|
-
return {
|
|
131
|
-
success: false,
|
|
132
|
-
error: new VldError([
|
|
133
|
-
createIssue('custom', [], getMessages().codecDecodeFailed)
|
|
134
|
-
])
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Async version of encode
|
|
140
|
-
*/
|
|
141
|
-
async encodeAsync(value) {
|
|
142
|
-
const result = await this.safeEncodeAsync(value);
|
|
143
|
-
if (!result.success) {
|
|
144
|
-
throw result.error;
|
|
145
|
-
}
|
|
146
|
-
return result.data;
|
|
147
|
-
}
|
|
148
|
-
/**
|
|
149
|
-
* Async version of safeEncode
|
|
150
|
-
*/
|
|
151
|
-
async safeEncodeAsync(value) {
|
|
152
|
-
// First validate the output
|
|
153
|
-
const outputResult = this.outputValidator.safeParse(value);
|
|
154
|
-
if (!outputResult.success) {
|
|
155
|
-
return outputResult;
|
|
156
|
-
}
|
|
157
|
-
try {
|
|
158
|
-
// Then encode to input (supports async)
|
|
159
|
-
const encoded = await this.codecTransform.encode(outputResult.data);
|
|
160
|
-
// Validate the encoded input
|
|
161
|
-
return this.inputValidator.safeParse(encoded);
|
|
162
|
-
}
|
|
163
|
-
catch (error) {
|
|
164
|
-
if (error instanceof VldError) {
|
|
165
|
-
return { success: false, error };
|
|
166
|
-
}
|
|
167
|
-
return {
|
|
168
|
-
success: false,
|
|
169
|
-
error: new VldError([
|
|
170
|
-
createIssue('custom', [], getMessages().codecEncodeFailed)
|
|
171
|
-
])
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Create a codec validator
|
|
177
|
-
*/
|
|
178
|
-
static create(inputValidator, outputValidator, codecTransform) {
|
|
179
|
-
return new VldCodec(inputValidator, outputValidator, codecTransform);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
//# sourceMappingURL=codec.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/validators/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,QAAQ,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAUzC;;;GAGG;AACH,MAAM,OAAO,QAA0B,SAAQ,OAAwB;IAKrE,YACE,cAAwC,EACxC,eAA0C,EAC1C,cAA+C;QAE/C,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;QACrC,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;QACvC,IAAI,CAAC,cAAc,GAAG,cAAc,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAc;QAClB,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACrC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;YACpB,MAAM,MAAM,CAAC,KAAK,CAAC;QACrB,CAAC;QACD,OAAO,MAAM,CAAC,IAAI,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,KAAc;QACtB,2BAA2B;QAC3B,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACzD,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,CAAC;YACzB,OAAO,WAAmC,CAAC;QAC7C,CAAC;QAED,IAAI,CAAC;YACH,wBAAwB;YACxB,MAAM,OAAO,GAAG,IAAI,CAAC,cAAc,CAA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C;YACf,IAAI,KAAK,YAAY,QAAQ,EAAE,CAAC;gBAC9B,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;YACnC,CAAC;YACD,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,KAAK,EAAE,IAAI,QAAQ,CAAC;oBAClB,WAAW,CAAC,QAAQ,EAAE,EAAE,EAAE,WAAW,EAAE,CAAC,iBAAiB,CAAC;iBAC3D,CAAC;aACH,CAAC;QACJ,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,MAAM,CACX,cAAwC,EACxC,eAA0C,EAC1C,cAA+C;QAE/C,OAAO,IAAI,QAAQ,CAAC,cAAc,EAAE,eAAe,EAAE,cAAc,CAAC,CAAC;IACvE,CAAC;CACF","sourcesContent":["import { VldBase, ParseResult } from './base';\nimport { VldError, createIssue } from '../errors';\nimport { getMessages } from '../locales';\n\n/**\n * Codec transform functions for bidirectional transformations\n */\nexport interface CodecTransform<TInput, TOutput> {\n decode: (value: TInput) => TOutput | Promise<TOutput>;\n encode: (value: TOutput) => TInput | Promise<TInput>;\n}\n\n/**\n * Codec validator for bidirectional transformations\n * Enables converting between different data representations\n */\nexport class VldCodec<TInput, TOutput> extends VldBase<TInput, TOutput> {\n private readonly inputValidator: VldBase<unknown, TInput>;\n private readonly outputValidator: VldBase<unknown, TOutput>;\n private readonly codecTransform: CodecTransform<TInput, TOutput>;\n \n constructor(\n inputValidator: VldBase<unknown, TInput>,\n outputValidator: VldBase<unknown, TOutput>,\n codecTransform: CodecTransform<TInput, TOutput>\n ) {\n super();\n this.inputValidator = inputValidator;\n this.outputValidator = outputValidator;\n this.codecTransform = codecTransform;\n }\n \n /**\n * Parse (decode) a value from input to output format\n */\n parse(value: unknown): TOutput {\n const result = this.safeParse(value);\n if (!result.success) {\n throw result.error;\n }\n return result.data;\n }\n \n /**\n * Safely parse (decode) a value from input to output format\n */\n safeParse(value: unknown): ParseResult<TOutput> {\n // First validate the input\n const inputResult = this.inputValidator.safeParse(value);\n if (!inputResult.success) {\n return inputResult as ParseResult<TOutput>;\n }\n \n try {\n // Then decode to output\n const decoded = this.codecTransform.decode(inputResult.data);\n \n // Handle async decode\n if (decoded instanceof Promise) {\n throw new VldError([\n createIssue('custom', [], getMessages().codecAsyncNotSupported)\n ]);\n }\n \n // Validate the decoded output\n return this.outputValidator.safeParse(decoded);\n } catch (error) {\n if (error instanceof VldError) {\n return { success: false, error };\n }\n return {\n success: false,\n error: new VldError([\n createIssue('custom', [], getMessages().codecDecodeFailed)\n ])\n };\n }\n }\n \n /**\n * Encode a value from output back to input format\n */\n encode(value: unknown): TInput {\n const result = this.safeEncode(value);\n if (!result.success) {\n throw result.error;\n }\n return result.data;\n }\n \n /**\n * Safely encode a value from output back to input format\n */\n safeEncode(value: unknown): ParseResult<TInput> {\n // First validate the output\n const outputResult = this.outputValidator.safeParse(value);\n if (!outputResult.success) {\n return outputResult as ParseResult<TInput>;\n }\n \n try {\n // Then encode to input\n const encoded = this.codecTransform.encode(outputResult.data);\n \n // Handle async encode\n if (encoded instanceof Promise) {\n throw new VldError([\n createIssue('custom', [], getMessages().codecAsyncNotSupported)\n ]);\n }\n \n // Validate the encoded input\n return this.inputValidator.safeParse(encoded);\n } catch (error) {\n if (error instanceof VldError) {\n return { success: false, error };\n }\n return {\n success: false,\n error: new VldError([\n createIssue('custom', [], getMessages().codecEncodeFailed)\n ])\n };\n }\n }\n \n /**\n * Async version of parse (decode)\n */\n async parseAsync(value: unknown): Promise<TOutput> {\n const result = await this.safeParseAsync(value);\n if (!result.success) {\n throw result.error;\n }\n return result.data;\n }\n \n /**\n * Async version of safeParse (decode)\n */\n async safeParseAsync(value: unknown): Promise<ParseResult<TOutput>> {\n // First validate the input\n const inputResult = this.inputValidator.safeParse(value);\n if (!inputResult.success) {\n return inputResult as ParseResult<TOutput>;\n }\n \n try {\n // Then decode to output (supports async)\n const decoded = await this.codecTransform.decode(inputResult.data);\n \n // Validate the decoded output\n return this.outputValidator.safeParse(decoded);\n } catch (error) {\n if (error instanceof VldError) {\n return { success: false, error };\n }\n return {\n success: false,\n error: new VldError([\n createIssue('custom', [], getMessages().codecDecodeFailed)\n ])\n };\n }\n }\n \n /**\n * Async version of encode\n */\n async encodeAsync(value: unknown): Promise<TInput> {\n const result = await this.safeEncodeAsync(value);\n if (!result.success) {\n throw result.error;\n }\n return result.data;\n }\n \n /**\n * Async version of safeEncode\n */\n async safeEncodeAsync(value: unknown): Promise<ParseResult<TInput>> {\n // First validate the output\n const outputResult = this.outputValidator.safeParse(value);\n if (!outputResult.success) {\n return outputResult as ParseResult<TInput>;\n }\n \n try {\n // Then encode to input (supports async)\n const encoded = await this.codecTransform.encode(outputResult.data);\n \n // Validate the encoded input\n return this.inputValidator.safeParse(encoded);\n } catch (error) {\n if (error instanceof VldError) {\n return { success: false, error };\n }\n return {\n success: false,\n error: new VldError([\n createIssue('custom', [], getMessages().codecEncodeFailed)\n ])\n };\n }\n }\n \n /**\n * Create a codec validator\n */\n static create<TInput, TOutput>(\n inputValidator: VldBase<unknown, TInput>,\n outputValidator: VldBase<unknown, TOutput>,\n codecTransform: CodecTransform<TInput, TOutput>\n ): VldCodec<TInput, TOutput> {\n return new VldCodec(inputValidator, outputValidator, codecTransform);\n }\n}"]}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { VldBase } from './base.js';
|
|
2
|
-
/**
|
|
3
|
-
* Immutable custom validator for user-defined validation logic
|
|
4
|
-
* Provides type safety while allowing complete control over validation
|
|
5
|
-
*/
|
|
6
|
-
export class VldCustom extends VldBase {
|
|
7
|
-
constructor(options) {
|
|
8
|
-
super();
|
|
9
|
-
this._parseFn = options.parse;
|
|
10
|
-
this._safeParseFn = options.safeParse || ((value) => {
|
|
11
|
-
try {
|
|
12
|
-
return { success: true, data: this._parseFn(value) };
|
|
13
|
-
}
|
|
14
|
-
catch (error) {
|
|
15
|
-
return { success: false, error: error };
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Create a new custom validator
|
|
21
|
-
*/
|
|
22
|
-
static create(options) {
|
|
23
|
-
return new VldCustom(options);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Parse and validate using the custom parse function
|
|
27
|
-
*/
|
|
28
|
-
parse(value) {
|
|
29
|
-
return this._parseFn(value);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Safely parse using the custom safeParse function or default implementation
|
|
33
|
-
*/
|
|
34
|
-
safeParse(value) {
|
|
35
|
-
return this._safeParseFn(value);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Helper function to create custom validators
|
|
40
|
-
* Usage: v.custom<number>({
|
|
41
|
-
* parse: (value) => {
|
|
42
|
-
* if (typeof value !== 'string') throw new Error('Expected string');
|
|
43
|
-
* return parseInt(value, 10);
|
|
44
|
-
* }
|
|
45
|
-
* })
|
|
46
|
-
*/
|
|
47
|
-
export function custom(options) {
|
|
48
|
-
return VldCustom.create(options);
|
|
49
|
-
}
|
|
50
|
-
//# sourceMappingURL=custom.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"custom.js","sourceRoot":"","sources":["../../src/validators/custom.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAe,MAAM,QAAQ,CAAC;AAkB9C;;;GAGG;AACH,MAAM,OAAO,SAA6B,SAAQ,OAAyB;IAIzE,YAAoB,OAAwC;QAC1D,KAAK,EAAE,CAAC;QACR,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,KAAK,CAAC;QAC9B,IAAI,CAAC,YAAY,GAAG,OAAO,CAAC,SAAS,IAAI,CAAC,CAAC,KAAc,EAAE,EAAE;YAC3D,IAAI,CAAC;gBACH,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACvD,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,KAAc,EAAE,CAAC;YACnD,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,MAAM,CACX,OAAwC;QAExC,OAAO,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,KAAc;QAClB,OAAO,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,KAAc;QACtB,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;CACF;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,MAAM,CACpB,OAAwC;IAExC,OAAO,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACnC,CAAC","sourcesContent":["import { VldBase, ParseResult } from './base';\n\n/**\n * Type-safe custom validator options\n */\nexport interface CustomValidatorOptions<TOutput> {\n /**\n * Parse function that validates and transforms input\n */\n parse: (value: unknown) => TOutput;\n\n /**\n * Optional safeParse implementation\n * If not provided, will use try-catch around parse()\n */\n safeParse?: (value: unknown) => ParseResult<TOutput>;\n}\n\n/**\n * Immutable custom validator for user-defined validation logic\n * Provides type safety while allowing complete control over validation\n */\nexport class VldCustom<TOutput = unknown> extends VldBase<unknown, TOutput> {\n private readonly _parseFn: (value: unknown) => TOutput;\n private readonly _safeParseFn: (value: unknown) => ParseResult<TOutput>;\n\n private constructor(options: CustomValidatorOptions<TOutput>) {\n super();\n this._parseFn = options.parse;\n this._safeParseFn = options.safeParse || ((value: unknown) => {\n try {\n return { success: true, data: this._parseFn(value) };\n } catch (error) {\n return { success: false, error: error as Error };\n }\n });\n }\n\n /**\n * Create a new custom validator\n */\n static create<TOutput>(\n options: CustomValidatorOptions<TOutput>\n ): VldCustom<TOutput> {\n return new VldCustom(options);\n }\n\n /**\n * Parse and validate using the custom parse function\n */\n parse(value: unknown): TOutput {\n return this._parseFn(value);\n }\n\n /**\n * Safely parse using the custom safeParse function or default implementation\n */\n safeParse(value: unknown): ParseResult<TOutput> {\n return this._safeParseFn(value);\n }\n}\n\n/**\n * Helper function to create custom validators\n * Usage: v.custom<number>({\n * parse: (value) => {\n * if (typeof value !== 'string') throw new Error('Expected string');\n * return parseInt(value, 10);\n * }\n * })\n */\nexport function custom<TOutput = unknown>(\n options: CustomValidatorOptions<TOutput>\n): VldCustom<TOutput> {\n return VldCustom.create(options);\n}\n"]}
|