@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,68 +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
|
-
* Base64 string validator
|
|
8
|
-
*/
|
|
9
|
-
class VldBase64 extends VldBase {
|
|
10
|
-
constructor(urlSafe = false) {
|
|
11
|
-
super(VLD_VALIDATOR_TYPES.BASE64);
|
|
12
|
-
this.urlSafe = urlSafe;
|
|
13
|
-
}
|
|
14
|
-
parse(value) {
|
|
15
|
-
const result = this.safeParse(value);
|
|
16
|
-
if (!result.success) {
|
|
17
|
-
throw result.error;
|
|
18
|
-
}
|
|
19
|
-
return result.data;
|
|
20
|
-
}
|
|
21
|
-
safeParse(value) {
|
|
22
|
-
if (typeof value !== 'string') {
|
|
23
|
-
return {
|
|
24
|
-
success: false,
|
|
25
|
-
error: new VldError([
|
|
26
|
-
createIssue('invalid_type', [], getMessages().expectedString)
|
|
27
|
-
])
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
// Check if it's a valid base64 string
|
|
31
|
-
const regex = this.urlSafe ? VldBase64.BASE64_URL_REGEX : VldBase64.BASE64_REGEX;
|
|
32
|
-
// Check basic format
|
|
33
|
-
if (!regex.test(value)) {
|
|
34
|
-
return {
|
|
35
|
-
success: false,
|
|
36
|
-
error: new VldError([
|
|
37
|
-
createIssue('custom', [], getMessages().invalidBase64)
|
|
38
|
-
])
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
// For standard base64, check length is multiple of 4 (after padding)
|
|
42
|
-
// Empty strings are valid (decode to empty buffer)
|
|
43
|
-
// For URL-safe base64, padding is not required
|
|
44
|
-
if (!this.urlSafe && value.length > 0 && value.length % 4 !== 0) {
|
|
45
|
-
return {
|
|
46
|
-
success: false,
|
|
47
|
-
error: new VldError([
|
|
48
|
-
createIssue('custom', [], getMessages().invalidBase64)
|
|
49
|
-
])
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
return { success: true, data: value };
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Create a URL-safe base64 validator
|
|
56
|
-
*/
|
|
57
|
-
urlSafeMode() {
|
|
58
|
-
return new VldBase64(true);
|
|
59
|
-
}
|
|
60
|
-
static create(urlSafe = false) {
|
|
61
|
-
return new VldBase64(urlSafe);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
VldBase64.BASE64_REGEX = /^[A-Za-z0-9+/]*={0,2}$/;
|
|
65
|
-
// Note: RFC 4648 specifies URL-safe base64 should omit padding, but we allow it for compatibility
|
|
66
|
-
VldBase64.BASE64_URL_REGEX = /^[A-Za-z0-9_-]*={0,2}$/;
|
|
67
|
-
|
|
68
|
-
export { VldBase64 };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{createIssue as r}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as a}from"../chunks/errors-core-Cqi4-9ew.js";class VldBase64 extends e{constructor(e=!1){super(s.BASE64),this.urlSafe=e}parse(e){const s=this.safeParse(e);if(!s.success)throw s.error;return s.data}safeParse(e){if("string"!=typeof e)return{success:!1,error:new a([r("invalid_type",[],t().expectedString)])};return(this.urlSafe?VldBase64.BASE64_URL_REGEX:VldBase64.BASE64_REGEX).test(e)?!this.urlSafe&&e.length>0&&e.length%4!=0?{success:!1,error:new a([r("custom",[],t().invalidBase64)])}:{success:!0,data:e}:{success:!1,error:new a([r("custom",[],t().invalidBase64)])}}urlSafeMode(){return new VldBase64(!0)}static create(e=!1){return new VldBase64(e)}}VldBase64.BASE64_REGEX=/^[A-Za-z0-9+/]*={0,2}$/,VldBase64.BASE64_URL_REGEX=/^[A-Za-z0-9_-]*={0,2}$/;export{VldBase64};
|
|
@@ -1,228 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createBigIntError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable bigint validator
|
|
10
|
-
*/
|
|
11
|
-
class VldBigInt extends VldBase {
|
|
12
|
-
/**
|
|
13
|
-
* Protected constructor to allow extension while maintaining immutability
|
|
14
|
-
*/
|
|
15
|
-
constructor(config) {
|
|
16
|
-
super(config?.validatorType || VLD_VALIDATOR_TYPES.BIGINT);
|
|
17
|
-
this.config = {
|
|
18
|
-
checks: config?.checks || [],
|
|
19
|
-
errorMessage: config?.errorMessage,
|
|
20
|
-
jsonSchema: config?.jsonSchema
|
|
21
|
-
};
|
|
22
|
-
this._checks = this.config.checks;
|
|
23
|
-
this._fastCheckMode = this.detectFastCheckMode();
|
|
24
|
-
}
|
|
25
|
-
get jsonSchema() {
|
|
26
|
-
return this.config.jsonSchema;
|
|
27
|
-
}
|
|
28
|
-
get isSimple() {
|
|
29
|
-
return this._checks.length === 0;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Create a new bigint validator
|
|
33
|
-
*/
|
|
34
|
-
static create() {
|
|
35
|
-
return new VldBigInt();
|
|
36
|
-
}
|
|
37
|
-
detectFastCheckMode() {
|
|
38
|
-
if (this._checks.length === 0) {
|
|
39
|
-
return 'none';
|
|
40
|
-
}
|
|
41
|
-
if (this._checks.length !== 1) {
|
|
42
|
-
return undefined;
|
|
43
|
-
}
|
|
44
|
-
const schema = this.config.jsonSchema;
|
|
45
|
-
if (schema?.exclusiveMinimum === 0n && schema.minimum === undefined && schema.maximum === undefined) {
|
|
46
|
-
return 'positive';
|
|
47
|
-
}
|
|
48
|
-
if (schema?.exclusiveMaximum === 0n && schema.minimum === undefined && schema.maximum === undefined) {
|
|
49
|
-
return 'negative';
|
|
50
|
-
}
|
|
51
|
-
if (schema?.minimum === 0n && schema.maximum === undefined && schema.exclusiveMaximum === undefined) {
|
|
52
|
-
return 'nonnegative';
|
|
53
|
-
}
|
|
54
|
-
if (schema?.maximum === 0n && schema.minimum === undefined && schema.exclusiveMinimum === undefined) {
|
|
55
|
-
return 'nonpositive';
|
|
56
|
-
}
|
|
57
|
-
return undefined;
|
|
58
|
-
}
|
|
59
|
-
getValidationError() {
|
|
60
|
-
return new Error(this.config.errorMessage || getMessages().invalidBigint);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Parse and validate a bigint value
|
|
64
|
-
*/
|
|
65
|
-
parse(value) {
|
|
66
|
-
if (typeof value !== 'bigint') {
|
|
67
|
-
throw this.getValidationError();
|
|
68
|
-
}
|
|
69
|
-
return this.parseKnownBigInt(value);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Parse a value that has already passed the bigint type guard.
|
|
73
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
74
|
-
*/
|
|
75
|
-
parseKnownBigInt(value) {
|
|
76
|
-
switch (this._fastCheckMode) {
|
|
77
|
-
case 'none':
|
|
78
|
-
return value;
|
|
79
|
-
case 'positive':
|
|
80
|
-
if (value > 0n)
|
|
81
|
-
return value;
|
|
82
|
-
throw this.getValidationError();
|
|
83
|
-
case 'negative':
|
|
84
|
-
if (value < 0n)
|
|
85
|
-
return value;
|
|
86
|
-
throw this.getValidationError();
|
|
87
|
-
case 'nonnegative':
|
|
88
|
-
if (value >= 0n)
|
|
89
|
-
return value;
|
|
90
|
-
throw this.getValidationError();
|
|
91
|
-
case 'nonpositive':
|
|
92
|
-
if (value <= 0n)
|
|
93
|
-
return value;
|
|
94
|
-
throw this.getValidationError();
|
|
95
|
-
}
|
|
96
|
-
// Apply all checks
|
|
97
|
-
for (const check of this._checks) {
|
|
98
|
-
if (!check(value)) {
|
|
99
|
-
throw this.getValidationError();
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Safely parse and validate a bigint value
|
|
106
|
-
*/
|
|
107
|
-
safeParse(value) {
|
|
108
|
-
if (typeof value !== 'bigint') {
|
|
109
|
-
return { success: false, error: createBigIntError(this.getValidationError().message) };
|
|
110
|
-
}
|
|
111
|
-
try {
|
|
112
|
-
return { success: true, data: this.parseKnownBigInt(value) };
|
|
113
|
-
}
|
|
114
|
-
catch (error) {
|
|
115
|
-
return { success: false, error: createBigIntError(error.message) };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Create a new validator with minimum value constraint
|
|
120
|
-
*/
|
|
121
|
-
min(value, message) {
|
|
122
|
-
return new VldBigInt({
|
|
123
|
-
...this.config,
|
|
124
|
-
checks: [...this.config.checks, (v) => v >= value],
|
|
125
|
-
errorMessage: message || `BigInt must be at least ${value}`,
|
|
126
|
-
jsonSchema: { ...this.config.jsonSchema, minimum: value }
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Create a new validator with maximum value constraint
|
|
131
|
-
*/
|
|
132
|
-
max(value, message) {
|
|
133
|
-
return new VldBigInt({
|
|
134
|
-
...this.config,
|
|
135
|
-
checks: [...this.config.checks, (v) => v <= value],
|
|
136
|
-
errorMessage: message || `BigInt must be at most ${value}`,
|
|
137
|
-
jsonSchema: { ...this.config.jsonSchema, maximum: value }
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Create a new validator that checks for positive values
|
|
142
|
-
*/
|
|
143
|
-
positive(message) {
|
|
144
|
-
return new VldBigInt({
|
|
145
|
-
...this.config,
|
|
146
|
-
checks: [...this.config.checks, (v) => v > 0n],
|
|
147
|
-
errorMessage: message || 'BigInt must be positive',
|
|
148
|
-
jsonSchema: { ...this.config.jsonSchema, exclusiveMinimum: 0n }
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Create a new validator that checks for negative values
|
|
153
|
-
*/
|
|
154
|
-
negative(message) {
|
|
155
|
-
return new VldBigInt({
|
|
156
|
-
...this.config,
|
|
157
|
-
checks: [...this.config.checks, (v) => v < 0n],
|
|
158
|
-
errorMessage: message || 'BigInt must be negative',
|
|
159
|
-
jsonSchema: { ...this.config.jsonSchema, exclusiveMaximum: 0n }
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Create a new validator that checks for non-negative values
|
|
164
|
-
*/
|
|
165
|
-
nonnegative(message) {
|
|
166
|
-
return new VldBigInt({
|
|
167
|
-
...this.config,
|
|
168
|
-
checks: [...this.config.checks, (v) => v >= 0n],
|
|
169
|
-
errorMessage: message || 'BigInt must be non-negative',
|
|
170
|
-
jsonSchema: { ...this.config.jsonSchema, minimum: 0n }
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Create a new validator that checks for non-positive values
|
|
175
|
-
*/
|
|
176
|
-
nonpositive(message) {
|
|
177
|
-
return new VldBigInt({
|
|
178
|
-
...this.config,
|
|
179
|
-
checks: [...this.config.checks, (v) => v <= 0n],
|
|
180
|
-
errorMessage: message || 'BigInt must be non-positive',
|
|
181
|
-
jsonSchema: { ...this.config.jsonSchema, maximum: 0n }
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
/**
|
|
185
|
-
* Create a new validator with strict greater than constraint
|
|
186
|
-
* Zod 4 API parity - strictly greater than (not equal to)
|
|
187
|
-
*/
|
|
188
|
-
gt(value, message) {
|
|
189
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
190
|
-
return new VldBigInt({
|
|
191
|
-
...this.config,
|
|
192
|
-
checks: [...this.config.checks, (v) => v > compareValue],
|
|
193
|
-
errorMessage: message || `BigInt must be greater than ${compareValue}`,
|
|
194
|
-
jsonSchema: { ...this.config.jsonSchema, exclusiveMinimum: compareValue }
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* Create a new validator with strict less than constraint
|
|
199
|
-
* Zod 4 API parity - strictly less than (not equal to)
|
|
200
|
-
*/
|
|
201
|
-
lt(value, message) {
|
|
202
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
203
|
-
return new VldBigInt({
|
|
204
|
-
...this.config,
|
|
205
|
-
checks: [...this.config.checks, (v) => v < compareValue],
|
|
206
|
-
errorMessage: message || `BigInt must be less than ${compareValue}`,
|
|
207
|
-
jsonSchema: { ...this.config.jsonSchema, exclusiveMaximum: compareValue }
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Create a new validator with greater than or equal constraint
|
|
212
|
-
* Zod 4 API parity - alias for min()
|
|
213
|
-
*/
|
|
214
|
-
gte(value, message) {
|
|
215
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
216
|
-
return this.min(compareValue, message);
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Create a new validator with less than or equal constraint
|
|
220
|
-
* Zod 4 API parity - alias for max()
|
|
221
|
-
*/
|
|
222
|
-
lte(value, message) {
|
|
223
|
-
const compareValue = typeof value === 'bigint' ? value : BigInt(value);
|
|
224
|
-
return this.max(compareValue, message);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export { VldBigInt };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as i}from"./base.js";import{getMessages as n}from"../locales/runtime.js";import{V as t}from"../chunks/errors-core-Cqi4-9ew.js";function s(e){return new t([{code:"invalid_type",path:[],message:e}])}class VldBigInt extends e{constructor(e){super(e?.validatorType||i.BIGINT),this.config={checks:e?.checks||[],errorMessage:e?.errorMessage,jsonSchema:e?.jsonSchema},this._checks=this.config.checks,this._fastCheckMode=this.detectFastCheckMode()}get jsonSchema(){return this.config.jsonSchema}get isSimple(){return 0===this._checks.length}static create(){return new VldBigInt}detectFastCheckMode(){if(0===this._checks.length)return"none";if(1!==this._checks.length)return;const e=this.config.jsonSchema;return 0n===e?.exclusiveMinimum&&void 0===e.minimum&&void 0===e.maximum?"positive":0n===e?.exclusiveMaximum&&void 0===e.minimum&&void 0===e.maximum?"negative":0n===e?.minimum&&void 0===e.maximum&&void 0===e.exclusiveMaximum?"nonnegative":0n===e?.maximum&&void 0===e.minimum&&void 0===e.exclusiveMinimum?"nonpositive":void 0}getValidationError(){return new Error(this.config.errorMessage||n().invalidBigint)}parse(e){if("bigint"!=typeof e)throw this.getValidationError();return this.parseKnownBigInt(e)}parseKnownBigInt(e){switch(this._fastCheckMode){case"none":return e;case"positive":if(e>0n)return e;throw this.getValidationError();case"negative":if(e<0n)return e;throw this.getValidationError();case"nonnegative":if(e>=0n)return e;throw this.getValidationError();case"nonpositive":if(e<=0n)return e;throw this.getValidationError()}for(const i of this._checks)if(!i(e))throw this.getValidationError();return e}safeParse(e){if("bigint"!=typeof e)return{success:!1,error:s(this.getValidationError().message)};try{return{success:!0,data:this.parseKnownBigInt(e)}}catch(e){return{success:!1,error:s(e.message)}}}min(e,i){return new VldBigInt({...this.config,checks:[...this.config.checks,i=>i>=e],errorMessage:i||`BigInt must be at least ${e}`,jsonSchema:{...this.config.jsonSchema,minimum:e}})}max(e,i){return new VldBigInt({...this.config,checks:[...this.config.checks,i=>i<=e],errorMessage:i||`BigInt must be at most ${e}`,jsonSchema:{...this.config.jsonSchema,maximum:e}})}positive(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e>0n],errorMessage:e||"BigInt must be positive",jsonSchema:{...this.config.jsonSchema,exclusiveMinimum:0n}})}negative(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e<0n],errorMessage:e||"BigInt must be negative",jsonSchema:{...this.config.jsonSchema,exclusiveMaximum:0n}})}nonnegative(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e>=0n],errorMessage:e||"BigInt must be non-negative",jsonSchema:{...this.config.jsonSchema,minimum:0n}})}nonpositive(e){return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e<=0n],errorMessage:e||"BigInt must be non-positive",jsonSchema:{...this.config.jsonSchema,maximum:0n}})}gt(e,i){const n="bigint"==typeof e?e:BigInt(e);return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e>n],errorMessage:i||`BigInt must be greater than ${n}`,jsonSchema:{...this.config.jsonSchema,exclusiveMinimum:n}})}lt(e,i){const n="bigint"==typeof e?e:BigInt(e);return new VldBigInt({...this.config,checks:[...this.config.checks,e=>e<n],errorMessage:i||`BigInt must be less than ${n}`,jsonSchema:{...this.config.jsonSchema,exclusiveMaximum:n}})}gte(e,i){const n="bigint"==typeof e?e:BigInt(e);return this.min(n,i)}lte(e,i){const n="bigint"==typeof e?e:BigInt(e);return this.max(n,i)}}export{VldBigInt};
|
|
@@ -1,142 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createBooleanError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_boolean', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable boolean validator
|
|
10
|
-
*/
|
|
11
|
-
class VldBoolean extends VldBase {
|
|
12
|
-
/**
|
|
13
|
-
* Protected constructor to allow extension while maintaining immutability
|
|
14
|
-
*/
|
|
15
|
-
constructor(errorMessage, validatorType) {
|
|
16
|
-
super(validatorType || VLD_VALIDATOR_TYPES.BOOLEAN);
|
|
17
|
-
this.errorMessage = errorMessage;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Returns true if this is a simple boolean validator with no custom checks
|
|
21
|
-
* Used by VldObject for optimized fast-path dispatch
|
|
22
|
-
*/
|
|
23
|
-
get isSimple() {
|
|
24
|
-
return true;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Create a new boolean validator
|
|
28
|
-
*/
|
|
29
|
-
static create() {
|
|
30
|
-
return new VldBoolean();
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Parse and validate a boolean value
|
|
34
|
-
*/
|
|
35
|
-
parse(value) {
|
|
36
|
-
if (typeof value !== 'boolean') {
|
|
37
|
-
throw new Error(this.errorMessage || getMessages().invalidBoolean);
|
|
38
|
-
}
|
|
39
|
-
return value;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Parse a value that has already passed the boolean type guard.
|
|
43
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
44
|
-
*/
|
|
45
|
-
parseKnownBoolean(value) {
|
|
46
|
-
return value;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Safely parse and validate a boolean value
|
|
50
|
-
*/
|
|
51
|
-
safeParse(value) {
|
|
52
|
-
if (typeof value === 'boolean') {
|
|
53
|
-
return { success: true, data: value };
|
|
54
|
-
}
|
|
55
|
-
return {
|
|
56
|
-
success: false,
|
|
57
|
-
error: createBooleanError(this.errorMessage || getMessages().invalidBoolean)
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Create a new validator that only accepts true
|
|
62
|
-
*/
|
|
63
|
-
true(message) {
|
|
64
|
-
return new VldTrue(message || 'Value must be true');
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Create a new validator that only accepts false
|
|
68
|
-
*/
|
|
69
|
-
false(message) {
|
|
70
|
-
return new VldFalse(message || 'Value must be false');
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Validator that only accepts true
|
|
75
|
-
*/
|
|
76
|
-
class VldTrue extends VldBoolean {
|
|
77
|
-
constructor(message) {
|
|
78
|
-
super(message);
|
|
79
|
-
this.message = message;
|
|
80
|
-
}
|
|
81
|
-
get isSimple() {
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
parse(value) {
|
|
85
|
-
if (value !== true) {
|
|
86
|
-
throw new Error(this.message);
|
|
87
|
-
}
|
|
88
|
-
return true;
|
|
89
|
-
}
|
|
90
|
-
parseKnownBoolean(value) {
|
|
91
|
-
if (value !== true) {
|
|
92
|
-
throw new Error(this.message);
|
|
93
|
-
}
|
|
94
|
-
return true;
|
|
95
|
-
}
|
|
96
|
-
// BUG-003 FIX: Override safeParse to ensure true validation
|
|
97
|
-
safeParse(value) {
|
|
98
|
-
if (value !== true) {
|
|
99
|
-
return {
|
|
100
|
-
success: false,
|
|
101
|
-
error: createBooleanError(this.message)
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
return { success: true, data: true };
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Validator that only accepts false
|
|
109
|
-
*/
|
|
110
|
-
class VldFalse extends VldBoolean {
|
|
111
|
-
constructor(message) {
|
|
112
|
-
super(message);
|
|
113
|
-
this.message = message;
|
|
114
|
-
}
|
|
115
|
-
get isSimple() {
|
|
116
|
-
return false;
|
|
117
|
-
}
|
|
118
|
-
parse(value) {
|
|
119
|
-
if (value !== false) {
|
|
120
|
-
throw new Error(this.message);
|
|
121
|
-
}
|
|
122
|
-
return false;
|
|
123
|
-
}
|
|
124
|
-
parseKnownBoolean(value) {
|
|
125
|
-
if (value !== false) {
|
|
126
|
-
throw new Error(this.message);
|
|
127
|
-
}
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
// BUG-003 FIX: Override safeParse to ensure false validation
|
|
131
|
-
safeParse(value) {
|
|
132
|
-
if (value !== false) {
|
|
133
|
-
return {
|
|
134
|
-
success: false,
|
|
135
|
-
error: createBooleanError(this.message)
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
return { success: true, data: false };
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export { VldBoolean };
|
|
1
|
+
import{VldBase as e,VLD_VALIDATOR_TYPES as s}from"./base.js";import{V as r,c as t,g as a}from"../chunks/errors-core-Cqi4-9ew.js";class VldBoolean extends e{constructor(e,r){super(r||s.BOOLEAN),this.errorMessage=e}get isSimple(){return!0}static create(){return new VldBoolean}parse(e){if("boolean"!=typeof e)throw new r([t("boolean",a(e),this.errorMessage)]);return e}parseKnownBoolean(e){return e}safeParse(e){return"boolean"==typeof e?{success:!0,data:e}:{success:!1,error:new r([t("boolean",a(e),this.errorMessage)])}}true(e){return new VldTrue(e||"Value must be true")}false(e){return new VldFalse(e||"Value must be false")}}class VldTrue extends VldBoolean{constructor(e){super(e),this.message=e}get isSimple(){return!1}parse(e){if(!0!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!0}parseKnownBoolean(e){if(!0!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!0}safeParse(e){return!0!==e?{success:!1,error:new r([{code:"custom",path:[],message:this.message}])}:{success:!0,data:!0}}}class VldFalse extends VldBoolean{constructor(e){super(e),this.message=e}get isSimple(){return!1}parse(e){if(!1!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!1}parseKnownBoolean(e){if(!1!==e)throw new r([{code:"custom",path:[],message:this.message}]);return!1}safeParse(e){return!1!==e?{success:!1,error:new r([{code:"custom",path:[],message:this.message}])}:{success:!0,data:!1}}}export{VldBoolean};
|
package/dist/validators/codec.js
CHANGED
|
@@ -1,200 +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
|
-
* Codec validator for bidirectional transformations
|
|
8
|
-
* Enables converting between different data representations
|
|
9
|
-
*/
|
|
10
|
-
class VldCodec extends VldBase {
|
|
11
|
-
constructor(inputValidator, outputValidator, codecTransform) {
|
|
12
|
-
super(VLD_VALIDATOR_TYPES.CODEC);
|
|
13
|
-
this.inputValidator = inputValidator;
|
|
14
|
-
this.outputValidator = outputValidator;
|
|
15
|
-
this.codecTransform = codecTransform;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Parse (decode) a value from input to output format
|
|
19
|
-
*/
|
|
20
|
-
parse(value) {
|
|
21
|
-
const result = this.safeParse(value);
|
|
22
|
-
if (!result.success) {
|
|
23
|
-
throw result.error;
|
|
24
|
-
}
|
|
25
|
-
return result.data;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Safely parse (decode) a value from input to output format
|
|
29
|
-
*/
|
|
30
|
-
safeParse(value) {
|
|
31
|
-
// First validate the input
|
|
32
|
-
const inputResult = this.inputValidator.safeParse(value);
|
|
33
|
-
if (!inputResult.success) {
|
|
34
|
-
return { success: false, error: inputResult.error };
|
|
35
|
-
}
|
|
36
|
-
try {
|
|
37
|
-
// Then decode to output
|
|
38
|
-
const decoded = this.codecTransform.decode(inputResult.data);
|
|
39
|
-
// Handle async decode
|
|
40
|
-
if (decoded instanceof Promise) {
|
|
41
|
-
throw new VldError([
|
|
42
|
-
createIssue('custom', [], getMessages().codecAsyncNotSupported)
|
|
43
|
-
]);
|
|
44
|
-
}
|
|
45
|
-
// Validate the decoded output
|
|
46
|
-
return this.outputValidator.safeParse(decoded);
|
|
47
|
-
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
if (error instanceof VldError) {
|
|
50
|
-
return { success: false, error };
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
success: false,
|
|
54
|
-
error: new VldError([
|
|
55
|
-
createIssue('custom', [], getMessages().codecDecodeFailed)
|
|
56
|
-
])
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Create a new codec with input and output directions swapped.
|
|
62
|
-
*/
|
|
63
|
-
invert() {
|
|
64
|
-
return new VldCodec(this.outputValidator, this.inputValidator, {
|
|
65
|
-
decode: this.codecTransform.encode,
|
|
66
|
-
encode: this.codecTransform.decode
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Encode a value from output back to input format
|
|
71
|
-
*/
|
|
72
|
-
encode(value) {
|
|
73
|
-
const result = this.safeEncode(value);
|
|
74
|
-
if (!result.success) {
|
|
75
|
-
throw result.error;
|
|
76
|
-
}
|
|
77
|
-
return result.data;
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Safely encode a value from output back to input format
|
|
81
|
-
*/
|
|
82
|
-
safeEncode(value) {
|
|
83
|
-
// First validate the output
|
|
84
|
-
const outputResult = this.outputValidator.safeParse(value);
|
|
85
|
-
if (!outputResult.success) {
|
|
86
|
-
return { success: false, error: outputResult.error };
|
|
87
|
-
}
|
|
88
|
-
try {
|
|
89
|
-
// Then encode to input
|
|
90
|
-
const encoded = this.codecTransform.encode(outputResult.data);
|
|
91
|
-
// Handle async encode
|
|
92
|
-
if (encoded instanceof Promise) {
|
|
93
|
-
throw new VldError([
|
|
94
|
-
createIssue('custom', [], getMessages().codecAsyncNotSupported)
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
97
|
-
// Validate the encoded input
|
|
98
|
-
return this.inputValidator.safeParse(encoded);
|
|
99
|
-
}
|
|
100
|
-
catch (error) {
|
|
101
|
-
if (error instanceof VldError) {
|
|
102
|
-
return { success: false, error };
|
|
103
|
-
}
|
|
104
|
-
return {
|
|
105
|
-
success: false,
|
|
106
|
-
error: new VldError([
|
|
107
|
-
createIssue('custom', [], getMessages().codecEncodeFailed)
|
|
108
|
-
])
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
/**
|
|
113
|
-
* Async version of parse (decode)
|
|
114
|
-
*/
|
|
115
|
-
async parseAsync(value) {
|
|
116
|
-
const result = await this.safeParseAsync(value);
|
|
117
|
-
if (!result.success) {
|
|
118
|
-
throw result.error;
|
|
119
|
-
}
|
|
120
|
-
return result.data;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* Async version of safeParse (decode)
|
|
124
|
-
*/
|
|
125
|
-
async safeParseAsync(value) {
|
|
126
|
-
// First validate the input
|
|
127
|
-
const inputResult = this.inputValidator.safeParse(value);
|
|
128
|
-
if (!inputResult.success) {
|
|
129
|
-
return { success: false, error: inputResult.error };
|
|
130
|
-
}
|
|
131
|
-
try {
|
|
132
|
-
// Then decode to output (supports async)
|
|
133
|
-
const decoded = await this.codecTransform.decode(inputResult.data);
|
|
134
|
-
// Validate the decoded output
|
|
135
|
-
return this.outputValidator.safeParse(decoded);
|
|
136
|
-
}
|
|
137
|
-
catch (error) {
|
|
138
|
-
if (error instanceof VldError) {
|
|
139
|
-
return { success: false, error };
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
success: false,
|
|
143
|
-
error: new VldError([
|
|
144
|
-
createIssue('custom', [], getMessages().codecDecodeFailed)
|
|
145
|
-
])
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Async version of encode
|
|
151
|
-
*/
|
|
152
|
-
async encodeAsync(value) {
|
|
153
|
-
const result = await this.safeEncodeAsync(value);
|
|
154
|
-
if (!result.success) {
|
|
155
|
-
throw result.error;
|
|
156
|
-
}
|
|
157
|
-
return result.data;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Async version of safeEncode
|
|
161
|
-
*/
|
|
162
|
-
async safeEncodeAsync(value) {
|
|
163
|
-
// First validate the output
|
|
164
|
-
const outputResult = this.outputValidator.safeParse(value);
|
|
165
|
-
if (!outputResult.success) {
|
|
166
|
-
return { success: false, error: outputResult.error };
|
|
167
|
-
}
|
|
168
|
-
try {
|
|
169
|
-
// Then encode to input (supports async)
|
|
170
|
-
const encoded = await this.codecTransform.encode(outputResult.data);
|
|
171
|
-
// Validate the encoded input
|
|
172
|
-
return this.inputValidator.safeParse(encoded);
|
|
173
|
-
}
|
|
174
|
-
catch (error) {
|
|
175
|
-
if (error instanceof VldError) {
|
|
176
|
-
return { success: false, error };
|
|
177
|
-
}
|
|
178
|
-
return {
|
|
179
|
-
success: false,
|
|
180
|
-
error: new VldError([
|
|
181
|
-
createIssue('custom', [], getMessages().codecEncodeFailed)
|
|
182
|
-
])
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Create a codec validator
|
|
188
|
-
*/
|
|
189
|
-
static create(inputValidator, outputValidator, codecTransform) {
|
|
190
|
-
return new VldCodec(inputValidator, outputValidator, codecTransform);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Create a codec with input and output directions swapped.
|
|
194
|
-
*/
|
|
195
|
-
static invert(codec) {
|
|
196
|
-
return codec.invert();
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
export { VldCodec };
|
|
1
|
+
import{VldBase as r,VLD_VALIDATOR_TYPES as s}from"./base.js";import{createIssue as e}from"../errors.js";import{getMessages as t}from"../locales/runtime.js";import{V as c}from"../chunks/errors-core-Cqi4-9ew.js";class VldCodec extends r{constructor(r,e,t){super(s.CODEC),this.inputValidator=r,this.outputValidator=e,this.codecTransform=t}parse(r){const s=this.safeParse(r);if(!s.success)throw s.error;return s.data}safeParse(r){const s=this.inputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=this.codecTransform.decode(s.data);if(r instanceof Promise)throw new c([e("custom",[],t().codecAsyncNotSupported)]);return this.outputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecDecodeFailed)])}}}invert(){return new VldCodec(this.outputValidator,this.inputValidator,{decode:this.codecTransform.encode,encode:this.codecTransform.decode})}encode(r){const s=this.safeEncode(r);if(!s.success)throw s.error;return s.data}safeEncode(r){const s=this.outputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=this.codecTransform.encode(s.data);if(r instanceof Promise)throw new c([e("custom",[],t().codecAsyncNotSupported)]);return this.inputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecEncodeFailed)])}}}async parseAsync(r){const s=await this.safeParseAsync(r);if(!s.success)throw s.error;return s.data}async safeParseAsync(r){const s=this.inputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=await this.codecTransform.decode(s.data);return this.outputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecDecodeFailed)])}}}async encodeAsync(r){const s=await this.safeEncodeAsync(r);if(!s.success)throw s.error;return s.data}async safeEncodeAsync(r){const s=this.outputValidator.safeParse(r);if(!s.success)return{success:!1,error:s.error};try{const r=await this.codecTransform.encode(s.data);return this.inputValidator.safeParse(r)}catch(r){return r instanceof c?{success:!1,error:r}:{success:!1,error:new c([e("custom",[],t().codecEncodeFailed)])}}}static create(r,s,e){return new VldCodec(r,s,e)}static invert(r){return r.invert()}}export{VldCodec};
|