unischema 1.0.1 → 1.2.0
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/README.md +780 -228
- package/dist/adapters/backend/index.d.mts +2 -1
- package/dist/adapters/backend/index.d.ts +2 -1
- package/dist/adapters/backend/index.js +17 -441
- package/dist/adapters/backend/index.mjs +9 -433
- package/dist/adapters/frontend/index.d.mts +2 -1
- package/dist/adapters/frontend/index.d.ts +2 -1
- package/dist/adapters/frontend/index.js +10 -421
- package/dist/adapters/frontend/index.mjs +8 -419
- package/dist/chunk-5A4ITJVD.mjs +124 -0
- package/dist/chunk-66RFUBVU.js +131 -0
- package/dist/chunk-75YSYC4K.mjs +85 -0
- package/dist/chunk-76BBWQDH.js +90 -0
- package/dist/chunk-7XES4A3M.mjs +237 -0
- package/dist/chunk-BVRXGZLS.js +17 -0
- package/dist/chunk-COMVAVFU.mjs +335 -0
- package/dist/chunk-DT2TQZU7.js +796 -0
- package/dist/chunk-FPCCH55A.js +103 -0
- package/dist/chunk-IUXRLMET.js +206 -0
- package/dist/chunk-JEW6U6CB.js +353 -0
- package/dist/chunk-KZCV5IW4.mjs +97 -0
- package/dist/chunk-KZZ7NVU3.mjs +41 -0
- package/dist/chunk-MFEBMQAU.mjs +779 -0
- package/dist/chunk-OIYG5D2I.js +50 -0
- package/dist/chunk-RW6HDA5H.mjs +194 -0
- package/dist/chunk-TTK77YBI.mjs +15 -0
- package/dist/chunk-TXT36BCE.js +248 -0
- package/dist/index-C17xs-fU.d.mts +140 -0
- package/dist/index-C17xs-fU.d.ts +140 -0
- package/dist/index.d.mts +26 -7
- package/dist/index.d.ts +26 -7
- package/dist/index.js +769 -499
- package/dist/index.mjs +695 -487
- package/dist/{schema-D9DGC9E_.d.ts → schema-DYE8Wz8X.d.mts} +264 -79
- package/dist/{schema-D9DGC9E_.d.mts → schema-Dtp-joeT.d.ts} +264 -79
- package/dist/validators/array.d.mts +15 -0
- package/dist/validators/array.d.ts +15 -0
- package/dist/validators/array.js +31 -0
- package/dist/validators/array.mjs +2 -0
- package/dist/validators/common.d.mts +13 -0
- package/dist/validators/common.d.ts +13 -0
- package/dist/validators/common.js +27 -0
- package/dist/validators/common.mjs +2 -0
- package/dist/validators/date.d.mts +23 -0
- package/dist/validators/date.d.ts +23 -0
- package/dist/validators/date.js +47 -0
- package/dist/validators/date.mjs +2 -0
- package/dist/validators/index.d.mts +46 -0
- package/dist/validators/index.d.ts +46 -0
- package/dist/validators/index.js +256 -0
- package/dist/validators/index.mjs +7 -0
- package/dist/validators/number.d.mts +25 -0
- package/dist/validators/number.d.ts +25 -0
- package/dist/validators/number.js +51 -0
- package/dist/validators/number.mjs +2 -0
- package/dist/validators/object.d.mts +11 -0
- package/dist/validators/object.d.ts +11 -0
- package/dist/validators/object.js +23 -0
- package/dist/validators/object.mjs +2 -0
- package/dist/validators/string.d.mts +37 -0
- package/dist/validators/string.d.ts +37 -0
- package/dist/validators/string.js +75 -0
- package/dist/validators/string.mjs +2 -0
- package/package.json +82 -5
- package/dist/adapters/backend/index.js.map +0 -1
- package/dist/adapters/backend/index.mjs.map +0 -1
- package/dist/adapters/frontend/index.js.map +0 -1
- package/dist/adapters/frontend/index.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,796 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk76BBWQDH_js = require('./chunk-76BBWQDH.js');
|
|
4
|
+
var chunkFPCCH55A_js = require('./chunk-FPCCH55A.js');
|
|
5
|
+
var chunkJEW6U6CB_js = require('./chunk-JEW6U6CB.js');
|
|
6
|
+
var chunkIUXRLMET_js = require('./chunk-IUXRLMET.js');
|
|
7
|
+
var chunkTXT36BCE_js = require('./chunk-TXT36BCE.js');
|
|
8
|
+
var chunk66RFUBVU_js = require('./chunk-66RFUBVU.js');
|
|
9
|
+
|
|
10
|
+
// src/core/validators.ts
|
|
11
|
+
function createError(context, code, message, soft = false, received, expected) {
|
|
12
|
+
return {
|
|
13
|
+
field: context.path,
|
|
14
|
+
code,
|
|
15
|
+
message,
|
|
16
|
+
severity: soft ? "soft" : "hard",
|
|
17
|
+
received,
|
|
18
|
+
expected
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
var typeValidators = {
|
|
22
|
+
string: (value, _params, context) => {
|
|
23
|
+
if (value !== void 0 && value !== null && typeof value !== "string") {
|
|
24
|
+
return createError(context, "INVALID_TYPE", `Expected string, got ${typeof value}`);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
},
|
|
28
|
+
number: (value, _params, context) => {
|
|
29
|
+
if (value !== void 0 && value !== null && typeof value !== "number") {
|
|
30
|
+
return createError(context, "INVALID_TYPE", `Expected number, got ${typeof value}`);
|
|
31
|
+
}
|
|
32
|
+
if (typeof value === "number" && isNaN(value)) {
|
|
33
|
+
return createError(context, "INVALID_NUMBER", "Value is not a valid number");
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
},
|
|
37
|
+
boolean: (value, _params, context) => {
|
|
38
|
+
if (value !== void 0 && value !== null && typeof value !== "boolean") {
|
|
39
|
+
return createError(context, "INVALID_TYPE", `Expected boolean, got ${typeof value}`);
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
},
|
|
43
|
+
date: (value, _params, context) => {
|
|
44
|
+
if (value === void 0 || value === null) return null;
|
|
45
|
+
if (value instanceof Date) {
|
|
46
|
+
if (isNaN(value.getTime())) {
|
|
47
|
+
return createError(context, "INVALID_DATE", "Invalid date value");
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
if (typeof value === "string") {
|
|
52
|
+
const parsed = new Date(value);
|
|
53
|
+
if (isNaN(parsed.getTime())) {
|
|
54
|
+
return createError(context, "INVALID_DATE", "Invalid date format");
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return createError(context, "INVALID_TYPE", `Expected date, got ${typeof value}`);
|
|
59
|
+
},
|
|
60
|
+
array: (value, _params, context) => {
|
|
61
|
+
if (value !== void 0 && value !== null && !Array.isArray(value)) {
|
|
62
|
+
return createError(context, "INVALID_TYPE", `Expected array, got ${typeof value}`);
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
},
|
|
66
|
+
object: (value, _params, context) => {
|
|
67
|
+
if (value !== void 0 && value !== null) {
|
|
68
|
+
if (typeof value !== "object" || Array.isArray(value)) {
|
|
69
|
+
return createError(context, "INVALID_TYPE", `Expected object, got ${typeof value}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
var ruleValidators = {
|
|
76
|
+
required: (value, _params, context) => {
|
|
77
|
+
const isEmpty = value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0;
|
|
78
|
+
if (isEmpty) {
|
|
79
|
+
return createError(context, "REQUIRED", "This field is required");
|
|
80
|
+
}
|
|
81
|
+
return null;
|
|
82
|
+
},
|
|
83
|
+
min: (value, params, context) => {
|
|
84
|
+
const min = params?.value;
|
|
85
|
+
const soft = params?.soft;
|
|
86
|
+
const message = params?.message;
|
|
87
|
+
if (value === void 0 || value === null) return null;
|
|
88
|
+
if (typeof value === "number") {
|
|
89
|
+
if (value < min) {
|
|
90
|
+
return createError(
|
|
91
|
+
context,
|
|
92
|
+
"MIN_VALUE",
|
|
93
|
+
message || `Value must be at least ${min}`,
|
|
94
|
+
soft,
|
|
95
|
+
value,
|
|
96
|
+
{ min }
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (typeof value === "string") {
|
|
101
|
+
if (value.length < min) {
|
|
102
|
+
return createError(
|
|
103
|
+
context,
|
|
104
|
+
"MIN_LENGTH",
|
|
105
|
+
message || `Must be at least ${min} characters`,
|
|
106
|
+
soft,
|
|
107
|
+
value,
|
|
108
|
+
{ min }
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (Array.isArray(value)) {
|
|
113
|
+
if (value.length < min) {
|
|
114
|
+
return createError(
|
|
115
|
+
context,
|
|
116
|
+
"MIN_ITEMS",
|
|
117
|
+
message || `Must have at least ${min} items`,
|
|
118
|
+
soft,
|
|
119
|
+
value,
|
|
120
|
+
{ min }
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
},
|
|
126
|
+
max: (value, params, context) => {
|
|
127
|
+
const max = params?.value;
|
|
128
|
+
const soft = params?.soft;
|
|
129
|
+
const message = params?.message;
|
|
130
|
+
if (value === void 0 || value === null) return null;
|
|
131
|
+
if (typeof value === "number") {
|
|
132
|
+
if (value > max) {
|
|
133
|
+
return createError(
|
|
134
|
+
context,
|
|
135
|
+
"MAX_VALUE",
|
|
136
|
+
message || `Value must be at most ${max}`,
|
|
137
|
+
soft,
|
|
138
|
+
value,
|
|
139
|
+
{ max }
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (typeof value === "string") {
|
|
144
|
+
if (value.length > max) {
|
|
145
|
+
return createError(
|
|
146
|
+
context,
|
|
147
|
+
"MAX_LENGTH",
|
|
148
|
+
message || `Must be at most ${max} characters`,
|
|
149
|
+
soft,
|
|
150
|
+
value,
|
|
151
|
+
{ max }
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (Array.isArray(value)) {
|
|
156
|
+
if (value.length > max) {
|
|
157
|
+
return createError(
|
|
158
|
+
context,
|
|
159
|
+
"MAX_ITEMS",
|
|
160
|
+
message || `Must have at most ${max} items`,
|
|
161
|
+
soft,
|
|
162
|
+
value,
|
|
163
|
+
{ max }
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
},
|
|
169
|
+
// String validators
|
|
170
|
+
email: chunkJEW6U6CB_js.emailValidator,
|
|
171
|
+
url: chunkJEW6U6CB_js.urlValidator,
|
|
172
|
+
ipAddress: chunkJEW6U6CB_js.ipAddressValidator,
|
|
173
|
+
ipv6: chunkJEW6U6CB_js.ipv6Validator,
|
|
174
|
+
alpha: chunkJEW6U6CB_js.alphaValidator,
|
|
175
|
+
alphanumeric: chunkJEW6U6CB_js.alphanumericValidator,
|
|
176
|
+
numeric: chunkJEW6U6CB_js.numericValidator,
|
|
177
|
+
lowercase: chunkJEW6U6CB_js.lowercaseValidator,
|
|
178
|
+
uppercase: chunkJEW6U6CB_js.uppercaseValidator,
|
|
179
|
+
slug: chunkJEW6U6CB_js.slugValidator,
|
|
180
|
+
hex: chunkJEW6U6CB_js.hexValidator,
|
|
181
|
+
base64: chunkJEW6U6CB_js.base64Validator,
|
|
182
|
+
json: chunkJEW6U6CB_js.jsonValidator,
|
|
183
|
+
length: chunkJEW6U6CB_js.lengthValidator,
|
|
184
|
+
contains: chunkJEW6U6CB_js.containsValidator,
|
|
185
|
+
startsWith: chunkJEW6U6CB_js.startsWithValidator,
|
|
186
|
+
endsWith: chunkJEW6U6CB_js.endsWithValidator,
|
|
187
|
+
// Number validators
|
|
188
|
+
port: chunkIUXRLMET_js.portValidator,
|
|
189
|
+
latitude: chunkIUXRLMET_js.latitudeValidator,
|
|
190
|
+
longitude: chunkIUXRLMET_js.longitudeValidator,
|
|
191
|
+
percentage: chunkIUXRLMET_js.percentageValidator,
|
|
192
|
+
numberBetween: chunkIUXRLMET_js.betweenValidator,
|
|
193
|
+
divisibleBy: chunkIUXRLMET_js.divisibleByValidator,
|
|
194
|
+
multipleOf: chunkIUXRLMET_js.multipleOfValidator,
|
|
195
|
+
even: chunkIUXRLMET_js.evenValidator,
|
|
196
|
+
odd: chunkIUXRLMET_js.oddValidator,
|
|
197
|
+
safe: chunkIUXRLMET_js.safeValidator,
|
|
198
|
+
finite: chunkIUXRLMET_js.finiteValidator,
|
|
199
|
+
// Date validators
|
|
200
|
+
today: chunkTXT36BCE_js.todayValidator,
|
|
201
|
+
yesterday: chunkTXT36BCE_js.yesterdayValidator,
|
|
202
|
+
tomorrow: chunkTXT36BCE_js.tomorrowValidator,
|
|
203
|
+
thisWeek: chunkTXT36BCE_js.thisWeekValidator,
|
|
204
|
+
thisMonth: chunkTXT36BCE_js.thisMonthValidator,
|
|
205
|
+
thisYear: chunkTXT36BCE_js.thisYearValidator,
|
|
206
|
+
weekday: chunkTXT36BCE_js.weekdayValidator,
|
|
207
|
+
weekend: chunkTXT36BCE_js.weekendValidator,
|
|
208
|
+
age: chunkTXT36BCE_js.ageValidator,
|
|
209
|
+
dateBetween: chunkTXT36BCE_js.betweenValidator,
|
|
210
|
+
// Array validators
|
|
211
|
+
includes: chunk66RFUBVU_js.includesValidator,
|
|
212
|
+
excludes: chunk66RFUBVU_js.excludesValidator,
|
|
213
|
+
empty: chunk66RFUBVU_js.emptyValidator,
|
|
214
|
+
notEmpty: chunk66RFUBVU_js.notEmptyValidator,
|
|
215
|
+
sorted: chunk66RFUBVU_js.sortedValidator,
|
|
216
|
+
compact: chunk66RFUBVU_js.compactValidator,
|
|
217
|
+
// Object validators
|
|
218
|
+
keys: chunk76BBWQDH_js.keysValidator,
|
|
219
|
+
pick: chunk76BBWQDH_js.pickValidator,
|
|
220
|
+
omit: chunk76BBWQDH_js.omitValidator,
|
|
221
|
+
strict: chunk76BBWQDH_js.strictValidator,
|
|
222
|
+
// Cross-field validators
|
|
223
|
+
notMatches: chunkFPCCH55A_js.notMatchesValidator,
|
|
224
|
+
greaterThan: chunkFPCCH55A_js.greaterThanValidator,
|
|
225
|
+
lessThan: chunkFPCCH55A_js.lessThanValidator,
|
|
226
|
+
when: chunkFPCCH55A_js.whenValidator,
|
|
227
|
+
dependsOn: chunkFPCCH55A_js.dependsOnValidator,
|
|
228
|
+
pattern: (value, params, context) => {
|
|
229
|
+
if (value === void 0 || value === null || value === "") return null;
|
|
230
|
+
const pattern = params?.pattern;
|
|
231
|
+
const soft = params?.soft;
|
|
232
|
+
const message = params?.message;
|
|
233
|
+
if (typeof value !== "string") return null;
|
|
234
|
+
const regex = new RegExp(pattern);
|
|
235
|
+
if (!regex.test(value)) {
|
|
236
|
+
return createError(
|
|
237
|
+
context,
|
|
238
|
+
"PATTERN_MISMATCH",
|
|
239
|
+
message || `Value does not match required pattern`,
|
|
240
|
+
soft
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
return null;
|
|
244
|
+
},
|
|
245
|
+
enum: (value, params, context) => {
|
|
246
|
+
if (value === void 0 || value === null) return null;
|
|
247
|
+
const values = params?.values;
|
|
248
|
+
const soft = params?.soft;
|
|
249
|
+
const message = params?.message;
|
|
250
|
+
if (!values.includes(value)) {
|
|
251
|
+
return createError(
|
|
252
|
+
context,
|
|
253
|
+
"INVALID_ENUM",
|
|
254
|
+
message || `Value must be one of: ${values.join(", ")}`,
|
|
255
|
+
soft
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
return null;
|
|
259
|
+
},
|
|
260
|
+
custom: (value, params, context) => {
|
|
261
|
+
const validate2 = params?.validate;
|
|
262
|
+
const soft = params?.soft;
|
|
263
|
+
const message = params?.message;
|
|
264
|
+
if (!validate2) return null;
|
|
265
|
+
const result = validate2(value, context);
|
|
266
|
+
if (typeof result === "boolean") {
|
|
267
|
+
if (!result) {
|
|
268
|
+
return createError(
|
|
269
|
+
context,
|
|
270
|
+
"CUSTOM_VALIDATION",
|
|
271
|
+
message || "Validation failed",
|
|
272
|
+
soft
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
return null;
|
|
276
|
+
}
|
|
277
|
+
if (!result.valid) {
|
|
278
|
+
return createError(
|
|
279
|
+
context,
|
|
280
|
+
"CUSTOM_VALIDATION",
|
|
281
|
+
result.message || message || "Validation failed",
|
|
282
|
+
soft
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
},
|
|
287
|
+
// Enterprise patterns - matches field against another field
|
|
288
|
+
matches: (value, params, context) => {
|
|
289
|
+
if (value === void 0 || value === null) return null;
|
|
290
|
+
const otherField = params?.field;
|
|
291
|
+
const soft = params?.soft;
|
|
292
|
+
const message = params?.message;
|
|
293
|
+
const root = context.root;
|
|
294
|
+
const otherValue = root[otherField];
|
|
295
|
+
if (value !== otherValue) {
|
|
296
|
+
return createError(
|
|
297
|
+
context,
|
|
298
|
+
"FIELD_MISMATCH",
|
|
299
|
+
message || `Must match ${otherField}`,
|
|
300
|
+
soft
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
return null;
|
|
304
|
+
},
|
|
305
|
+
// Integer validation
|
|
306
|
+
integer: (value, params, context) => {
|
|
307
|
+
if (value === void 0 || value === null) return null;
|
|
308
|
+
const soft = params?.soft;
|
|
309
|
+
const message = params?.message;
|
|
310
|
+
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
311
|
+
return createError(
|
|
312
|
+
context,
|
|
313
|
+
"NOT_INTEGER",
|
|
314
|
+
message || "Value must be an integer",
|
|
315
|
+
soft
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
return null;
|
|
319
|
+
},
|
|
320
|
+
// Positive number validation
|
|
321
|
+
positive: (value, params, context) => {
|
|
322
|
+
if (value === void 0 || value === null) return null;
|
|
323
|
+
const soft = params?.soft;
|
|
324
|
+
const message = params?.message;
|
|
325
|
+
if (typeof value === "number" && value <= 0) {
|
|
326
|
+
return createError(
|
|
327
|
+
context,
|
|
328
|
+
"NOT_POSITIVE",
|
|
329
|
+
message || "Value must be positive",
|
|
330
|
+
soft
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
return null;
|
|
334
|
+
},
|
|
335
|
+
// Negative number validation
|
|
336
|
+
negative: (value, params, context) => {
|
|
337
|
+
if (value === void 0 || value === null) return null;
|
|
338
|
+
const soft = params?.soft;
|
|
339
|
+
const message = params?.message;
|
|
340
|
+
if (typeof value === "number" && value >= 0) {
|
|
341
|
+
return createError(
|
|
342
|
+
context,
|
|
343
|
+
"NOT_NEGATIVE",
|
|
344
|
+
message || "Value must be negative",
|
|
345
|
+
soft
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
return null;
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
var customValidators = /* @__PURE__ */ new Map();
|
|
352
|
+
function registerValidator(name, validator) {
|
|
353
|
+
customValidators.set(name, validator);
|
|
354
|
+
}
|
|
355
|
+
function getValidator(name) {
|
|
356
|
+
return ruleValidators[name] ?? customValidators.get(name);
|
|
357
|
+
}
|
|
358
|
+
function getTypeValidator(type) {
|
|
359
|
+
return typeValidators[type];
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// src/core/engine.ts
|
|
363
|
+
function validateField(fieldDef, value, context) {
|
|
364
|
+
const errors = [];
|
|
365
|
+
let processedValue = value;
|
|
366
|
+
if (value === null || value === void 0) {
|
|
367
|
+
if (fieldDef.nullish || fieldDef.nullable && value === null) {
|
|
368
|
+
if (!fieldDef.required) {
|
|
369
|
+
return errors;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
if (fieldDef.preprocess) {
|
|
374
|
+
processedValue = fieldDef.preprocess(value);
|
|
375
|
+
}
|
|
376
|
+
if (fieldDef.transforms && fieldDef.transforms.length > 0) {
|
|
377
|
+
for (const transform of fieldDef.transforms) {
|
|
378
|
+
processedValue = transform(processedValue);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const typeValidator = getTypeValidator(fieldDef.type);
|
|
382
|
+
if (typeValidator) {
|
|
383
|
+
const typeError = typeValidator(processedValue, void 0, context);
|
|
384
|
+
if (typeError) {
|
|
385
|
+
errors.push({
|
|
386
|
+
...typeError,
|
|
387
|
+
path: context.path.split(".").filter((p) => p !== "")
|
|
388
|
+
});
|
|
389
|
+
return errors;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
if (fieldDef.required) {
|
|
393
|
+
const isMissing = processedValue === void 0 && !fieldDef.nullish || processedValue === null && !fieldDef.nullable && !fieldDef.nullish || processedValue === "" && !fieldDef.nullable && !fieldDef.nullish || Array.isArray(processedValue) && processedValue.length === 0;
|
|
394
|
+
if (isMissing) {
|
|
395
|
+
const requiredValidator = getValidator("required");
|
|
396
|
+
if (requiredValidator) {
|
|
397
|
+
const error = requiredValidator(processedValue, void 0, context);
|
|
398
|
+
if (error) {
|
|
399
|
+
errors.push({
|
|
400
|
+
...error,
|
|
401
|
+
path: context.path.split(".").filter((p) => p !== "")
|
|
402
|
+
});
|
|
403
|
+
return errors;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
} else if (value === void 0 || value === null || value === "") {
|
|
408
|
+
return errors;
|
|
409
|
+
}
|
|
410
|
+
for (const rule of fieldDef.rules) {
|
|
411
|
+
const validator = getValidator(rule.type);
|
|
412
|
+
if (!validator) {
|
|
413
|
+
console.warn(`Unknown validator: ${rule.type}`);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
const params = {
|
|
417
|
+
...rule.params,
|
|
418
|
+
soft: rule.soft,
|
|
419
|
+
message: rule.message
|
|
420
|
+
};
|
|
421
|
+
const error = validator(processedValue, params, context);
|
|
422
|
+
if (error) {
|
|
423
|
+
errors.push({
|
|
424
|
+
...error,
|
|
425
|
+
path: context.path.split(".").filter((p) => p !== "")
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
if (fieldDef.type === "object" && fieldDef.schema && processedValue !== null && processedValue !== void 0) {
|
|
430
|
+
const nestedResult = validateSchema(fieldDef.schema, processedValue, context.path, context.root);
|
|
431
|
+
errors.push(...nestedResult.hardErrors, ...nestedResult.softErrors);
|
|
432
|
+
}
|
|
433
|
+
if (fieldDef.type === "array" && fieldDef.items && Array.isArray(processedValue)) {
|
|
434
|
+
for (let i = 0; i < processedValue.length; i++) {
|
|
435
|
+
const itemContext = {
|
|
436
|
+
path: `${context.path}[${i}]`,
|
|
437
|
+
root: context.root,
|
|
438
|
+
parent: processedValue
|
|
439
|
+
};
|
|
440
|
+
const itemErrors = validateField(fieldDef.items, processedValue[i], itemContext);
|
|
441
|
+
errors.push(...itemErrors);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
return errors;
|
|
445
|
+
}
|
|
446
|
+
function validateSchema(schema, data, basePath = "", root, options) {
|
|
447
|
+
const hardErrors = [];
|
|
448
|
+
const softErrors = [];
|
|
449
|
+
const rootData = root ?? data;
|
|
450
|
+
for (const [fieldName, fieldDef] of Object.entries(schema.fields)) {
|
|
451
|
+
const value = data[fieldName];
|
|
452
|
+
const path = basePath ? `${basePath}.${fieldName}` : fieldName;
|
|
453
|
+
const context = {
|
|
454
|
+
path,
|
|
455
|
+
root: rootData,
|
|
456
|
+
parent: data
|
|
457
|
+
};
|
|
458
|
+
const errors = validateField(fieldDef, value, context);
|
|
459
|
+
for (let error of errors) {
|
|
460
|
+
if (options?.errorMap) {
|
|
461
|
+
const mapped = options.errorMap(error);
|
|
462
|
+
if ("message" in mapped && !("field" in mapped)) {
|
|
463
|
+
error = { ...error, message: mapped.message };
|
|
464
|
+
} else {
|
|
465
|
+
error = mapped;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (error.severity === "soft") {
|
|
469
|
+
softErrors.push(error);
|
|
470
|
+
} else {
|
|
471
|
+
hardErrors.push(error);
|
|
472
|
+
if (options?.abortEarly) {
|
|
473
|
+
return buildValidationResult(hardErrors, softErrors, options);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return buildValidationResult(hardErrors, softErrors, options);
|
|
479
|
+
}
|
|
480
|
+
function buildValidationResult(hardErrors, softErrors, options) {
|
|
481
|
+
const result = {
|
|
482
|
+
valid: hardErrors.length === 0,
|
|
483
|
+
hardErrors,
|
|
484
|
+
softErrors
|
|
485
|
+
};
|
|
486
|
+
if (options?.aggregateByField) {
|
|
487
|
+
const errorsByField = {};
|
|
488
|
+
for (const error of [...hardErrors, ...softErrors]) {
|
|
489
|
+
const field = error.field;
|
|
490
|
+
if (!errorsByField[field]) {
|
|
491
|
+
errorsByField[field] = [];
|
|
492
|
+
}
|
|
493
|
+
errorsByField[field].push(error);
|
|
494
|
+
}
|
|
495
|
+
result.errorsByField = errorsByField;
|
|
496
|
+
}
|
|
497
|
+
return result;
|
|
498
|
+
}
|
|
499
|
+
function validate(schema, data, options) {
|
|
500
|
+
return validateSchema(schema, data, "", void 0, options);
|
|
501
|
+
}
|
|
502
|
+
function isValid(schema, data, options) {
|
|
503
|
+
return validate(schema, data, options).valid;
|
|
504
|
+
}
|
|
505
|
+
function assertValid(schema, data, options) {
|
|
506
|
+
const result = validate(schema, data, options);
|
|
507
|
+
if (!result.valid) {
|
|
508
|
+
const error = new Error("Validation failed");
|
|
509
|
+
error.errors = result.hardErrors;
|
|
510
|
+
throw error;
|
|
511
|
+
}
|
|
512
|
+
return data;
|
|
513
|
+
}
|
|
514
|
+
function mergeResults(...results) {
|
|
515
|
+
const hardErrors = [];
|
|
516
|
+
const softErrors = [];
|
|
517
|
+
for (const result of results) {
|
|
518
|
+
hardErrors.push(...result.hardErrors);
|
|
519
|
+
softErrors.push(...result.softErrors);
|
|
520
|
+
}
|
|
521
|
+
return {
|
|
522
|
+
valid: hardErrors.length === 0,
|
|
523
|
+
hardErrors,
|
|
524
|
+
softErrors
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function validResult() {
|
|
528
|
+
return {
|
|
529
|
+
valid: true,
|
|
530
|
+
hardErrors: [],
|
|
531
|
+
softErrors: []
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
function errorResult(field, code, message, soft = false) {
|
|
535
|
+
const error = {
|
|
536
|
+
field,
|
|
537
|
+
code,
|
|
538
|
+
message,
|
|
539
|
+
severity: soft ? "soft" : "hard"
|
|
540
|
+
};
|
|
541
|
+
return {
|
|
542
|
+
valid: soft,
|
|
543
|
+
hardErrors: soft ? [] : [error],
|
|
544
|
+
softErrors: soft ? [error] : []
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// src/core/async-engine.ts
|
|
549
|
+
var debounceCache = /* @__PURE__ */ new Map();
|
|
550
|
+
function createDebouncedValidation(key, validate2, value, context, message, soft = false, timeout = 5e3, debounceMs = 300) {
|
|
551
|
+
const cache = debounceCache.get(key) || {};
|
|
552
|
+
if (cache.timeoutId !== void 0) {
|
|
553
|
+
clearTimeout(cache.timeoutId);
|
|
554
|
+
}
|
|
555
|
+
if (cache.reject) {
|
|
556
|
+
cache.reject(new Error("Validation cancelled - new input received"));
|
|
557
|
+
}
|
|
558
|
+
return new Promise((resolve, reject) => {
|
|
559
|
+
cache.resolve = resolve;
|
|
560
|
+
cache.reject = reject;
|
|
561
|
+
cache.timeoutId = setTimeout(async () => {
|
|
562
|
+
try {
|
|
563
|
+
const result = await executeAsyncValidator(validate2, value, context, message, soft, timeout);
|
|
564
|
+
resolve(result);
|
|
565
|
+
debounceCache.delete(key);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
reject(error);
|
|
568
|
+
debounceCache.delete(key);
|
|
569
|
+
}
|
|
570
|
+
}, debounceMs);
|
|
571
|
+
debounceCache.set(key, cache);
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
function withTimeout(promise, timeoutMs, errorMessage) {
|
|
575
|
+
return Promise.race([
|
|
576
|
+
promise,
|
|
577
|
+
new Promise(
|
|
578
|
+
(_, reject) => setTimeout(() => reject(new Error(errorMessage)), timeoutMs)
|
|
579
|
+
)
|
|
580
|
+
]);
|
|
581
|
+
}
|
|
582
|
+
async function executeAsyncValidator(validate2, value, context, message, soft = false, timeout = 5e3) {
|
|
583
|
+
try {
|
|
584
|
+
const result = await withTimeout(
|
|
585
|
+
validate2(value),
|
|
586
|
+
timeout,
|
|
587
|
+
`Async validation timed out after ${timeout}ms`
|
|
588
|
+
);
|
|
589
|
+
if (typeof result === "boolean") {
|
|
590
|
+
if (result) {
|
|
591
|
+
return null;
|
|
592
|
+
}
|
|
593
|
+
return {
|
|
594
|
+
field: context.path,
|
|
595
|
+
path: context.path.split("."),
|
|
596
|
+
code: "ASYNC_VALIDATION_FAILED",
|
|
597
|
+
message: message || "Async validation failed",
|
|
598
|
+
severity: soft ? "soft" : "hard",
|
|
599
|
+
received: value
|
|
600
|
+
};
|
|
601
|
+
}
|
|
602
|
+
if (!result.valid) {
|
|
603
|
+
return {
|
|
604
|
+
field: context.path,
|
|
605
|
+
path: context.path.split("."),
|
|
606
|
+
code: "ASYNC_VALIDATION_FAILED",
|
|
607
|
+
message: result.message || message || "Async validation failed",
|
|
608
|
+
severity: soft ? "soft" : "hard",
|
|
609
|
+
received: value
|
|
610
|
+
};
|
|
611
|
+
}
|
|
612
|
+
return null;
|
|
613
|
+
} catch (error) {
|
|
614
|
+
return {
|
|
615
|
+
field: context.path,
|
|
616
|
+
path: context.path.split("."),
|
|
617
|
+
code: "ASYNC_VALIDATION_ERROR",
|
|
618
|
+
message: error instanceof Error ? error.message : "Async validation error",
|
|
619
|
+
severity: soft ? "soft" : "hard",
|
|
620
|
+
received: value
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
async function validateFieldAsync(fieldDef, value, context) {
|
|
625
|
+
const errors = [];
|
|
626
|
+
const asyncValidations = [];
|
|
627
|
+
const typeValidator = getTypeValidator(fieldDef.type);
|
|
628
|
+
if (typeValidator) {
|
|
629
|
+
const typeError = typeValidator(value, void 0, context);
|
|
630
|
+
if (typeError) {
|
|
631
|
+
errors.push({
|
|
632
|
+
...typeError,
|
|
633
|
+
path: context.path.split(".")
|
|
634
|
+
});
|
|
635
|
+
return errors;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
if (fieldDef.required) {
|
|
639
|
+
const requiredValidator = getValidator("required");
|
|
640
|
+
if (requiredValidator) {
|
|
641
|
+
const error = requiredValidator(value, void 0, context);
|
|
642
|
+
if (error) {
|
|
643
|
+
errors.push({
|
|
644
|
+
...error,
|
|
645
|
+
path: context.path.split(".")
|
|
646
|
+
});
|
|
647
|
+
return errors;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
} else if (value === void 0 || value === null || value === "") {
|
|
651
|
+
return errors;
|
|
652
|
+
}
|
|
653
|
+
for (const rule of fieldDef.rules) {
|
|
654
|
+
if (rule.async && rule.type === "refineAsync") {
|
|
655
|
+
const validate2 = rule.params?.validate;
|
|
656
|
+
if (!validate2) continue;
|
|
657
|
+
if (rule.debounce && rule.debounce > 0) {
|
|
658
|
+
const key = `${context.path}_${rule.type}`;
|
|
659
|
+
asyncValidations.push(
|
|
660
|
+
createDebouncedValidation(
|
|
661
|
+
key,
|
|
662
|
+
validate2,
|
|
663
|
+
value,
|
|
664
|
+
context,
|
|
665
|
+
rule.message,
|
|
666
|
+
rule.soft,
|
|
667
|
+
rule.timeout || 5e3,
|
|
668
|
+
rule.debounce
|
|
669
|
+
)
|
|
670
|
+
);
|
|
671
|
+
} else {
|
|
672
|
+
asyncValidations.push(
|
|
673
|
+
executeAsyncValidator(
|
|
674
|
+
validate2,
|
|
675
|
+
value,
|
|
676
|
+
context,
|
|
677
|
+
rule.message,
|
|
678
|
+
rule.soft,
|
|
679
|
+
rule.timeout || 5e3
|
|
680
|
+
)
|
|
681
|
+
);
|
|
682
|
+
}
|
|
683
|
+
} else {
|
|
684
|
+
const validator = getValidator(rule.type);
|
|
685
|
+
if (!validator) {
|
|
686
|
+
console.warn(`Unknown validator: ${rule.type}`);
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
const params = {
|
|
690
|
+
...rule.params,
|
|
691
|
+
soft: rule.soft,
|
|
692
|
+
message: rule.message
|
|
693
|
+
};
|
|
694
|
+
const error = validator(value, params, context);
|
|
695
|
+
if (error) {
|
|
696
|
+
errors.push({
|
|
697
|
+
...error,
|
|
698
|
+
path: context.path.split(".")
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
const asyncErrors = await Promise.all(asyncValidations);
|
|
704
|
+
for (const error of asyncErrors) {
|
|
705
|
+
if (error) {
|
|
706
|
+
errors.push(error);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
if (fieldDef.type === "object" && fieldDef.schema && value !== null && value !== void 0) {
|
|
710
|
+
const nestedResult = await validateSchemaAsync(
|
|
711
|
+
fieldDef.schema,
|
|
712
|
+
value,
|
|
713
|
+
context.path,
|
|
714
|
+
context.root
|
|
715
|
+
);
|
|
716
|
+
errors.push(...nestedResult.hardErrors, ...nestedResult.softErrors);
|
|
717
|
+
}
|
|
718
|
+
if (fieldDef.type === "array" && fieldDef.items && Array.isArray(value)) {
|
|
719
|
+
const itemValidations = value.map(async (item, i) => {
|
|
720
|
+
const itemContext = {
|
|
721
|
+
path: `${context.path}[${i}]`,
|
|
722
|
+
root: context.root,
|
|
723
|
+
parent: value
|
|
724
|
+
};
|
|
725
|
+
return validateFieldAsync(fieldDef.items, item, itemContext);
|
|
726
|
+
});
|
|
727
|
+
const itemResults = await Promise.all(itemValidations);
|
|
728
|
+
for (const itemErrors of itemResults) {
|
|
729
|
+
errors.push(...itemErrors);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
return errors;
|
|
733
|
+
}
|
|
734
|
+
async function validateSchemaAsync(schema, data, basePath = "", root) {
|
|
735
|
+
const hardErrors = [];
|
|
736
|
+
const softErrors = [];
|
|
737
|
+
const rootData = root ?? data;
|
|
738
|
+
const fieldValidations = Object.entries(schema.fields).map(async ([fieldName, fieldDef]) => {
|
|
739
|
+
const value = data[fieldName];
|
|
740
|
+
const path = basePath ? `${basePath}.${fieldName}` : fieldName;
|
|
741
|
+
const context = {
|
|
742
|
+
path,
|
|
743
|
+
root: rootData,
|
|
744
|
+
parent: data
|
|
745
|
+
};
|
|
746
|
+
return validateFieldAsync(fieldDef, value, context);
|
|
747
|
+
});
|
|
748
|
+
const results = await Promise.all(fieldValidations);
|
|
749
|
+
for (const errors of results) {
|
|
750
|
+
for (const error of errors) {
|
|
751
|
+
if (error.severity === "soft") {
|
|
752
|
+
softErrors.push(error);
|
|
753
|
+
} else {
|
|
754
|
+
hardErrors.push(error);
|
|
755
|
+
}
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
return {
|
|
759
|
+
valid: hardErrors.length === 0,
|
|
760
|
+
hardErrors,
|
|
761
|
+
softErrors
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
async function validateAsync(schema, data) {
|
|
765
|
+
return validateSchemaAsync(schema, data);
|
|
766
|
+
}
|
|
767
|
+
async function isValidAsync(schema, data) {
|
|
768
|
+
const result = await validateAsync(schema, data);
|
|
769
|
+
return result.valid;
|
|
770
|
+
}
|
|
771
|
+
async function assertValidAsync(schema, data) {
|
|
772
|
+
const result = await validateAsync(schema, data);
|
|
773
|
+
if (!result.valid) {
|
|
774
|
+
const error = new Error("Async validation failed");
|
|
775
|
+
error.errors = result.hardErrors;
|
|
776
|
+
throw error;
|
|
777
|
+
}
|
|
778
|
+
return data;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
exports.assertValid = assertValid;
|
|
782
|
+
exports.assertValidAsync = assertValidAsync;
|
|
783
|
+
exports.errorResult = errorResult;
|
|
784
|
+
exports.getTypeValidator = getTypeValidator;
|
|
785
|
+
exports.getValidator = getValidator;
|
|
786
|
+
exports.isValid = isValid;
|
|
787
|
+
exports.isValidAsync = isValidAsync;
|
|
788
|
+
exports.mergeResults = mergeResults;
|
|
789
|
+
exports.registerValidator = registerValidator;
|
|
790
|
+
exports.ruleValidators = ruleValidators;
|
|
791
|
+
exports.typeValidators = typeValidators;
|
|
792
|
+
exports.validResult = validResult;
|
|
793
|
+
exports.validate = validate;
|
|
794
|
+
exports.validateAsync = validateAsync;
|
|
795
|
+
exports.validateSchema = validateSchema;
|
|
796
|
+
exports.validateSchemaAsync = validateSchemaAsync;
|