@oxog/vld 2.1.0 → 2.2.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +113 -0
- package/README.md +413 -1235
- package/dist/chunks/errors-core-CerMg2rq.js +1 -0
- package/dist/chunks/index-CkbfQQ4n.js +1 -0
- package/dist/chunks/index-yjpbvFgw.js +1 -0
- package/dist/chunks/ip-validation-g5LWgy9t.js +1 -0
- package/dist/chunks/security-Dy24T8N7.js +1 -0
- package/dist/cjs/chunks/errors-core-DrydF46s.cjs +1 -0
- package/dist/cjs/chunks/index-BEV9nG0N.cjs +1 -0
- package/dist/cjs/chunks/index-ZjHAxYdA.cjs +1 -0
- package/dist/cjs/chunks/ip-validation-BZqPamPC.cjs +1 -0
- package/dist/cjs/chunks/security-aXZVI7qW.cjs +1 -0
- package/dist/cjs/cli/bin.cjs +1 -505
- package/dist/cjs/cli/index.cjs +1 -209
- package/dist/cjs/codecs/index.cjs +1 -45
- package/dist/cjs/coercion/index.cjs +1 -588
- package/dist/cjs/compat/emitter.cjs +1 -184
- package/dist/cjs/compat/result.cjs +1 -189
- package/dist/cjs/errors.cjs +1 -180
- package/dist/cjs/index.cjs +1 -1894
- package/dist/cjs/kernel.cjs +1 -415
- package/dist/cjs/locales/af.cjs +1 -108
- package/dist/cjs/locales/ar.cjs +1 -108
- package/dist/cjs/locales/bn.cjs +1 -108
- package/dist/cjs/locales/da.cjs +1 -108
- package/dist/cjs/locales/de.cjs +1 -108
- package/dist/cjs/locales/en.cjs +1 -108
- package/dist/cjs/locales/es-MX.cjs +1 -107
- package/dist/cjs/locales/es.cjs +1 -96
- package/dist/cjs/locales/fi.cjs +1 -108
- package/dist/cjs/locales/fr.cjs +1 -96
- package/dist/cjs/locales/hi.cjs +1 -108
- package/dist/cjs/locales/id.cjs +1 -108
- package/dist/cjs/locales/index.cjs +1 -43
- package/dist/cjs/locales/it.cjs +1 -108
- package/dist/cjs/locales/ja.cjs +1 -108
- package/dist/cjs/locales/ko.cjs +1 -108
- package/dist/cjs/locales/lazy.cjs +1 -188
- package/dist/cjs/locales/nl.cjs +1 -108
- package/dist/cjs/locales/no.cjs +1 -108
- package/dist/cjs/locales/pl.cjs +1 -108
- package/dist/cjs/locales/pt-BR.cjs +1 -108
- package/dist/cjs/locales/pt.cjs +1 -108
- package/dist/cjs/locales/ru.cjs +1 -108
- package/dist/cjs/locales/runtime.cjs +1 -51
- package/dist/cjs/locales/sv.cjs +1 -108
- package/dist/cjs/locales/sw.cjs +1 -108
- package/dist/cjs/locales/th.cjs +1 -108
- package/dist/cjs/locales/tr.cjs +1 -108
- package/dist/cjs/locales/vi.cjs +1 -108
- package/dist/cjs/locales/zh.cjs +1 -108
- package/dist/cjs/mini.cjs +1 -242
- package/dist/cjs/pigment.cjs +1 -207
- package/dist/cjs/plugins/index.cjs +1 -12
- package/dist/cjs/registry.cjs +1 -37
- package/dist/cjs/v3/index.cjs +1 -425
- package/dist/cjs/v4/core/index.cjs +1 -894
- package/dist/cjs/v4/index.cjs +1 -425
- package/dist/cjs/v4/locales/index.cjs +1 -130
- package/dist/cjs/v4/mini/index.cjs +1 -509
- package/dist/cjs/v4-mini/index.cjs +1 -532
- package/dist/cjs/validators/any.cjs +1 -40
- package/dist/cjs/validators/array.cjs +1 -283
- package/dist/cjs/validators/base.cjs +1 -887
- package/dist/cjs/validators/base64.cjs +1 -73
- package/dist/cjs/validators/bigint.cjs +1 -232
- package/dist/cjs/validators/boolean.cjs +1 -146
- package/dist/cjs/validators/codec.cjs +1 -205
- package/dist/cjs/validators/custom.cjs +1 -71
- package/dist/cjs/validators/date.cjs +1 -278
- package/dist/cjs/validators/discriminated-union.cjs +1 -130
- package/dist/cjs/validators/enum.cjs +1 -154
- package/dist/cjs/validators/file.cjs +1 -130
- package/dist/cjs/validators/function.cjs +1 -63
- package/dist/cjs/validators/hex.cjs +1 -70
- package/dist/cjs/validators/index.cjs +1 -123
- package/dist/cjs/validators/intersection.cjs +1 -120
- package/dist/cjs/validators/json.cjs +1 -70
- package/dist/cjs/validators/lazy.cjs +1 -53
- package/dist/cjs/validators/literal.cjs +1 -64
- package/dist/cjs/validators/map.cjs +1 -218
- package/dist/cjs/validators/nan.cjs +1 -41
- package/dist/cjs/validators/never.cjs +1 -36
- package/dist/cjs/validators/null.cjs +1 -43
- package/dist/cjs/validators/number.cjs +1 -427
- package/dist/cjs/validators/object.cjs +1 -868
- package/dist/cjs/validators/promise.cjs +1 -115
- package/dist/cjs/validators/record.cjs +1 -262
- package/dist/cjs/validators/set.cjs +1 -203
- package/dist/cjs/validators/string-bool.cjs +1 -124
- package/dist/cjs/validators/string-formats.cjs +1 -168
- package/dist/cjs/validators/string.cjs +1 -12
- package/dist/cjs/validators/symbol.cjs +1 -62
- package/dist/cjs/validators/template-literal.cjs +1 -108
- package/dist/cjs/validators/tuple.cjs +1 -187
- package/dist/cjs/validators/uint8array.cjs +1 -106
- package/dist/cjs/validators/undefined.cjs +1 -43
- package/dist/cjs/validators/union.cjs +1 -191
- package/dist/cjs/validators/unknown.cjs +1 -40
- package/dist/cjs/validators/void.cjs +1 -48
- package/dist/cjs/validators/xor.cjs +1 -69
- package/dist/cli/bin.js +1 -406
- package/dist/cli/index.js +1 -205
- package/dist/codecs/index.js +1 -11
- package/dist/coercion/index.js +1 -579
- package/dist/compat/emitter.js +1 -180
- package/dist/compat/result.js +1 -170
- package/dist/errors-core.d.ts +32 -2
- package/dist/errors.js +1 -171
- package/dist/index.d.ts +315 -361
- package/dist/index.js +1 -1534
- package/dist/kernel.js +1 -409
- package/dist/locales/af.js +1 -106
- package/dist/locales/ar.js +1 -106
- package/dist/locales/bn.js +1 -106
- package/dist/locales/da.js +1 -106
- package/dist/locales/de.js +1 -106
- package/dist/locales/en.js +1 -106
- package/dist/locales/es-MX.js +1 -105
- package/dist/locales/es.js +1 -94
- package/dist/locales/fi.js +1 -106
- package/dist/locales/fr.js +1 -94
- package/dist/locales/hi.js +1 -106
- package/dist/locales/id.js +1 -106
- package/dist/locales/index.js +1 -29
- package/dist/locales/it.js +1 -106
- package/dist/locales/ja.js +1 -106
- package/dist/locales/ko.js +1 -106
- package/dist/locales/lazy.js +1 -177
- package/dist/locales/nl.js +1 -106
- package/dist/locales/no.js +1 -106
- package/dist/locales/pl.js +1 -106
- package/dist/locales/pt-BR.js +1 -106
- package/dist/locales/pt.js +1 -106
- package/dist/locales/ru.js +1 -106
- package/dist/locales/runtime.js +1 -41
- package/dist/locales/sv.js +1 -106
- package/dist/locales/sw.js +1 -106
- package/dist/locales/th.js +1 -106
- package/dist/locales/tr.js +1 -106
- package/dist/locales/vi.js +1 -106
- package/dist/locales/zh.js +1 -106
- package/dist/mini.d.ts +3 -3
- package/dist/mini.js +1 -156
- package/dist/pigment.js +1 -180
- package/dist/plugins/index.js +1 -1
- package/dist/registry.js +1 -34
- package/dist/utils/json-schema.d.ts +10 -1
- package/dist/v3/index.js +1 -46
- package/dist/v4/core/index.d.ts +28 -16
- package/dist/v4/core/index.js +1 -277
- package/dist/v4/index.js +1 -50
- package/dist/v4/locales/index.d.ts +4 -0
- package/dist/v4/locales/index.js +1 -77
- package/dist/v4/mini/index.js +1 -47
- package/dist/v4-mini/index.d.ts +1 -1
- package/dist/v4-mini/index.js +1 -75
- package/dist/validators/any.js +1 -37
- package/dist/validators/array.d.ts +7 -0
- package/dist/validators/array.js +1 -279
- package/dist/validators/base.d.ts +210 -17
- package/dist/validators/base.js +1 -868
- package/dist/validators/base64.js +1 -68
- package/dist/validators/bigint.d.ts +7 -0
- package/dist/validators/bigint.js +1 -228
- package/dist/validators/boolean.js +1 -142
- package/dist/validators/codec.js +1 -200
- package/dist/validators/custom.d.ts +20 -10
- package/dist/validators/custom.js +1 -67
- package/dist/validators/date.d.ts +2 -0
- package/dist/validators/date.js +1 -274
- package/dist/validators/discriminated-union.d.ts +4 -2
- package/dist/validators/discriminated-union.js +1 -123
- package/dist/validators/enum.d.ts +8 -2
- package/dist/validators/enum.js +1 -150
- package/dist/validators/file.js +1 -125
- package/dist/validators/function.js +1 -58
- package/dist/validators/hex.js +1 -65
- package/dist/validators/index.d.ts +1 -1
- package/dist/validators/index.js +1 -35
- package/dist/validators/intersection.js +1 -116
- package/dist/validators/json.js +1 -66
- package/dist/validators/lazy.js +1 -50
- package/dist/validators/literal.d.ts +8 -3
- package/dist/validators/literal.js +1 -60
- package/dist/validators/map.d.ts +7 -0
- package/dist/validators/map.js +1 -214
- package/dist/validators/nan.js +1 -38
- package/dist/validators/never.js +1 -32
- package/dist/validators/null.js +1 -40
- package/dist/validators/number.d.ts +25 -1
- package/dist/validators/number.js +1 -423
- package/dist/validators/object.d.ts +18 -2
- package/dist/validators/object.js +1 -864
- package/dist/validators/promise.d.ts +6 -7
- package/dist/validators/promise.js +1 -111
- package/dist/validators/record.d.ts +13 -8
- package/dist/validators/record.js +1 -258
- package/dist/validators/set.d.ts +5 -0
- package/dist/validators/set.js +1 -199
- package/dist/validators/string-bool.js +1 -120
- package/dist/validators/string-formats.d.ts +58 -62
- package/dist/validators/string-formats.js +1 -135
- package/dist/validators/string.d.ts +49 -10
- package/dist/validators/string.js +1 -4
- package/dist/validators/symbol.js +1 -58
- package/dist/validators/template-literal.js +1 -103
- package/dist/validators/tuple.d.ts +17 -13
- package/dist/validators/tuple.js +1 -183
- package/dist/validators/uint8array.js +1 -101
- package/dist/validators/undefined.js +1 -40
- package/dist/validators/union.d.ts +1 -0
- package/dist/validators/union.js +1 -187
- package/dist/validators/unknown.js +1 -37
- package/dist/validators/void.js +1 -44
- package/dist/validators/xor.d.ts +2 -2
- package/dist/validators/xor.js +1 -66
- package/package.json +240 -238
- package/dist/chunks/errors-core-BeiFUtZM.js +0 -81
- package/dist/chunks/index-0R2ntSud.js +0 -508
- package/dist/chunks/index-BvH403Yy.js +0 -88
- package/dist/chunks/security-B0Pd_vSY.js +0 -35
- package/dist/chunks/string-BJRQod3t.js +0 -530
- package/dist/cjs/chunks/errors-core-DWxLFs2f.cjs +0 -83
- package/dist/cjs/chunks/index-BkI37108.cjs +0 -538
- package/dist/cjs/chunks/index-Cc3bjUMX.cjs +0 -91
- package/dist/cjs/chunks/security-Dc3Jh13T.cjs +0 -37
- package/dist/cjs/chunks/string-BKZrc4hs.cjs +0 -533
|
@@ -1,283 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var validators_base = require('./base.cjs');
|
|
4
|
-
var locales_runtime = require('../locales/runtime.cjs');
|
|
5
|
-
var errorsCore = require('../chunks/errors-core-DWxLFs2f.cjs');
|
|
6
|
-
require('../registry.cjs');
|
|
7
|
-
require('../locales/en.cjs');
|
|
8
|
-
|
|
9
|
-
function createArrayError(message) {
|
|
10
|
-
return new errorsCore.VldError([{ code: 'invalid_array', path: [], message }]);
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Immutable array validator with chainable methods
|
|
14
|
-
*/
|
|
15
|
-
class VldArray extends validators_base.VldBase {
|
|
16
|
-
/**
|
|
17
|
-
* Private constructor to enforce immutability
|
|
18
|
-
*/
|
|
19
|
-
constructor(config) {
|
|
20
|
-
super(validators_base.VLD_VALIDATOR_TYPES.ARRAY);
|
|
21
|
-
this.config = config;
|
|
22
|
-
this._simpleItemMode = this.getSimpleItemMode(config.itemValidator);
|
|
23
|
-
}
|
|
24
|
-
getSimpleItemMode(itemValidator) {
|
|
25
|
-
if (itemValidator.isSimple !== true) {
|
|
26
|
-
return undefined;
|
|
27
|
-
}
|
|
28
|
-
switch (itemValidator.validatorType) {
|
|
29
|
-
case validators_base.VLD_VALIDATOR_TYPES.STRING:
|
|
30
|
-
return 'string';
|
|
31
|
-
case validators_base.VLD_VALIDATOR_TYPES.NUMBER:
|
|
32
|
-
return 'number';
|
|
33
|
-
case validators_base.VLD_VALIDATOR_TYPES.BOOLEAN:
|
|
34
|
-
return 'boolean';
|
|
35
|
-
default:
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Create a new array validator
|
|
41
|
-
*/
|
|
42
|
-
static create(itemValidator) {
|
|
43
|
-
return new VldArray({ itemValidator });
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Parse and validate an array value
|
|
47
|
-
*/
|
|
48
|
-
parse(value) {
|
|
49
|
-
if (!Array.isArray(value)) {
|
|
50
|
-
throw new Error(this.config.errorMessage || locales_runtime.getMessages().invalidArray);
|
|
51
|
-
}
|
|
52
|
-
return this.parseArrayValue(value);
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Parse a value that has already passed the array type guard.
|
|
56
|
-
* @internal Used by object validators to avoid duplicate hot-path checks.
|
|
57
|
-
*/
|
|
58
|
-
parseKnownArray(value) {
|
|
59
|
-
return this.parseArrayValue(value);
|
|
60
|
-
}
|
|
61
|
-
parseArrayValue(value) {
|
|
62
|
-
// Validate length constraints
|
|
63
|
-
if (this.config.exactLength !== undefined && value.length !== this.config.exactLength) {
|
|
64
|
-
throw new Error(this.config.errorMessage || locales_runtime.getMessages().arrayLength(this.config.exactLength));
|
|
65
|
-
}
|
|
66
|
-
if (this.config.minLength !== undefined && value.length < this.config.minLength) {
|
|
67
|
-
throw new Error(this.config.errorMessage || locales_runtime.getMessages().arrayMin(this.config.minLength));
|
|
68
|
-
}
|
|
69
|
-
if (this.config.maxLength !== undefined && value.length > this.config.maxLength) {
|
|
70
|
-
throw new Error(this.config.errorMessage || locales_runtime.getMessages().arrayMax(this.config.maxLength));
|
|
71
|
-
}
|
|
72
|
-
const length = value.length;
|
|
73
|
-
const result = new Array(length);
|
|
74
|
-
const simpleItemMode = this._simpleItemMode;
|
|
75
|
-
if (simpleItemMode !== undefined) {
|
|
76
|
-
for (let i = 0; i < length; i++) {
|
|
77
|
-
const item = value[i];
|
|
78
|
-
switch (simpleItemMode) {
|
|
79
|
-
case 'string':
|
|
80
|
-
if (typeof item !== 'string') {
|
|
81
|
-
throw new Error(locales_runtime.getMessages().arrayItem(i, locales_runtime.getMessages().invalidString));
|
|
82
|
-
}
|
|
83
|
-
result[i] = item;
|
|
84
|
-
break;
|
|
85
|
-
case 'number':
|
|
86
|
-
if (typeof item !== 'number' || isNaN(item)) {
|
|
87
|
-
throw new Error(locales_runtime.getMessages().arrayItem(i, locales_runtime.getMessages().invalidNumber));
|
|
88
|
-
}
|
|
89
|
-
result[i] = item;
|
|
90
|
-
break;
|
|
91
|
-
case 'boolean':
|
|
92
|
-
if (typeof item !== 'boolean') {
|
|
93
|
-
throw new Error(locales_runtime.getMessages().arrayItem(i, locales_runtime.getMessages().invalidBoolean));
|
|
94
|
-
}
|
|
95
|
-
result[i] = item;
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
if (this.config.unique) {
|
|
100
|
-
this.checkUnique(result);
|
|
101
|
-
}
|
|
102
|
-
return result;
|
|
103
|
-
}
|
|
104
|
-
// Validate each item without allocating a ParseResult object per element.
|
|
105
|
-
for (let i = 0; i < length; i++) {
|
|
106
|
-
try {
|
|
107
|
-
result[i] = this.config.itemValidator.parse(value[i]);
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
111
|
-
throw new Error(locales_runtime.getMessages().arrayItem(i, message));
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
// Check uniqueness if required - optimized with Map-based approach
|
|
115
|
-
if (this.config.unique) {
|
|
116
|
-
this.checkUnique(result);
|
|
117
|
-
}
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Safely parse and validate an array value
|
|
122
|
-
*/
|
|
123
|
-
safeParse(value) {
|
|
124
|
-
try {
|
|
125
|
-
return { success: true, data: this.parse(value) };
|
|
126
|
-
}
|
|
127
|
-
catch (error) {
|
|
128
|
-
return { success: false, error: createArrayError(error.message) };
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Optimized uniqueness check using Map-based approach
|
|
133
|
-
* Avoids repeated stableStringify calls by caching serialized values
|
|
134
|
-
*/
|
|
135
|
-
checkUnique(items) {
|
|
136
|
-
const seen = new Set(); // Set of seen keys
|
|
137
|
-
const objectKeys = new WeakMap(); // Cache for object->string mappings
|
|
138
|
-
for (const item of items) {
|
|
139
|
-
let key;
|
|
140
|
-
if (typeof item === 'object' && item !== null) {
|
|
141
|
-
// Check if we've already serialized this object reference
|
|
142
|
-
const cached = objectKeys.get(item);
|
|
143
|
-
if (cached !== undefined) {
|
|
144
|
-
key = cached;
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
// Serialize and cache
|
|
148
|
-
const serialized = this.stableStringify(item);
|
|
149
|
-
key = serialized;
|
|
150
|
-
objectKeys.set(item, serialized);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
else {
|
|
154
|
-
// Primitives can be used directly as keys
|
|
155
|
-
key = item;
|
|
156
|
-
}
|
|
157
|
-
if (seen.has(key)) {
|
|
158
|
-
throw new Error('Array must contain unique items');
|
|
159
|
-
}
|
|
160
|
-
seen.add(key);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Create a stable string representation of an object for hashing
|
|
165
|
-
* Handles circular references and deep nesting gracefully
|
|
166
|
-
* BUG-006 FIX: Added depth limit to prevent stack overflow
|
|
167
|
-
* BUG-NEW-003 FIX: Fixed depth tracking to properly track recursion depth
|
|
168
|
-
*/
|
|
169
|
-
stableStringify(obj) {
|
|
170
|
-
const seen = new WeakSet();
|
|
171
|
-
const MAX_DEPTH = 100; // Reasonable depth limit to prevent stack overflow
|
|
172
|
-
// Helper function to sort keys and create stable representation
|
|
173
|
-
const sortedStringify = (value, depth = 0) => {
|
|
174
|
-
// Check depth limit
|
|
175
|
-
if (depth > MAX_DEPTH) {
|
|
176
|
-
return '"[Max Depth Exceeded]"';
|
|
177
|
-
}
|
|
178
|
-
// Handle primitives
|
|
179
|
-
if (value === null)
|
|
180
|
-
return 'null';
|
|
181
|
-
if (value === undefined)
|
|
182
|
-
return 'undefined';
|
|
183
|
-
if (typeof value !== 'object')
|
|
184
|
-
return JSON.stringify(value);
|
|
185
|
-
// Handle circular references
|
|
186
|
-
if (seen.has(value)) {
|
|
187
|
-
return '"[Circular]"';
|
|
188
|
-
}
|
|
189
|
-
seen.add(value);
|
|
190
|
-
try {
|
|
191
|
-
// Handle arrays
|
|
192
|
-
if (Array.isArray(value)) {
|
|
193
|
-
const items = value.map(item => sortedStringify(item, depth + 1));
|
|
194
|
-
return `[${items.join(',')}]`;
|
|
195
|
-
}
|
|
196
|
-
// Handle objects - sort keys for stability
|
|
197
|
-
const keys = Object.keys(value).sort();
|
|
198
|
-
const pairs = keys.map(key => {
|
|
199
|
-
const serializedKey = JSON.stringify(key);
|
|
200
|
-
const serializedValue = sortedStringify(value[key], depth + 1);
|
|
201
|
-
return `${serializedKey}:${serializedValue}`;
|
|
202
|
-
});
|
|
203
|
-
return `{${pairs.join(',')}}`;
|
|
204
|
-
}
|
|
205
|
-
finally {
|
|
206
|
-
// Clean up seen set for this branch
|
|
207
|
-
seen.delete(value);
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
try {
|
|
211
|
-
return sortedStringify(obj);
|
|
212
|
-
}
|
|
213
|
-
catch {
|
|
214
|
-
// Fallback to safe representation if stringify fails
|
|
215
|
-
return String(obj);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Create a new validator with minimum length constraint
|
|
220
|
-
*/
|
|
221
|
-
min(length, message) {
|
|
222
|
-
return new VldArray({
|
|
223
|
-
...this.config,
|
|
224
|
-
minLength: length,
|
|
225
|
-
errorMessage: message || locales_runtime.getMessages().arrayMin(length)
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Create a new validator with maximum length constraint
|
|
230
|
-
*/
|
|
231
|
-
max(length, message) {
|
|
232
|
-
return new VldArray({
|
|
233
|
-
...this.config,
|
|
234
|
-
maxLength: length,
|
|
235
|
-
errorMessage: message || locales_runtime.getMessages().arrayMax(length)
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Create a new validator with exact length constraint
|
|
240
|
-
*/
|
|
241
|
-
length(length, message) {
|
|
242
|
-
return new VldArray({
|
|
243
|
-
...this.config,
|
|
244
|
-
exactLength: length,
|
|
245
|
-
minLength: undefined,
|
|
246
|
-
maxLength: undefined,
|
|
247
|
-
errorMessage: message || locales_runtime.getMessages().arrayLength(length)
|
|
248
|
-
});
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Create a new validator that ensures array is not empty
|
|
252
|
-
*/
|
|
253
|
-
nonempty(message) {
|
|
254
|
-
return new VldArray({
|
|
255
|
-
...this.config,
|
|
256
|
-
minLength: 1,
|
|
257
|
-
errorMessage: message || locales_runtime.getMessages().arrayEmpty
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* Create a new validator that ensures array contains unique items
|
|
262
|
-
*/
|
|
263
|
-
unique(message) {
|
|
264
|
-
return new VldArray({
|
|
265
|
-
...this.config,
|
|
266
|
-
unique: true,
|
|
267
|
-
errorMessage: message || 'Array must contain unique items'
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Create a new validator with a range constraint for length
|
|
272
|
-
*/
|
|
273
|
-
between(min, max, message) {
|
|
274
|
-
return new VldArray({
|
|
275
|
-
...this.config,
|
|
276
|
-
minLength: min,
|
|
277
|
-
maxLength: max,
|
|
278
|
-
errorMessage: message || `Array length must be between ${min} and ${max}`
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
exports.VldArray = VldArray;
|
|
1
|
+
"use strict";var e=require("./base.cjs"),r=require("../locales/runtime.cjs"),t=require("../chunks/errors-core-DrydF46s.cjs");function n(e){return new t.VldError([{code:"invalid_array",path:[],message:e}])}function s(e,r){return new t.VldError([{code:"too_small",path:[],origin:"array",minimum:e,inclusive:!0,message:r}])}require("../registry.cjs"),require("../locales/en.cjs");class VldArray extends e.VldBase{constructor(r){super(e.VLD_VALIDATOR_TYPES.ARRAY),this.config=r,this._simpleItemMode=this.getSimpleItemMode(r.itemValidator)}getSimpleItemMode(r){if(!0===r.isSimple)switch(r.validatorType){case e.VLD_VALIDATOR_TYPES.STRING:return"string";case e.VLD_VALIDATOR_TYPES.NUMBER:return"number";case e.VLD_VALIDATOR_TYPES.BOOLEAN:return"boolean";default:return}}static create(e){return new VldArray({itemValidator:e})}get element(){return this.config.itemValidator}unwrap(){return this.config.itemValidator}parse(e){if(!Array.isArray(e)){const n=this.config.errorMessage||r.getMessages().invalidArray;throw new t.VldError([t.createInvalidTypeIssue("array",t.getTypeName(e),n)])}return this.parseArrayValue(e)}parseKnownArray(e){return this.parseArrayValue(e)}parseArrayValue(e){if(void 0!==this.config.exactLength&&e.length!==this.config.exactLength)throw s(this.config.exactLength,this.config.errorMessage||r.getMessages().arrayLength(this.config.exactLength));if(void 0!==this.config.minLength&&e.length<this.config.minLength)throw s(this.config.minLength,this.config.errorMessage||r.getMessages().arrayMin(this.config.minLength));if(void 0!==this.config.maxLength&&e.length>this.config.maxLength)throw n=this.config.maxLength,i=this.config.errorMessage||r.getMessages().arrayMax(this.config.maxLength),new t.VldError([{code:"too_big",path:[],origin:"array",maximum:n,inclusive:!0,message:i}]);var n,i;const a=e.length,o=new Array(a),g=this._simpleItemMode;if(void 0!==g){for(let t=0;t<a;t++){const n=e[t];switch(g){case"string":if("string"!=typeof n)throw new Error(r.getMessages().arrayItem(t,r.getMessages().invalidString));o[t]=n;break;case"number":if("number"!=typeof n||isNaN(n))throw new Error(r.getMessages().arrayItem(t,r.getMessages().invalidNumber));o[t]=n;break;case"boolean":if("boolean"!=typeof n)throw new Error(r.getMessages().arrayItem(t,r.getMessages().invalidBoolean));o[t]=n}}return this.config.unique&&this.checkUnique(o),o}for(let t=0;t<a;t++)try{o[t]=this.config.itemValidator.parse(e[t])}catch(e){const n=e instanceof Error?e.message:String(e);throw new Error(r.getMessages().arrayItem(t,n))}return this.config.unique&&this.checkUnique(o),o}safeParse(e){try{return{success:!0,data:this.parse(e)}}catch(e){return e instanceof t.VldError?{success:!1,error:e}:{success:!1,error:n(e.message)}}}encode(e){this.validateEncodedArray(e);const r=new Array(e.length);for(let t=0;t<e.length;t++)r[t]=this.config.itemValidator.encode(e[t]);return r}safeEncode(e){try{return{success:!0,data:this.encode(e)}}catch(e){return{success:!1,error:n(e.message)}}}async encodeAsync(e){this.validateEncodedArray(e);const r=new Array(e.length);for(let t=0;t<e.length;t++)r[t]=await this.config.itemValidator.encodeAsync(e[t]);return r}async safeEncodeAsync(e){try{return{success:!0,data:await this.encodeAsync(e)}}catch(e){return{success:!1,error:n(e.message)}}}validateEncodedArray(e){if(!Array.isArray(e))throw new Error(this.config.errorMessage||r.getMessages().invalidArray);if(void 0!==this.config.exactLength&&e.length!==this.config.exactLength)throw new Error(this.config.errorMessage);if(void 0!==this.config.minLength&&e.length<this.config.minLength)throw new Error(this.config.errorMessage);if(void 0!==this.config.maxLength&&e.length>this.config.maxLength)throw new Error(this.config.errorMessage);this.config.unique&&this.checkUnique(e)}checkUnique(e){const r=new Set,t=new WeakMap;for(const n of e){let e;if("object"==typeof n&&null!==n){const r=t.get(n);if(void 0!==r)e=r;else{const r=this.stableStringify(n);e=r,t.set(n,r)}}else e=n;if(r.has(e))throw new Error("Array must contain unique items");r.add(e)}}stableStringify(e){const r=new WeakSet,t=(e,n=0)=>{if(n>100)return'"[Max Depth Exceeded]"';if(null===e)return"null";if(void 0===e)return"undefined";if("object"!=typeof e)return JSON.stringify(e);if(r.has(e))return'"[Circular]"';r.add(e);try{if(Array.isArray(e)){return`[${e.map(e=>t(e,n+1)).join(",")}]`}const r=Object.keys(e).sort();return`{${r.map(r=>`${JSON.stringify(r)}:${t(e[r],n+1)}`).join(",")}}`}finally{r.delete(e)}};try{return t(e)}catch{return String(e)}}min(e,t){return new VldArray({...this.config,minLength:e,errorMessage:t||r.getMessages().arrayMin(e)})}max(e,t){return new VldArray({...this.config,maxLength:e,errorMessage:t||r.getMessages().arrayMax(e)})}length(e,t){return new VldArray({...this.config,exactLength:e,minLength:void 0,maxLength:void 0,errorMessage:t||r.getMessages().arrayLength(e)})}nonempty(e){return new VldArray({...this.config,minLength:1,errorMessage:e||r.getMessages().arrayEmpty})}unique(e){return new VldArray({...this.config,unique:!0,errorMessage:e||"Array must contain unique items"})}between(e,r,t){return new VldArray({...this.config,minLength:e,maxLength:r,errorMessage:t||`Array length must be between ${e} and ${r}`})}}exports.VldArray=VldArray;
|