@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,120 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Default truthy and falsy value sets
|
|
6
|
-
*/
|
|
7
|
-
const DEFAULT_TRUTHY = ['true', '1', 'yes', 'on', 'y', 'enabled'];
|
|
8
|
-
const DEFAULT_FALSY = ['false', '0', 'no', 'off', 'n', 'disabled'];
|
|
9
|
-
/**
|
|
10
|
-
* Immutable string boolean validator for flexible boolean parsing
|
|
11
|
-
* Supports string representations like "true", "yes", "on", "1", etc.
|
|
12
|
-
*/
|
|
13
|
-
class VldStringBool extends VldBase {
|
|
14
|
-
constructor(options) {
|
|
15
|
-
super(VLD_VALIDATOR_TYPES.STRING_BOOL);
|
|
16
|
-
this.options = options;
|
|
17
|
-
this.truthyValues = options.truthy ?? DEFAULT_TRUTHY;
|
|
18
|
-
this.falsyValues = options.falsy ?? DEFAULT_FALSY;
|
|
19
|
-
const normalizedTruthy = this.normalizeValues(this.truthyValues);
|
|
20
|
-
const normalizedFalsy = this.normalizeValues(this.falsyValues);
|
|
21
|
-
this.normalizedTruthySet = new Set(normalizedTruthy);
|
|
22
|
-
this.normalizedFalsySet = new Set(normalizedFalsy);
|
|
23
|
-
this.validValuesText = [...normalizedTruthy, ...normalizedFalsy].join(', ');
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Create a new string boolean validator
|
|
27
|
-
*/
|
|
28
|
-
static create(options = {}) {
|
|
29
|
-
return new VldStringBool(options);
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Normalize a string value based on case sensitivity setting
|
|
33
|
-
*/
|
|
34
|
-
normalizeValue(value) {
|
|
35
|
-
return this.options.caseSensitive ? value : value.toLowerCase();
|
|
36
|
-
}
|
|
37
|
-
normalizeValues(values) {
|
|
38
|
-
if (this.options.caseSensitive) {
|
|
39
|
-
return values;
|
|
40
|
-
}
|
|
41
|
-
return values.map(value => value.toLowerCase());
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Parse and validate a string boolean value
|
|
45
|
-
*/
|
|
46
|
-
parse(value) {
|
|
47
|
-
// First, ensure we have a string
|
|
48
|
-
if (typeof value !== 'string') {
|
|
49
|
-
// Also accept actual boolean values for convenience
|
|
50
|
-
if (typeof value === 'boolean') {
|
|
51
|
-
return value;
|
|
52
|
-
}
|
|
53
|
-
throw new Error(getMessages().stringExpected(typeof value, 'string'));
|
|
54
|
-
}
|
|
55
|
-
const normalized = this.normalizeValue(value);
|
|
56
|
-
// Check if value is in truthy set
|
|
57
|
-
if (this.normalizedTruthySet.has(normalized)) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
// Check if value is in falsy set
|
|
61
|
-
if (this.normalizedFalsySet.has(normalized)) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
// Value is not recognized
|
|
65
|
-
throw new Error(getMessages().stringBoolExpected(this.validValuesText, value));
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Safely parse and validate a string boolean value
|
|
69
|
-
*/
|
|
70
|
-
safeParse(value) {
|
|
71
|
-
try {
|
|
72
|
-
const result = this.parse(value);
|
|
73
|
-
return { success: true, data: result };
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
return {
|
|
77
|
-
success: false,
|
|
78
|
-
error: error
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Create a new validator with custom truthy values
|
|
84
|
-
*/
|
|
85
|
-
withTruthy(values) {
|
|
86
|
-
return new VldStringBool({
|
|
87
|
-
...this.options,
|
|
88
|
-
truthy: values
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Create a new validator with custom falsy values
|
|
93
|
-
*/
|
|
94
|
-
withFalsy(values) {
|
|
95
|
-
return new VldStringBool({
|
|
96
|
-
...this.options,
|
|
97
|
-
falsy: values
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Create a new validator with case-sensitive matching
|
|
102
|
-
*/
|
|
103
|
-
caseSensitive() {
|
|
104
|
-
return new VldStringBool({
|
|
105
|
-
...this.options,
|
|
106
|
-
caseSensitive: true
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Create a new validator with case-insensitive matching (default)
|
|
111
|
-
*/
|
|
112
|
-
caseInsensitive() {
|
|
113
|
-
return new VldStringBool({
|
|
114
|
-
...this.options,
|
|
115
|
-
caseSensitive: false
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export { VldStringBool };
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as s}from"../locales/runtime.js";const r=["true","1","yes","on","y","enabled"],i=["false","0","no","off","n","disabled"];class VldStringBool extends t{constructor(t){super(e.STRING_BOOL),this.options=t,this.truthyValues=t.truthy??r,this.falsyValues=t.falsy??i;const s=this.normalizeValues(this.truthyValues),o=this.normalizeValues(this.falsyValues);this.normalizedTruthySet=new Set(s),this.normalizedFalsySet=new Set(o),this.validValuesText=[...s,...o].join(", ")}static create(t={}){return new VldStringBool(t)}normalizeValue(t){return this.options.caseSensitive?t:t.toLowerCase()}normalizeValues(t){return this.options.caseSensitive?t:t.map(t=>t.toLowerCase())}parse(t){if("string"!=typeof t){if("boolean"==typeof t)return t;throw new Error(s().stringExpected(typeof t,"string"))}const e=this.normalizeValue(t);if(this.normalizedTruthySet.has(e))return!0;if(this.normalizedFalsySet.has(e))return!1;throw new Error(s().stringBoolExpected(this.validValuesText,t))}safeParse(t){try{return{success:!0,data:this.parse(t)}}catch(t){return{success:!1,error:t}}}withTruthy(t){return new VldStringBool({...this.options,truthy:t})}withFalsy(t){return new VldStringBool({...this.options,falsy:t})}caseSensitive(){return new VldStringBool({...this.options,caseSensitive:!0})}caseInsensitive(){return new VldStringBool({...this.options,caseSensitive:!1})}}export{VldStringBool};
|
|
@@ -5,64 +5,57 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { VldBase } from './base.js';
|
|
7
7
|
import type { ParseResult } from './base.js';
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
readonly xid: RegExp;
|
|
42
|
-
readonly guid: RegExp;
|
|
43
|
-
readonly httpUrl: RegExp;
|
|
44
|
-
readonly ksuid: RegExp;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Generic string format validator
|
|
48
|
-
*/
|
|
8
|
+
export type UUIDVersion = 'v1' | 'v2' | 'v3' | 'v4' | 'v5' | 'v6' | 'v7' | 'v8';
|
|
9
|
+
export interface URLFormatOptions {
|
|
10
|
+
hostname?: RegExp;
|
|
11
|
+
normalize?: boolean;
|
|
12
|
+
protocol?: RegExp;
|
|
13
|
+
}
|
|
14
|
+
export interface ISOTimeOptions {
|
|
15
|
+
precision?: number | null;
|
|
16
|
+
}
|
|
17
|
+
export interface ISODateTimeOptions extends ISOTimeOptions {
|
|
18
|
+
local?: boolean;
|
|
19
|
+
offset?: boolean;
|
|
20
|
+
}
|
|
21
|
+
declare function uuidRegex(version?: number): RegExp;
|
|
22
|
+
declare function timeRegex(options?: ISOTimeOptions): RegExp;
|
|
23
|
+
declare function dateTimeRegex(options?: ISODateTimeOptions): RegExp;
|
|
24
|
+
declare function stringRegex(options?: {
|
|
25
|
+
minimum?: number;
|
|
26
|
+
maximum?: number;
|
|
27
|
+
}): RegExp;
|
|
28
|
+
declare function macRegex(delimiter?: string): RegExp;
|
|
29
|
+
type StaticRegexName = 'base64' | 'base64url' | 'bigint' | 'boolean' | 'browserEmail' | 'cidrv4' | 'cidrv6' | 'creditCard' | 'cuid' | 'cuid2' | 'date' | 'domain' | 'duration' | 'e164' | 'email' | 'extendedDuration' | 'guid' | 'hex' | 'hostname' | 'html5Email' | 'httpProtocol' | 'httpUrl' | 'idnEmail' | 'integer' | 'ipv4' | 'ipv6' | 'jwt' | 'ksuid' | 'lowercase' | 'md5' | 'md5_base64' | 'md5_base64url' | 'md5_hex' | 'nanoid' | 'null' | 'number' | 'rfc5322Email' | 'sha1' | 'sha1_base64' | 'sha1_base64url' | 'sha1_hex' | 'sha256' | 'sha256_base64' | 'sha256_base64url' | 'sha256_hex' | 'sha384' | 'sha384_base64' | 'sha384_base64url' | 'sha384_hex' | 'sha512' | 'sha512_base64' | 'sha512_base64url' | 'sha512_hex' | 'ulid' | 'undefined' | 'unicodeEmail' | 'uppercase' | 'uuid4' | 'uuid6' | 'uuid7' | 'xid';
|
|
30
|
+
export type RegexNamespace = Readonly<Record<StaticRegexName, RegExp> & {
|
|
31
|
+
datetime: typeof dateTimeRegex;
|
|
32
|
+
emoji: () => RegExp;
|
|
33
|
+
mac: typeof macRegex;
|
|
34
|
+
string: typeof stringRegex;
|
|
35
|
+
time: typeof timeRegex;
|
|
36
|
+
uuid: typeof uuidRegex;
|
|
37
|
+
}>;
|
|
38
|
+
/** Public regex helpers matching the current Zod 4 namespace. */
|
|
39
|
+
declare const REGEXES: RegexNamespace;
|
|
40
|
+
/** Generic string format validator. */
|
|
49
41
|
export declare class VldStringFormat extends VldBase<unknown, string> {
|
|
50
42
|
readonly _format: string;
|
|
51
|
-
readonly _validator: (
|
|
43
|
+
readonly _validator: (value: string) => boolean;
|
|
52
44
|
readonly _errorMessage?: string | undefined;
|
|
53
|
-
|
|
54
|
-
|
|
45
|
+
private readonly _normalize;
|
|
46
|
+
private readonly _patternSource?;
|
|
47
|
+
constructor(_format: string, _validator: (value: string) => boolean, _errorMessage?: string | undefined, _normalize?: (value: string) => string, _patternSource?: string | undefined);
|
|
48
|
+
static create(format: string, validator: (value: string) => boolean, errorMessage?: string, normalize?: (value: string) => string, patternSource?: string): VldStringFormat;
|
|
49
|
+
private _createFormatIssue;
|
|
55
50
|
parse(value: unknown): string;
|
|
56
51
|
safeParse(value: unknown): ParseResult<string>;
|
|
57
52
|
}
|
|
58
|
-
/**
|
|
59
|
-
* Top-level string format validators
|
|
60
|
-
*/
|
|
61
53
|
export declare const email: (options?: {
|
|
62
54
|
pattern?: RegExp;
|
|
63
55
|
}) => VldStringFormat;
|
|
56
|
+
export declare const url: (options?: URLFormatOptions) => VldStringFormat;
|
|
64
57
|
export declare const uuid: (options?: {
|
|
65
|
-
version?:
|
|
58
|
+
version?: UUIDVersion;
|
|
66
59
|
}) => VldStringFormat;
|
|
67
60
|
export declare const uuidv4: () => VldStringFormat;
|
|
68
61
|
export declare const uuidv6: () => VldStringFormat;
|
|
@@ -79,7 +72,9 @@ export declare const cuid2: () => VldStringFormat;
|
|
|
79
72
|
export declare const ulid: () => VldStringFormat;
|
|
80
73
|
export declare const ipv4: () => VldStringFormat;
|
|
81
74
|
export declare const ipv6: () => VldStringFormat;
|
|
82
|
-
export declare const mac: (
|
|
75
|
+
export declare const mac: (options?: {
|
|
76
|
+
delimiter?: string;
|
|
77
|
+
}) => VldStringFormat;
|
|
83
78
|
export declare const cidrv4: () => VldStringFormat;
|
|
84
79
|
export declare const cidrv6: () => VldStringFormat;
|
|
85
80
|
export declare const e164: () => VldStringFormat;
|
|
@@ -88,19 +83,20 @@ export declare const guid: () => VldStringFormat;
|
|
|
88
83
|
export declare const httpUrl: () => VldStringFormat;
|
|
89
84
|
export declare const ksuid: () => VldStringFormat;
|
|
90
85
|
export declare const hash: (algorithm: "md5" | "sha1" | "sha256" | "sha384" | "sha512") => VldStringFormat;
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
86
|
+
export declare function isValidCreditCard(input: string): boolean;
|
|
87
|
+
export declare const creditCard: (params?: {
|
|
88
|
+
message?: string;
|
|
89
|
+
}) => VldStringFormat;
|
|
94
90
|
export declare const iso: {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
91
|
+
readonly ZodISODate: typeof VldStringFormat;
|
|
92
|
+
readonly ZodISODateTime: typeof VldStringFormat;
|
|
93
|
+
readonly ZodISODuration: typeof VldStringFormat;
|
|
94
|
+
readonly ZodISOTime: typeof VldStringFormat;
|
|
95
|
+
readonly date: () => VldStringFormat;
|
|
96
|
+
readonly time: (options?: ISOTimeOptions) => VldStringFormat;
|
|
97
|
+
readonly datetime: (options?: ISODateTimeOptions) => VldStringFormat;
|
|
98
|
+
readonly dateTime: (options?: ISODateTimeOptions) => VldStringFormat;
|
|
99
|
+
readonly duration: () => VldStringFormat;
|
|
101
100
|
};
|
|
102
|
-
|
|
103
|
-
* Custom format validator
|
|
104
|
-
*/
|
|
105
|
-
export declare const stringFormat: (name: string, validator: ((val: string) => boolean) | RegExp) => VldStringFormat;
|
|
101
|
+
export declare const stringFormat: (name: string, validator: ((value: string) => boolean) | RegExp) => VldStringFormat;
|
|
106
102
|
export { REGEXES as regexes };
|
|
@@ -1,135 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Top-level string format validators
|
|
6
|
-
* Part of Zod 4 API parity implementation
|
|
7
|
-
* Provides convenient validators for common string formats
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Pre-compiled regex patterns for performance
|
|
11
|
-
*/
|
|
12
|
-
const REGEXES = {
|
|
13
|
-
email: /^[^\s@]+@[^\s@]+\.[^\s@]+$/,
|
|
14
|
-
uuid: /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
15
|
-
uuidv4: /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
16
|
-
uuidv6: /^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
17
|
-
uuidv7: /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
18
|
-
hostname: /^(?=.{1,253}$)(?:(?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+[a-zA-Z]{2,63}$/,
|
|
19
|
-
emoji: /^(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200D(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*$/u,
|
|
20
|
-
base64: /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,
|
|
21
|
-
base64url: /^[A-Za-z0-9_-]*$/,
|
|
22
|
-
hex: /^[0-9a-fA-F]*$/,
|
|
23
|
-
jwt: /^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,
|
|
24
|
-
nanoid: /^[A-Za-z0-9_-]{21}$/,
|
|
25
|
-
cuid: /^c[^\s-]{8,}$/i,
|
|
26
|
-
cuid2: /^[0-9a-z]+$/,
|
|
27
|
-
ulid: /^[0-9A-HJKMNP-TV-Z]{26}$/,
|
|
28
|
-
ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
|
|
29
|
-
ipv6: /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/,
|
|
30
|
-
mac: /^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$/,
|
|
31
|
-
cidrv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/,
|
|
32
|
-
cidrv6: /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^(?:[0-9a-fA-F]{1,4}:){1,7}:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,
|
|
33
|
-
e164: /^\+[1-9]\d{1,14}$/,
|
|
34
|
-
isoDate: /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/,
|
|
35
|
-
isoTime: /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$/,
|
|
36
|
-
isoDateTime: /^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z?$/,
|
|
37
|
-
md5: /^[a-f0-9]{32}$/i,
|
|
38
|
-
sha1: /^[a-f0-9]{40}$/i,
|
|
39
|
-
sha256: /^[a-f0-9]{64}$/i,
|
|
40
|
-
sha384: /^[a-f0-9]{96}$/i,
|
|
41
|
-
sha512: /^[a-f0-9]{128}$/i,
|
|
42
|
-
// Additional formats (Zod v4 parity)
|
|
43
|
-
xid: /^[A-HJKMNP-TV-Z0-9]{20}$/, // ksort's XID format
|
|
44
|
-
guid: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i, // Alias for UUID
|
|
45
|
-
httpUrl: /^https?:\/\/[^\s/$.?#].[^\s]*$/, // HTTP/HTTPS URL
|
|
46
|
-
ksuid: /^[0-9A-Za-z]{27}$/,
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Generic string format validator
|
|
50
|
-
*/
|
|
51
|
-
class VldStringFormat extends VldBase {
|
|
52
|
-
constructor(_format, _validator, _errorMessage) {
|
|
53
|
-
super(VLD_VALIDATOR_TYPES.STRING_FORMAT);
|
|
54
|
-
this._format = _format;
|
|
55
|
-
this._validator = _validator;
|
|
56
|
-
this._errorMessage = _errorMessage;
|
|
57
|
-
}
|
|
58
|
-
static create(format, validator, errorMessage) {
|
|
59
|
-
return new VldStringFormat(format, validator, errorMessage);
|
|
60
|
-
}
|
|
61
|
-
parse(value) {
|
|
62
|
-
if (typeof value !== 'string') {
|
|
63
|
-
throw new Error(getMessages().invalidString);
|
|
64
|
-
}
|
|
65
|
-
if (!this._validator(value)) {
|
|
66
|
-
throw new Error(this._errorMessage || `Invalid ${this._format} format`);
|
|
67
|
-
}
|
|
68
|
-
return value;
|
|
69
|
-
}
|
|
70
|
-
safeParse(value) {
|
|
71
|
-
if (typeof value !== 'string') {
|
|
72
|
-
return {
|
|
73
|
-
success: false,
|
|
74
|
-
error: new Error(getMessages().invalidString)
|
|
75
|
-
};
|
|
76
|
-
}
|
|
77
|
-
if (!this._validator(value)) {
|
|
78
|
-
return {
|
|
79
|
-
success: false,
|
|
80
|
-
error: new Error(this._errorMessage || `Invalid ${this._format} format`)
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
return { success: true, data: value };
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Top-level string format validators
|
|
88
|
-
*/
|
|
89
|
-
const email = (options) => VldStringFormat.create('email', (val) => (options?.pattern ?? REGEXES.email).test(val));
|
|
90
|
-
const uuid = (options) => {
|
|
91
|
-
const pattern = options?.version ? REGEXES[`uuid${options.version}`] : REGEXES.uuid;
|
|
92
|
-
return VldStringFormat.create('uuid', (val) => pattern.test(val));
|
|
93
|
-
};
|
|
94
|
-
const uuidv4 = () => VldStringFormat.create('uuid', (val) => REGEXES.uuidv4.test(val));
|
|
95
|
-
const uuidv6 = () => VldStringFormat.create('uuid', (val) => REGEXES.uuidv6.test(val));
|
|
96
|
-
const uuidv7 = () => VldStringFormat.create('uuid', (val) => REGEXES.uuidv7.test(val));
|
|
97
|
-
const hostname = () => VldStringFormat.create('hostname', (val) => REGEXES.hostname.test(val));
|
|
98
|
-
const emoji = () => VldStringFormat.create('emoji', (val) => REGEXES.emoji.test(val));
|
|
99
|
-
const base64 = () => VldStringFormat.create('base64', (val) => REGEXES.base64.test(val));
|
|
100
|
-
const base64url = () => VldStringFormat.create('base64url', (val) => REGEXES.base64url.test(val));
|
|
101
|
-
const hex = () => VldStringFormat.create('hex', (val) => REGEXES.hex.test(val));
|
|
102
|
-
const jwt = () => VldStringFormat.create('jwt', (val) => REGEXES.jwt.test(val));
|
|
103
|
-
const nanoid = () => VldStringFormat.create('nanoid', (val) => REGEXES.nanoid.test(val));
|
|
104
|
-
const cuid = () => VldStringFormat.create('cuid', (val) => REGEXES.cuid.test(val));
|
|
105
|
-
const cuid2 = () => VldStringFormat.create('cuid2', (val) => REGEXES.cuid2.test(val));
|
|
106
|
-
const ulid = () => VldStringFormat.create('ulid', (val) => REGEXES.ulid.test(val));
|
|
107
|
-
const ipv4 = () => VldStringFormat.create('ipv4', (val) => REGEXES.ipv4.test(val));
|
|
108
|
-
const ipv6 = () => VldStringFormat.create('ipv6', (val) => REGEXES.ipv6.test(val));
|
|
109
|
-
const mac = () => VldStringFormat.create('mac', (val) => REGEXES.mac.test(val));
|
|
110
|
-
const cidrv4 = () => VldStringFormat.create('cidrv4', (val) => REGEXES.cidrv4.test(val));
|
|
111
|
-
const cidrv6 = () => VldStringFormat.create('cidrv6', (val) => REGEXES.cidrv6.test(val));
|
|
112
|
-
const e164 = () => VldStringFormat.create('e164', (val) => REGEXES.e164.test(val));
|
|
113
|
-
const xid = () => VldStringFormat.create('xid', (val) => REGEXES.xid.test(val));
|
|
114
|
-
const guid = () => VldStringFormat.create('guid', (val) => REGEXES.guid.test(val));
|
|
115
|
-
const httpUrl = () => VldStringFormat.create('httpUrl', (val) => REGEXES.httpUrl.test(val));
|
|
116
|
-
const ksuid = () => VldStringFormat.create('ksuid', (val) => REGEXES.ksuid.test(val));
|
|
117
|
-
const hash = (algorithm) => VldStringFormat.create('hash', (val) => REGEXES[algorithm]?.test(val) ?? false, `Invalid ${algorithm} hash`);
|
|
118
|
-
/**
|
|
119
|
-
* ISO date/time validators
|
|
120
|
-
*/
|
|
121
|
-
const iso = {
|
|
122
|
-
date: () => VldStringFormat.create('date', (val) => REGEXES.isoDate.test(val)),
|
|
123
|
-
time: () => VldStringFormat.create('time', (val) => REGEXES.isoTime.test(val)),
|
|
124
|
-
dateTime: (_options) => VldStringFormat.create('datetime', (val) => REGEXES.isoDateTime.test(val)),
|
|
125
|
-
duration: () => VldStringFormat.create('duration', (val) => /^-?P(?!$)(?:\d+(?:\.\d+)?Y)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?W)?(?:\d+(?:\.\d+)?D)?(?:T(?=\d)(?:\d+(?:\.\d+)?H)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?S)?)?$/.test(val)),
|
|
126
|
-
};
|
|
127
|
-
/**
|
|
128
|
-
* Custom format validator
|
|
129
|
-
*/
|
|
130
|
-
const stringFormat = (name, validator) => {
|
|
131
|
-
const fn = validator instanceof RegExp ? (val) => validator.test(val) : validator;
|
|
132
|
-
return VldStringFormat.create(name, fn);
|
|
133
|
-
};
|
|
134
|
-
|
|
135
|
-
export { VldStringFormat, base64, base64url, cidrv4, cidrv6, cuid, cuid2, e164, email, emoji, guid, hash, hex, hostname, httpUrl, ipv4, ipv6, iso, jwt, ksuid, mac, nanoid, REGEXES as regexes, stringFormat, ulid, uuid, uuidv4, uuidv6, uuidv7, xid };
|
|
1
|
+
import{VldBase as t,VLD_VALIDATOR_TYPES as a}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{i as e}from"../chunks/ip-validation-g5LWgy9t.js";import{V as i,c as n,g as d}from"../chunks/errors-core-Cqi4-9ew.js";const o="(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))";function s(t,a){return t.lastIndex=0,t.test(a)}function c(t,a){return new RegExp(`^[A-Za-z0-9+/]{${t}}${a}$`)}function u(t){return new RegExp(`^[A-Za-z0-9_-]{${t}}$`)}function m(t){const a="(?:[01]\\d|2[0-3]):[0-5]\\d";return"number"!=typeof t.precision?`${a}(?::[0-5]\\d(?:\\.\\d+)?)?`:-1===t.precision?a:0===t.precision?`${a}:[0-5]\\d`:`${a}:[0-5]\\d\\.\\d{${t.precision}}`}function f(t){return void 0===t?/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/:new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${t}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`)}const l={cuid:/^[cC][0-9a-z]{6,}$/,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{26}$/,xid:/^[0-9a-vA-V]{20}$/,ksuid:/^[A-Za-z0-9]{27}$/,nanoid:/^[a-zA-Z0-9_-]{21}$/,duration:/^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/,extendedDuration:/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/,guid:/^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/,creditCard:/^\d(?:[ -]?\d){11,18}$/,uuid:f,uuid4:f(4),uuid6:f(6),uuid7:f(7),email:/^(?!\.)(?!.*\.\.)([A-Za-z0-9_'+.-]*)[A-Za-z0-9_+-]@([A-Za-z0-9][A-Za-z0-9-]*\.)+[A-Za-z]{2,}$/,html5Email:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,rfc5322Email:/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}))$/,unicodeEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,idnEmail:/^[^\s@"]{1,64}@[^\s@]{1,255}$/u,browserEmail:/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,emoji:()=>/^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/,ipv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/,mac:function(t=":"){const a=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");return new RegExp(`^(?:[0-9A-F]{2}${a}){5}[0-9A-F]{2}$|^(?:[0-9a-f]{2}${a}){5}[0-9a-f]{2}$`)},cidrv4:/^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/,cidrv6:/^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::|([0-9a-fA-F]{1,4})?::([0-9a-fA-F]{1,4}:?){0,6})\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,base64:/^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/,base64url:/^[A-Za-z0-9_-]*$/,hostname:/^(?=.{1,253}\.?$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[-0-9a-zA-Z]{0,61}[0-9a-zA-Z])?)*\.?$/,domain:/^([a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,httpProtocol:/^https?$/,e164:/^\+[1-9]\d{6,14}$/,date:new RegExp(`^${o}$`),time:function(t={}){return new RegExp(`^${m(t)}$`)},datetime:function(t={}){const a=["Z"];return t.local&&a.push(""),t.offset&&a.push("([+-](?:[01]\\d|2[0-3]):[0-5]\\d)"),new RegExp(`^${o}T${m(t)}(?:${a.join("|")})$`)},string:function(t){const a=t?`[\\s\\S]{${t.minimum??0},${t.maximum??""}}`:"[\\s\\S]*";return new RegExp(`^${a}$`)},bigint:/^-?\d+n?$/,integer:/^-?\d+$/,number:/^-?\d+(?:\.\d+)?$/,boolean:/^(?:true|false)$/i,null:/^null$/i,undefined:/^undefined$/i,lowercase:/^[^A-Z]*$/,uppercase:/^[^a-z]*$/,hex:/^[0-9a-fA-F]*$/,md5_hex:/^[0-9a-fA-F]{32}$/,md5_base64:c(22,"=="),md5_base64url:u(22),sha1_hex:/^[0-9a-fA-F]{40}$/,sha1_base64:c(27,"="),sha1_base64url:u(27),sha256_hex:/^[0-9a-fA-F]{64}$/,sha256_base64:c(43,"="),sha256_base64url:u(43),sha384_hex:/^[0-9a-fA-F]{96}$/,sha384_base64:c(64,""),sha384_base64url:u(64),sha512_hex:/^[0-9a-fA-F]{128}$/,sha512_base64:c(86,"=="),sha512_base64url:u(86),jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,httpUrl:/^https?:\/\/[^\s/$.?#].[^\s]*$/,md5:/^[a-f0-9]{32}$/i,sha1:/^[a-f0-9]{40}$/i,sha256:/^[a-f0-9]{64}$/i,sha384:/^[a-f0-9]{96}$/i,sha512:/^[a-f0-9]{128}$/i};class VldStringFormat extends t{constructor(t,r,e,i=t=>t,n){super(a.STRING_FORMAT),this._format=t,this._validator=r,this._errorMessage=e,this._normalize=i,this._patternSource=n}static create(t,a,r,e,i){return new VldStringFormat(t,a,r,e,i)}_createFormatIssue(){const t={code:"invalid_format",path:[],origin:"string",format:this._format,message:this._errorMessage||`Invalid ${this._format}`};return this._patternSource&&(t.pattern=this._patternSource),new i([t])}parse(t){if("string"!=typeof t)throw new i([n("string",d(t),r().invalidString)]);if(!this._validator(t))throw this._createFormatIssue();return this._normalize(t)}safeParse(t){return"string"!=typeof t?{success:!1,error:new i([n("string",d(t),r().invalidString)])}:this._validator(t)?{success:!0,data:this._normalize(t)}:{success:!1,error:this._createFormatIssue()}}}const A=t=>{const a=t?.pattern??l.email;return VldStringFormat.create("email",t=>s(a,t),void 0,void 0,a.source)},$=(t={})=>VldStringFormat.create("url",a=>{const r=(t=>{try{return new URL(t)}catch{return}})(a);return void 0!==r&&(!t.hostname||s(t.hostname,r.hostname))&&(!t.protocol||s(t.protocol,r.protocol.slice(0,-1)))},void 0,t.normalize?t=>new URL(t).href:void 0),F=t=>{const a=f(void 0===t?.version?void 0:Number(t.version.slice(1)));return VldStringFormat.create("uuid",t=>s(a,t))},g=()=>VldStringFormat.create("uuid",t=>s(l.uuid4,t)),h=()=>VldStringFormat.create("uuid",t=>s(l.uuid6,t)),p=()=>VldStringFormat.create("uuid",t=>s(l.uuid7,t)),S=()=>VldStringFormat.create("hostname",t=>s(l.hostname,t)),z=()=>VldStringFormat.create("emoji",t=>s(l.emoji(),t)),Z=()=>VldStringFormat.create("base64",t=>s(l.base64,t)),_=()=>VldStringFormat.create("base64url",t=>s(l.base64url,t)),V=()=>VldStringFormat.create("hex",t=>s(l.hex,t)),v=()=>VldStringFormat.create("jwt",t=>s(l.jwt,t)),x=()=>VldStringFormat.create("nanoid",t=>s(l.nanoid,t)),b=()=>VldStringFormat.create("cuid",t=>s(l.cuid,t)),w=()=>VldStringFormat.create("cuid2",t=>s(l.cuid2,t)),E=()=>VldStringFormat.create("ulid",t=>s(l.ulid,t)),j=()=>VldStringFormat.create("ipv4",t=>s(l.ipv4,t)),I=()=>VldStringFormat.create("ipv6",t=>s(l.ipv6,t)),R=t=>{const a=l.mac(t?.delimiter);return VldStringFormat.create("mac",t=>s(a,t))},M=()=>VldStringFormat.create("cidrv4",t=>s(l.cidrv4,t)),T=()=>VldStringFormat.create("cidrv6",t=>{const a=t.lastIndexOf("/");if(a<=0)return!1;const r=Number(t.slice(a+1));return Number.isInteger(r)&&r>=0&&r<=128&&e(t.slice(0,a))}),D=()=>VldStringFormat.create("e164",t=>s(l.e164,t)),k=()=>VldStringFormat.create("xid",t=>s(l.xid,t)),C=()=>VldStringFormat.create("guid",t=>s(l.guid,t)),O=()=>VldStringFormat.create("httpUrl",t=>s(l.httpUrl,t)),P=()=>VldStringFormat.create("ksuid",t=>s(l.ksuid,t)),N=t=>VldStringFormat.create("hash",a=>l[t]?.test(a)??!1,`Invalid ${t} hash`),U=/[ -]/g;function y(t){return!!l.creditCard.test(t)&&function(t){let a=0,r=!1;for(let e=t.length-1;e>=0;e--){let i=t.charCodeAt(e)-48;r&&(i*=2,i>9&&(i-=9)),a+=i,r=!r}return a%10==0}(t.replace(U,""))}const H=t=>VldStringFormat.create("credit_card",y,t?.message,void 0,l.creditCard.source),W={ZodISODate:VldStringFormat,ZodISODateTime:VldStringFormat,ZodISODuration:VldStringFormat,ZodISOTime:VldStringFormat,date:()=>VldStringFormat.create("date",t=>s(l.date,t)),time:(t={})=>{const a=l.time(t);return VldStringFormat.create("time",t=>s(a,t))},datetime:(t={})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},dateTime:(t={local:!0})=>{const a=l.datetime(t);return VldStringFormat.create("datetime",t=>s(a,t))},duration:()=>VldStringFormat.create("duration",t=>s(l.duration,t))},Y=(t,a)=>{const r=a instanceof RegExp?t=>s(a,t):a;return VldStringFormat.create(t,r)};export{VldStringFormat,Z as base64,_ as base64url,M as cidrv4,T as cidrv6,H as creditCard,b as cuid,w as cuid2,D as e164,A as email,z as emoji,C as guid,N as hash,V as hex,S as hostname,O as httpUrl,j as ipv4,I as ipv6,y as isValidCreditCard,W as iso,v as jwt,P as ksuid,R as mac,x as nanoid,l as regexes,Y as stringFormat,E as ulid,$ as url,F as uuid,g as uuidv4,h as uuidv6,p as uuidv7,k as xid};
|
|
@@ -14,6 +14,16 @@ interface StringJSONSchemaHints {
|
|
|
14
14
|
format?: string;
|
|
15
15
|
pattern?: string;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Metadata for a single string constraint, enabling Zod 4-compatible issues.
|
|
19
|
+
*/
|
|
20
|
+
interface StringCheckMeta {
|
|
21
|
+
readonly kind: 'min' | 'max' | 'length' | 'format' | 'regex' | 'other';
|
|
22
|
+
readonly value?: number;
|
|
23
|
+
readonly format?: string;
|
|
24
|
+
readonly pattern?: string;
|
|
25
|
+
readonly message: string | undefined;
|
|
26
|
+
}
|
|
17
27
|
/**
|
|
18
28
|
* Configuration for string validator
|
|
19
29
|
*/
|
|
@@ -23,6 +33,7 @@ interface StringValidatorConfig {
|
|
|
23
33
|
readonly errorMessage: string | undefined;
|
|
24
34
|
readonly validatorType?: ValidatorType;
|
|
25
35
|
readonly jsonSchema: StringJSONSchemaHints | undefined;
|
|
36
|
+
readonly checkMetas: ReadonlyArray<StringCheckMeta> | undefined;
|
|
26
37
|
}
|
|
27
38
|
/**
|
|
28
39
|
* Immutable string validator with chainable methods
|
|
@@ -33,29 +44,29 @@ export declare class VldString extends VldBase<string, string> {
|
|
|
33
44
|
private readonly _checks;
|
|
34
45
|
private readonly _transforms;
|
|
35
46
|
private readonly _isSimple;
|
|
36
|
-
private
|
|
47
|
+
private readonly _checkMetas;
|
|
37
48
|
/**
|
|
38
49
|
* Protected constructor to allow extension while maintaining immutability
|
|
39
50
|
*/
|
|
40
51
|
protected constructor(config?: Partial<StringValidatorConfig>);
|
|
41
52
|
/**
|
|
42
|
-
*
|
|
43
|
-
* This eliminates loop overhead and enables better JIT optimization
|
|
44
|
-
*/
|
|
45
|
-
private _compileValidator;
|
|
46
|
-
/**
|
|
47
|
-
* Get the cached compiled validator, creating it if necessary
|
|
53
|
+
* Create a new string validator
|
|
48
54
|
*/
|
|
49
|
-
|
|
55
|
+
static create(): VldString;
|
|
50
56
|
/**
|
|
51
57
|
* Returns true if this is a simple string validator with no transforms or checks
|
|
52
58
|
* Used by VldObject for optimized fast-path dispatch
|
|
53
59
|
*/
|
|
54
60
|
get isSimple(): boolean;
|
|
55
61
|
/**
|
|
56
|
-
*
|
|
62
|
+
* Build the Zod 4-compatible VldIssue for a failed check at the given index.
|
|
57
63
|
*/
|
|
58
|
-
|
|
64
|
+
private _createCheckIssue;
|
|
65
|
+
/**
|
|
66
|
+
* Run all checks against a known string and return the first failing meta, or null.
|
|
67
|
+
*/
|
|
68
|
+
private _runChecks;
|
|
69
|
+
private _fallbackMeta;
|
|
59
70
|
/**
|
|
60
71
|
* Parse and validate a string value without allocating intermediate result objects.
|
|
61
72
|
*/
|
|
@@ -93,6 +104,28 @@ export declare class VldString extends VldBase<string, string> {
|
|
|
93
104
|
* Create a new validator that checks for valid UUID format
|
|
94
105
|
*/
|
|
95
106
|
uuid(message?: ErrorParam): VldString;
|
|
107
|
+
private format;
|
|
108
|
+
uuidv4(message?: ErrorParam): VldString;
|
|
109
|
+
uuidv6(message?: ErrorParam): VldString;
|
|
110
|
+
uuidv7(message?: ErrorParam): VldString;
|
|
111
|
+
emoji(message?: ErrorParam): VldString;
|
|
112
|
+
base64(message?: ErrorParam): VldString;
|
|
113
|
+
base64url(message?: ErrorParam): VldString;
|
|
114
|
+
jwt(message?: ErrorParam): VldString;
|
|
115
|
+
nanoid(message?: ErrorParam): VldString;
|
|
116
|
+
cuid(message?: ErrorParam): VldString;
|
|
117
|
+
cuid2(message?: ErrorParam): VldString;
|
|
118
|
+
ulid(message?: ErrorParam): VldString;
|
|
119
|
+
cidrv4(message?: ErrorParam): VldString;
|
|
120
|
+
cidrv6(message?: ErrorParam): VldString;
|
|
121
|
+
e164(message?: ErrorParam): VldString;
|
|
122
|
+
xid(message?: ErrorParam): VldString;
|
|
123
|
+
guid(message?: ErrorParam): VldString;
|
|
124
|
+
ksuid(message?: ErrorParam): VldString;
|
|
125
|
+
date(message?: ErrorParam): VldString;
|
|
126
|
+
time(message?: ErrorParam): VldString;
|
|
127
|
+
datetime(message?: ErrorParam): VldString;
|
|
128
|
+
duration(message?: ErrorParam): VldString;
|
|
96
129
|
/**
|
|
97
130
|
* Create a new validator with regex pattern matching
|
|
98
131
|
*/
|
|
@@ -105,10 +138,14 @@ export declare class VldString extends VldBase<string, string> {
|
|
|
105
138
|
* Create a new validator that converts to lowercase
|
|
106
139
|
*/
|
|
107
140
|
toLowerCase(): VldString;
|
|
141
|
+
lowercase(): VldString;
|
|
108
142
|
/**
|
|
109
143
|
* Create a new validator that converts to uppercase
|
|
110
144
|
*/
|
|
111
145
|
toUpperCase(): VldString;
|
|
146
|
+
uppercase(): VldString;
|
|
147
|
+
normalize(form?: 'NFC' | 'NFD' | 'NFKC' | 'NFKD'): VldString;
|
|
148
|
+
slugify(): VldString;
|
|
112
149
|
/**
|
|
113
150
|
* Create a new validator that checks if string starts with a substring
|
|
114
151
|
*/
|
|
@@ -1,4 +1 @@
|
|
|
1
|
-
import './base.js';
|
|
2
|
-
import '../locales/runtime.js';
|
|
3
|
-
export { V as VldString } from '../chunks/string-BJRQod3t.js';
|
|
4
|
-
import '../chunks/errors-core-BeiFUtZM.js';
|
|
1
|
+
import{VldBase as s,VLD_VALIDATOR_TYPES as e,resolveErrorMessage as t}from"./base.js";import{getMessages as r}from"../locales/runtime.js";import{i}from"../chunks/ip-validation-g5LWgy9t.js";import{V as n,c as a,g as c}from"../chunks/errors-core-Cqi4-9ew.js";const o={email:/^[^\s@]+@[^\s@]+\.[^\s@]+$/,uuid:/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,url:/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/,ipv4:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,uuidv4:/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,uuidv6:/^[0-9a-f]{8}-[0-9a-f]{4}-6[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,uuidv7:/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,emoji:/^(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F)(?:\u200D(?:\p{Emoji_Presentation}|\p{Emoji}\uFE0F))*$/u,base64:/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/,base64url:/^[A-Za-z0-9_-]*$/,jwt:/^[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]*$/,nanoid:/^[A-Za-z0-9_-]{21}$/,cuid:/^c[^\s-]{8,}$/i,cuid2:/^[0-9a-z]+$/,ulid:/^[0-9A-HJKMNP-TV-Z]{26}$/,cidrv4:/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\/(?:3[0-2]|[12]?[0-9])$/,cidrv6:/^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$|^(?:[0-9a-fA-F]{1,4}:){1,7}:\/(?:12[0-8]|1[01][0-9]|[1-9]?[0-9])$/,e164:/^\+[1-9]\d{1,14}$/,xid:/^[A-HJKMNP-TV-Z0-9]{20}$/,guid:/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/i,ksuid:/^[0-9A-Za-z]{27}$/,isoDate:/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/,isoTime:/^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d(?:\.\d+)?)?$/,isoDateTime:/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d+)?Z?$/,isoDuration:/^-?P(?!$)(?:\d+(?:\.\d+)?Y)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?W)?(?:\d+(?:\.\d+)?D)?(?:T(?=\d)(?:\d+(?:\.\d+)?H)?(?:\d+(?:\.\d+)?M)?(?:\d+(?:\.\d+)?S)?)?$/};class VldString extends s{constructor(s){super(s?.validatorType||e.STRING),this.config={checks:s?.checks||[],transforms:s?.transforms||[],errorMessage:s?.errorMessage,jsonSchema:s?.jsonSchema,checkMetas:s?.checkMetas},this._checks=this.config.checks,this._transforms=this.config.transforms,this._isSimple=0===this._checks.length&&0===this._transforms.length,this._checkMetas=this.config.checkMetas}static create(){return new VldString}get isSimple(){return this._isSimple}_createCheckIssue(s){switch(s.kind){case"min":return{code:"too_small",path:[],origin:"string",minimum:s.value,inclusive:!0,message:s.message||`Too small: expected string to have >=${s.value} characters`};case"max":return{code:"too_big",path:[],origin:"string",maximum:s.value,inclusive:!0,message:s.message||`Too big: expected string to have <=${s.value} characters`};case"length":return{code:"too_big",path:[],origin:"string",exact:s.value,message:s.message||`Too big: expected string to have exactly ${s.value} characters`};case"format":{const e={code:"invalid_format",path:[],origin:"string",format:s.format,message:s.message||`Invalid ${s.format}`};return void 0!==s.pattern&&(e.pattern=s.pattern),e}case"regex":{const e={code:"invalid_format",path:[],origin:"string",format:"regex",message:s.message||"Invalid string: does not match pattern"};return void 0!==s.pattern&&(e.pattern=s.pattern),e}default:return{code:"custom",path:[],message:s.message||"Invalid string"}}}_runChecks(s){const e=this._checks,t=this._checkMetas;for(let r=0;r<e.length;r++)if(!e[r](s))return t?.[r]??this._fallbackMeta();return null}_fallbackMeta(){return{kind:"other",message:this.config.errorMessage}}parse(s){if("string"!=typeof s)throw new n([a("string",c(s),this.config.errorMessage)]);return this._isSimple?s:this.parseKnownString(s)}parseKnownString(s){if(this._isSimple)return s;const e=this._transforms;let t=s;switch(e.length){case 0:break;case 1:t=e[0](t);break;case 2:t=e[1](e[0](t));break;case 3:t=e[2](e[1](e[0](t)));break;default:for(let s=0;s<e.length;s++)t=e[s](t)}const r=this._runChecks(t);if(r)throw new n([this._createCheckIssue(r)]);return t}safeParse(s){if("string"!=typeof s)return{success:!1,error:new n([a("string",c(s),this.config.errorMessage)])};if(this._isSimple)return{success:!0,data:s};try{return{success:!0,data:this.parseKnownString(s)}}catch(s){return s instanceof n?{success:!1,error:s}:{success:!1,error:(e=s.message,new n([{code:"invalid_string",path:[],message:e}]))}}var e}min(s,e){return new VldString({checks:[...this.config.checks,e=>e.length>=s],transforms:this.config.transforms,errorMessage:t(e,r().stringMin(s)),jsonSchema:{...this.config.jsonSchema,minLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"min",value:s,message:t(e,r().stringMin(s))}]})}max(s,e){return new VldString({checks:[...this.config.checks,e=>e.length<=s],transforms:this.config.transforms,errorMessage:t(e,r().stringMax(s)),jsonSchema:{...this.config.jsonSchema,maxLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"max",value:s,message:t(e,r().stringMax(s))}]})}length(s,e){return new VldString({checks:[...this.config.checks,e=>e.length===s],transforms:this.config.transforms,errorMessage:t(e,r().stringLength(s)),jsonSchema:{...this.config.jsonSchema,exactLength:s},checkMetas:[...this.config.checkMetas??[],{kind:"length",value:s,message:t(e,r().stringLength(s))}]})}email(s){return new VldString({checks:[...this.config.checks,s=>o.email.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringEmail),jsonSchema:{...this.config.jsonSchema,format:"email"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"email",message:t(s,r().stringEmail)}]})}url(s){return new VldString({checks:[...this.config.checks,s=>o.url.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringUrl),jsonSchema:{...this.config.jsonSchema,format:"uri"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"url",message:t(s,r().stringUrl)}]})}uuid(s){return new VldString({checks:[...this.config.checks,s=>o.uuid.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringUuid),jsonSchema:{...this.config.jsonSchema,format:"uuid"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"uuid",message:t(s,r().stringUuid)}]})}format(s,e,r){return new VldString({checks:[...this.config.checks,e=>s.test(e)],transforms:this.config.transforms,errorMessage:t(r,`Invalid ${e}`),jsonSchema:{...this.config.jsonSchema,format:e,pattern:s.source},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:e,pattern:s.source,message:t(r,`Invalid ${e}`)}]})}uuidv4(s){return this.format(o.uuidv4,"uuid",s)}uuidv6(s){return this.format(o.uuidv6,"uuid",s)}uuidv7(s){return this.format(o.uuidv7,"uuid",s)}emoji(s){return this.format(o.emoji,"emoji",s)}base64(s){return this.format(o.base64,"base64",s)}base64url(s){return this.format(o.base64url,"base64url",s)}jwt(s){return this.format(o.jwt,"jwt",s)}nanoid(s){return this.format(o.nanoid,"nanoid",s)}cuid(s){return this.format(o.cuid,"cuid",s)}cuid2(s){return this.format(o.cuid2,"cuid2",s)}ulid(s){return this.format(o.ulid,"ulid",s)}cidrv4(s){return this.format(o.cidrv4,"cidrv4",s)}cidrv6(s){return this.format(o.cidrv6,"cidrv6",s)}e164(s){return this.format(o.e164,"e164",s)}xid(s){return this.format(o.xid,"xid",s)}guid(s){return this.format(o.guid,"guid",s)}ksuid(s){return this.format(o.ksuid,"ksuid",s)}date(s){return this.format(o.isoDate,"date",s)}time(s){return this.format(o.isoTime,"time",s)}datetime(s){return this.format(o.isoDateTime,"datetime",s)}duration(s){return this.format(o.isoDuration,"duration",s)}regex(s,e){return new VldString({checks:[...this.config.checks,e=>s.test(e)],transforms:this.config.transforms,errorMessage:t(e,r().stringRegex),jsonSchema:{...this.config.jsonSchema,pattern:s.source},checkMetas:[...this.config.checkMetas??[],{kind:"regex",pattern:s.source,message:t(e,r().stringRegex)}]})}trim(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.trim()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}toLowerCase(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toLowerCase()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}lowercase(){return this.toLowerCase()}toUpperCase(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toUpperCase()],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}uppercase(){return this.toUpperCase()}normalize(s){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,e=>e.normalize(s)],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}slugify(){return new VldString({checks:this.config.checks,transforms:[...this.config.transforms,s=>s.toLowerCase().trim().replace(/[^\w\s-]/g,"").replace(/[\s_-]+/g,"-").replace(/^-+|-+$/g,"")],errorMessage:this.config.errorMessage,jsonSchema:this.config.jsonSchema})}startsWith(s,e){return new VldString({checks:[...this.config.checks,e=>e.startsWith(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringStartsWith(s)),jsonSchema:this.config.jsonSchema})}endsWith(s,e){return new VldString({checks:[...this.config.checks,e=>e.endsWith(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringEndsWith(s)),jsonSchema:this.config.jsonSchema})}includes(s,e){return new VldString({checks:[...this.config.checks,e=>e.includes(s)],transforms:this.config.transforms,errorMessage:t(e,r().stringIncludes(s)),jsonSchema:this.config.jsonSchema})}ip(s){return new VldString({checks:[...this.config.checks,s=>!(s.length>100)&&(o.ipv4.test(s)||i(s))],transforms:this.config.transforms,errorMessage:t(s,r().stringIp),jsonSchema:{...this.config.jsonSchema,format:"ip"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ip",message:t(s,r().stringIp)}]})}ipv4(s){return new VldString({checks:[...this.config.checks,s=>o.ipv4.test(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringIpv4),jsonSchema:{...this.config.jsonSchema,format:"ipv4"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ipv4",message:t(s,r().stringIpv4)}]})}ipv6(s){return new VldString({checks:[...this.config.checks,s=>i(s)],transforms:this.config.transforms,errorMessage:t(s,r().stringIpv6),jsonSchema:{...this.config.jsonSchema,format:"ipv6"},checkMetas:[...this.config.checkMetas??[],{kind:"format",format:"ipv6",message:t(s,r().stringIpv6)}]})}nonempty(s){return new VldString({checks:[...this.config.checks,s=>s.length>0],transforms:this.config.transforms,errorMessage:t(s,r().stringEmpty),jsonSchema:{...this.config.jsonSchema,minLength:Math.max(this.config.jsonSchema?.minLength||0,1)},checkMetas:[...this.config.checkMetas??[],{kind:"min",value:1,message:t(s,r().stringEmpty)}]})}}export{VldString};
|
|
@@ -1,58 +1 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { getMessages } from '../locales/runtime.js';
|
|
3
|
-
import { V as VldError } from '../chunks/errors-core-BeiFUtZM.js';
|
|
4
|
-
|
|
5
|
-
function createSymbolError(message) {
|
|
6
|
-
return new VldError([{ code: 'invalid_type', path: [], message }]);
|
|
7
|
-
}
|
|
8
|
-
/**
|
|
9
|
-
* Immutable symbol validator
|
|
10
|
-
*/
|
|
11
|
-
class VldSymbol extends VldBase {
|
|
12
|
-
/**
|
|
13
|
-
* Private constructor to enforce immutability
|
|
14
|
-
*/
|
|
15
|
-
constructor(errorMessage) {
|
|
16
|
-
super(VLD_VALIDATOR_TYPES.SYMBOL);
|
|
17
|
-
this.errorMessage = errorMessage;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Create a new symbol validator
|
|
21
|
-
*/
|
|
22
|
-
static create() {
|
|
23
|
-
return new VldSymbol();
|
|
24
|
-
}
|
|
25
|
-
get isSimple() {
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Parse and validate a symbol value
|
|
30
|
-
*/
|
|
31
|
-
parse(value) {
|
|
32
|
-
if (typeof value !== 'symbol') {
|
|
33
|
-
throw new Error(this.errorMessage || getMessages().invalidSymbol);
|
|
34
|
-
}
|
|
35
|
-
return value;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Parse a value that has already passed the symbol type guard.
|
|
39
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
40
|
-
*/
|
|
41
|
-
parseKnownSymbol(value) {
|
|
42
|
-
return value;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Safely parse and validate a symbol value
|
|
46
|
-
*/
|
|
47
|
-
safeParse(value) {
|
|
48
|
-
if (typeof value === 'symbol') {
|
|
49
|
-
return { success: true, data: value };
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
success: false,
|
|
53
|
-
error: createSymbolError(this.errorMessage || getMessages().invalidSymbol)
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export { VldSymbol };
|
|
1
|
+
import{VldBase as r,VLD_VALIDATOR_TYPES as e}from"./base.js";import{getMessages as s}from"../locales/runtime.js";import{V as o}from"../chunks/errors-core-Cqi4-9ew.js";class VldSymbol extends r{constructor(r){super(e.SYMBOL),this.errorMessage=r}static create(){return new VldSymbol}get isSimple(){return!0}parse(r){if("symbol"!=typeof r)throw new Error(this.errorMessage||s().invalidSymbol);return r}parseKnownSymbol(r){return r}safeParse(r){return"symbol"==typeof r?{success:!0,data:r}:{success:!1,error:(e=this.errorMessage||s().invalidSymbol,new o([{code:"invalid_type",path:[],message:e}]))};var e}}export{VldSymbol};
|