unischema 1.0.1 → 1.1.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.
Files changed (70) hide show
  1. package/README.md +634 -231
  2. package/dist/adapters/backend/index.d.mts +2 -1
  3. package/dist/adapters/backend/index.d.ts +2 -1
  4. package/dist/adapters/backend/index.js +17 -441
  5. package/dist/adapters/backend/index.mjs +9 -433
  6. package/dist/adapters/frontend/index.d.mts +2 -1
  7. package/dist/adapters/frontend/index.d.ts +2 -1
  8. package/dist/adapters/frontend/index.js +10 -421
  9. package/dist/adapters/frontend/index.mjs +8 -419
  10. package/dist/chunk-2JYFKT3R.js +103 -0
  11. package/dist/chunk-3FANCMEF.js +206 -0
  12. package/dist/chunk-3TS35CVJ.mjs +478 -0
  13. package/dist/chunk-ASKTY6EG.js +131 -0
  14. package/dist/chunk-BJLVOIAP.js +491 -0
  15. package/dist/chunk-BNIB23NQ.js +90 -0
  16. package/dist/chunk-BVRXGZLS.js +17 -0
  17. package/dist/chunk-CQYXR2LZ.js +353 -0
  18. package/dist/chunk-ELL7U7IC.mjs +237 -0
  19. package/dist/chunk-FKDWSZIV.mjs +39 -0
  20. package/dist/chunk-FRBZHN4K.mjs +335 -0
  21. package/dist/chunk-FZ7K2PC7.js +248 -0
  22. package/dist/chunk-KHHJD6QK.mjs +85 -0
  23. package/dist/chunk-NUW55QTO.js +48 -0
  24. package/dist/chunk-TTK77YBI.mjs +15 -0
  25. package/dist/chunk-VWP24NYS.mjs +194 -0
  26. package/dist/chunk-XC4DKEXP.mjs +97 -0
  27. package/dist/chunk-XGTUU27F.mjs +124 -0
  28. package/dist/index-BQR7OrY7.d.mts +80 -0
  29. package/dist/index-BQR7OrY7.d.ts +80 -0
  30. package/dist/index.d.mts +3 -2
  31. package/dist/index.d.ts +3 -2
  32. package/dist/index.js +527 -494
  33. package/dist/index.mjs +476 -482
  34. package/dist/{schema-D9DGC9E_.d.mts → schema-CpAjXgEF.d.ts} +182 -79
  35. package/dist/{schema-D9DGC9E_.d.ts → schema-DYU1zGVm.d.mts} +182 -79
  36. package/dist/validators/array.d.mts +15 -0
  37. package/dist/validators/array.d.ts +15 -0
  38. package/dist/validators/array.js +31 -0
  39. package/dist/validators/array.mjs +2 -0
  40. package/dist/validators/common.d.mts +13 -0
  41. package/dist/validators/common.d.ts +13 -0
  42. package/dist/validators/common.js +27 -0
  43. package/dist/validators/common.mjs +2 -0
  44. package/dist/validators/date.d.mts +23 -0
  45. package/dist/validators/date.d.ts +23 -0
  46. package/dist/validators/date.js +47 -0
  47. package/dist/validators/date.mjs +2 -0
  48. package/dist/validators/index.d.mts +46 -0
  49. package/dist/validators/index.d.ts +46 -0
  50. package/dist/validators/index.js +256 -0
  51. package/dist/validators/index.mjs +7 -0
  52. package/dist/validators/number.d.mts +25 -0
  53. package/dist/validators/number.d.ts +25 -0
  54. package/dist/validators/number.js +51 -0
  55. package/dist/validators/number.mjs +2 -0
  56. package/dist/validators/object.d.mts +11 -0
  57. package/dist/validators/object.d.ts +11 -0
  58. package/dist/validators/object.js +23 -0
  59. package/dist/validators/object.mjs +2 -0
  60. package/dist/validators/string.d.mts +37 -0
  61. package/dist/validators/string.d.ts +37 -0
  62. package/dist/validators/string.js +75 -0
  63. package/dist/validators/string.mjs +2 -0
  64. package/package.json +36 -1
  65. package/dist/adapters/backend/index.js.map +0 -1
  66. package/dist/adapters/backend/index.mjs.map +0 -1
  67. package/dist/adapters/frontend/index.js.map +0 -1
  68. package/dist/adapters/frontend/index.mjs.map +0 -1
  69. package/dist/index.js.map +0 -1
  70. package/dist/index.mjs.map +0 -1
@@ -1,422 +1,13 @@
1
1
  'use strict';
2
2
 
3
- // src/core/validators.ts
4
- function createError(context, code, message, soft = false) {
5
- return {
6
- field: context.path,
7
- code,
8
- message,
9
- severity: soft ? "soft" : "hard"
10
- };
11
- }
12
- var typeValidators = {
13
- string: (value, _params, context) => {
14
- if (value !== void 0 && value !== null && typeof value !== "string") {
15
- return createError(context, "INVALID_TYPE", `Expected string, got ${typeof value}`);
16
- }
17
- return null;
18
- },
19
- number: (value, _params, context) => {
20
- if (value !== void 0 && value !== null && typeof value !== "number") {
21
- return createError(context, "INVALID_TYPE", `Expected number, got ${typeof value}`);
22
- }
23
- if (typeof value === "number" && isNaN(value)) {
24
- return createError(context, "INVALID_NUMBER", "Value is not a valid number");
25
- }
26
- return null;
27
- },
28
- boolean: (value, _params, context) => {
29
- if (value !== void 0 && value !== null && typeof value !== "boolean") {
30
- return createError(context, "INVALID_TYPE", `Expected boolean, got ${typeof value}`);
31
- }
32
- return null;
33
- },
34
- date: (value, _params, context) => {
35
- if (value === void 0 || value === null) return null;
36
- if (value instanceof Date) {
37
- if (isNaN(value.getTime())) {
38
- return createError(context, "INVALID_DATE", "Invalid date value");
39
- }
40
- return null;
41
- }
42
- if (typeof value === "string") {
43
- const parsed = new Date(value);
44
- if (isNaN(parsed.getTime())) {
45
- return createError(context, "INVALID_DATE", "Invalid date format");
46
- }
47
- return null;
48
- }
49
- return createError(context, "INVALID_TYPE", `Expected date, got ${typeof value}`);
50
- },
51
- array: (value, _params, context) => {
52
- if (value !== void 0 && value !== null && !Array.isArray(value)) {
53
- return createError(context, "INVALID_TYPE", `Expected array, got ${typeof value}`);
54
- }
55
- return null;
56
- },
57
- object: (value, _params, context) => {
58
- if (value !== void 0 && value !== null) {
59
- if (typeof value !== "object" || Array.isArray(value)) {
60
- return createError(context, "INVALID_TYPE", `Expected object, got ${typeof value}`);
61
- }
62
- }
63
- return null;
64
- }
65
- };
66
- var ruleValidators = {
67
- required: (value, _params, context) => {
68
- const isEmpty = value === void 0 || value === null || value === "" || Array.isArray(value) && value.length === 0;
69
- if (isEmpty) {
70
- return createError(context, "REQUIRED", "This field is required");
71
- }
72
- return null;
73
- },
74
- min: (value, params, context) => {
75
- const min = params?.value;
76
- const soft = params?.soft;
77
- const message = params?.message;
78
- if (value === void 0 || value === null) return null;
79
- if (typeof value === "number") {
80
- if (value < min) {
81
- return createError(
82
- context,
83
- "MIN_VALUE",
84
- message || `Value must be at least ${min}`,
85
- soft
86
- );
87
- }
88
- }
89
- if (typeof value === "string") {
90
- if (value.length < min) {
91
- return createError(
92
- context,
93
- "MIN_LENGTH",
94
- message || `Must be at least ${min} characters`,
95
- soft
96
- );
97
- }
98
- }
99
- if (Array.isArray(value)) {
100
- if (value.length < min) {
101
- return createError(
102
- context,
103
- "MIN_ITEMS",
104
- message || `Must have at least ${min} items`,
105
- soft
106
- );
107
- }
108
- }
109
- return null;
110
- },
111
- max: (value, params, context) => {
112
- const max = params?.value;
113
- const soft = params?.soft;
114
- const message = params?.message;
115
- if (value === void 0 || value === null) return null;
116
- if (typeof value === "number") {
117
- if (value > max) {
118
- return createError(
119
- context,
120
- "MAX_VALUE",
121
- message || `Value must be at most ${max}`,
122
- soft
123
- );
124
- }
125
- }
126
- if (typeof value === "string") {
127
- if (value.length > max) {
128
- return createError(
129
- context,
130
- "MAX_LENGTH",
131
- message || `Must be at most ${max} characters`,
132
- soft
133
- );
134
- }
135
- }
136
- if (Array.isArray(value)) {
137
- if (value.length > max) {
138
- return createError(
139
- context,
140
- "MAX_ITEMS",
141
- message || `Must have at most ${max} items`,
142
- soft
143
- );
144
- }
145
- }
146
- return null;
147
- },
148
- email: (value, params, context) => {
149
- if (value === void 0 || value === null || value === "") return null;
150
- const soft = params?.soft;
151
- const message = params?.message;
152
- if (typeof value !== "string") {
153
- return createError(context, "INVALID_EMAIL", "Email must be a string", soft);
154
- }
155
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
156
- if (!emailRegex.test(value)) {
157
- return createError(
158
- context,
159
- "INVALID_EMAIL",
160
- message || "Invalid email address",
161
- soft
162
- );
163
- }
164
- return null;
165
- },
166
- pattern: (value, params, context) => {
167
- if (value === void 0 || value === null || value === "") return null;
168
- const pattern = params?.pattern;
169
- const soft = params?.soft;
170
- const message = params?.message;
171
- if (typeof value !== "string") return null;
172
- const regex = new RegExp(pattern);
173
- if (!regex.test(value)) {
174
- return createError(
175
- context,
176
- "PATTERN_MISMATCH",
177
- message || `Value does not match required pattern`,
178
- soft
179
- );
180
- }
181
- return null;
182
- },
183
- url: (value, params, context) => {
184
- if (value === void 0 || value === null || value === "") return null;
185
- const soft = params?.soft;
186
- const message = params?.message;
187
- if (typeof value !== "string") {
188
- return createError(context, "INVALID_URL", "URL must be a string", soft);
189
- }
190
- try {
191
- new URL(value);
192
- return null;
193
- } catch {
194
- return createError(
195
- context,
196
- "INVALID_URL",
197
- message || "Invalid URL format",
198
- soft
199
- );
200
- }
201
- },
202
- ipAddress: (value, params, context) => {
203
- if (value === void 0 || value === null || value === "") return null;
204
- const soft = params?.soft;
205
- const message = params?.message;
206
- if (typeof value !== "string") {
207
- return createError(context, "INVALID_IP", "IP address must be a string", soft);
208
- }
209
- const ipv4Pattern = /^((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]?)$/;
210
- if (!ipv4Pattern.test(value)) {
211
- return createError(
212
- context,
213
- "INVALID_IP",
214
- message || "Invalid IP address format",
215
- soft
216
- );
217
- }
218
- return null;
219
- },
220
- enum: (value, params, context) => {
221
- if (value === void 0 || value === null) return null;
222
- const values = params?.values;
223
- const soft = params?.soft;
224
- const message = params?.message;
225
- if (!values.includes(value)) {
226
- return createError(
227
- context,
228
- "INVALID_ENUM",
229
- message || `Value must be one of: ${values.join(", ")}`,
230
- soft
231
- );
232
- }
233
- return null;
234
- },
235
- custom: (value, params, context) => {
236
- const validate2 = params?.validate;
237
- const soft = params?.soft;
238
- const message = params?.message;
239
- if (!validate2) return null;
240
- const result = validate2(value, context);
241
- if (typeof result === "boolean") {
242
- if (!result) {
243
- return createError(
244
- context,
245
- "CUSTOM_VALIDATION",
246
- message || "Validation failed",
247
- soft
248
- );
249
- }
250
- return null;
251
- }
252
- if (!result.valid) {
253
- return createError(
254
- context,
255
- "CUSTOM_VALIDATION",
256
- result.message || message || "Validation failed",
257
- soft
258
- );
259
- }
260
- return null;
261
- },
262
- // Enterprise patterns - matches field against another field
263
- matches: (value, params, context) => {
264
- if (value === void 0 || value === null) return null;
265
- const otherField = params?.field;
266
- const soft = params?.soft;
267
- const message = params?.message;
268
- const root = context.root;
269
- const otherValue = root[otherField];
270
- if (value !== otherValue) {
271
- return createError(
272
- context,
273
- "FIELD_MISMATCH",
274
- message || `Must match ${otherField}`,
275
- soft
276
- );
277
- }
278
- return null;
279
- },
280
- // Integer validation
281
- integer: (value, params, context) => {
282
- if (value === void 0 || value === null) return null;
283
- const soft = params?.soft;
284
- const message = params?.message;
285
- if (typeof value !== "number" || !Number.isInteger(value)) {
286
- return createError(
287
- context,
288
- "NOT_INTEGER",
289
- message || "Value must be an integer",
290
- soft
291
- );
292
- }
293
- return null;
294
- },
295
- // Positive number validation
296
- positive: (value, params, context) => {
297
- if (value === void 0 || value === null) return null;
298
- const soft = params?.soft;
299
- const message = params?.message;
300
- if (typeof value === "number" && value <= 0) {
301
- return createError(
302
- context,
303
- "NOT_POSITIVE",
304
- message || "Value must be positive",
305
- soft
306
- );
307
- }
308
- return null;
309
- },
310
- // Negative number validation
311
- negative: (value, params, context) => {
312
- if (value === void 0 || value === null) return null;
313
- const soft = params?.soft;
314
- const message = params?.message;
315
- if (typeof value === "number" && value >= 0) {
316
- return createError(
317
- context,
318
- "NOT_NEGATIVE",
319
- message || "Value must be negative",
320
- soft
321
- );
322
- }
323
- return null;
324
- }
325
- };
326
- var customValidators = /* @__PURE__ */ new Map();
327
- function getValidator(name) {
328
- return ruleValidators[name] ?? customValidators.get(name);
329
- }
330
- function getTypeValidator(type) {
331
- return typeValidators[type];
332
- }
333
-
334
- // src/core/engine.ts
335
- function validateField(fieldDef, value, context) {
336
- const errors = [];
337
- const typeValidator = getTypeValidator(fieldDef.type);
338
- if (typeValidator) {
339
- const typeError = typeValidator(value, void 0, context);
340
- if (typeError) {
341
- errors.push(typeError);
342
- return errors;
343
- }
344
- }
345
- if (fieldDef.required) {
346
- const requiredValidator = getValidator("required");
347
- if (requiredValidator) {
348
- const error = requiredValidator(value, void 0, context);
349
- if (error) {
350
- errors.push(error);
351
- return errors;
352
- }
353
- }
354
- } else if (value === void 0 || value === null || value === "") {
355
- return errors;
356
- }
357
- for (const rule of fieldDef.rules) {
358
- const validator = getValidator(rule.type);
359
- if (!validator) {
360
- console.warn(`Unknown validator: ${rule.type}`);
361
- continue;
362
- }
363
- const params = {
364
- ...rule.params,
365
- soft: rule.soft,
366
- message: rule.message
367
- };
368
- const error = validator(value, params, context);
369
- if (error) {
370
- errors.push(error);
371
- }
372
- }
373
- if (fieldDef.type === "object" && fieldDef.schema && value !== null && value !== void 0) {
374
- const nestedResult = validateSchema(fieldDef.schema, value, context.path, context.root);
375
- errors.push(...nestedResult.hardErrors, ...nestedResult.softErrors);
376
- }
377
- if (fieldDef.type === "array" && fieldDef.items && Array.isArray(value)) {
378
- for (let i = 0; i < value.length; i++) {
379
- const itemContext = {
380
- path: `${context.path}[${i}]`,
381
- root: context.root,
382
- parent: value
383
- };
384
- const itemErrors = validateField(fieldDef.items, value[i], itemContext);
385
- errors.push(...itemErrors);
386
- }
387
- }
388
- return errors;
389
- }
390
- function validateSchema(schema, data, basePath = "", root) {
391
- const hardErrors = [];
392
- const softErrors = [];
393
- const rootData = root ?? data;
394
- for (const [fieldName, fieldDef] of Object.entries(schema.fields)) {
395
- const value = data[fieldName];
396
- const path = basePath ? `${basePath}.${fieldName}` : fieldName;
397
- const context = {
398
- path,
399
- root: rootData,
400
- parent: data
401
- };
402
- const errors = validateField(fieldDef, value, context);
403
- for (const error of errors) {
404
- if (error.severity === "soft") {
405
- softErrors.push(error);
406
- } else {
407
- hardErrors.push(error);
408
- }
409
- }
410
- }
411
- return {
412
- valid: hardErrors.length === 0,
413
- hardErrors,
414
- softErrors
415
- };
416
- }
417
- function validate(schema, data) {
418
- return validateSchema(schema, data);
419
- }
3
+ var chunkBJLVOIAP_js = require('../../chunk-BJLVOIAP.js');
4
+ require('../../chunk-BNIB23NQ.js');
5
+ require('../../chunk-2JYFKT3R.js');
6
+ require('../../chunk-CQYXR2LZ.js');
7
+ require('../../chunk-3FANCMEF.js');
8
+ require('../../chunk-FZ7K2PC7.js');
9
+ require('../../chunk-ASKTY6EG.js');
10
+ require('../../chunk-NUW55QTO.js');
420
11
 
421
12
  // src/adapters/frontend/form.ts
422
13
  function createForm(schema, options = {}) {
@@ -546,7 +137,7 @@ var FormController = class {
546
137
  * Validate a single field
547
138
  */
548
139
  validateField(field) {
549
- const result = validate(this.schema.definition, this.state.values);
140
+ const result = chunkBJLVOIAP_js.validate(this.schema.definition, this.state.values);
550
141
  const fieldHardErrors = result.hardErrors.filter((e) => e.field === field);
551
142
  const fieldSoftErrors = result.softErrors.filter((e) => e.field === field);
552
143
  this.state.errors[field] = fieldHardErrors;
@@ -563,7 +154,7 @@ var FormController = class {
563
154
  * Validate entire form
564
155
  */
565
156
  validate() {
566
- const result = validate(this.schema.definition, this.state.values);
157
+ const result = chunkBJLVOIAP_js.validate(this.schema.definition, this.state.values);
567
158
  const errors = {};
568
159
  const warnings = {};
569
160
  for (const error of result.hardErrors) {
@@ -881,5 +472,3 @@ exports.hasFieldWarning = hasFieldWarning;
881
472
  exports.parseApiErrors = parseApiErrors;
882
473
  exports.toErrorMap = toErrorMap;
883
474
  exports.toWarningsMap = toWarningsMap;
884
- //# sourceMappingURL=index.js.map
885
- //# sourceMappingURL=index.js.map