unischema 1.0.0 → 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 -230
  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 -423
  5. package/dist/adapters/backend/index.mjs +9 -415
  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 -403
  9. package/dist/adapters/frontend/index.mjs +8 -401
  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 +537 -476
  33. package/dist/index.mjs +486 -464
  34. package/dist/{schema-BwQtngae.d.mts → schema-CpAjXgEF.d.ts} +186 -79
  35. package/dist/{schema-BwQtngae.d.ts → schema-DYU1zGVm.d.mts} +186 -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,404 +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
- enum: (value, params, context) => {
203
- if (value === void 0 || value === null) return null;
204
- const values = params?.values;
205
- const soft = params?.soft;
206
- const message = params?.message;
207
- if (!values.includes(value)) {
208
- return createError(
209
- context,
210
- "INVALID_ENUM",
211
- message || `Value must be one of: ${values.join(", ")}`,
212
- soft
213
- );
214
- }
215
- return null;
216
- },
217
- custom: (value, params, context) => {
218
- const validate2 = params?.validate;
219
- const soft = params?.soft;
220
- const message = params?.message;
221
- if (!validate2) return null;
222
- const result = validate2(value, context);
223
- if (typeof result === "boolean") {
224
- if (!result) {
225
- return createError(
226
- context,
227
- "CUSTOM_VALIDATION",
228
- message || "Validation failed",
229
- soft
230
- );
231
- }
232
- return null;
233
- }
234
- if (!result.valid) {
235
- return createError(
236
- context,
237
- "CUSTOM_VALIDATION",
238
- result.message || message || "Validation failed",
239
- soft
240
- );
241
- }
242
- return null;
243
- },
244
- // Enterprise patterns - matches field against another field
245
- matches: (value, params, context) => {
246
- if (value === void 0 || value === null) return null;
247
- const otherField = params?.field;
248
- const soft = params?.soft;
249
- const message = params?.message;
250
- const root = context.root;
251
- const otherValue = root[otherField];
252
- if (value !== otherValue) {
253
- return createError(
254
- context,
255
- "FIELD_MISMATCH",
256
- message || `Must match ${otherField}`,
257
- soft
258
- );
259
- }
260
- return null;
261
- },
262
- // Integer validation
263
- integer: (value, params, context) => {
264
- if (value === void 0 || value === null) return null;
265
- const soft = params?.soft;
266
- const message = params?.message;
267
- if (typeof value !== "number" || !Number.isInteger(value)) {
268
- return createError(
269
- context,
270
- "NOT_INTEGER",
271
- message || "Value must be an integer",
272
- soft
273
- );
274
- }
275
- return null;
276
- },
277
- // Positive number validation
278
- positive: (value, params, context) => {
279
- if (value === void 0 || value === null) return null;
280
- const soft = params?.soft;
281
- const message = params?.message;
282
- if (typeof value === "number" && value <= 0) {
283
- return createError(
284
- context,
285
- "NOT_POSITIVE",
286
- message || "Value must be positive",
287
- soft
288
- );
289
- }
290
- return null;
291
- },
292
- // Negative number validation
293
- negative: (value, params, context) => {
294
- if (value === void 0 || value === null) return null;
295
- const soft = params?.soft;
296
- const message = params?.message;
297
- if (typeof value === "number" && value >= 0) {
298
- return createError(
299
- context,
300
- "NOT_NEGATIVE",
301
- message || "Value must be negative",
302
- soft
303
- );
304
- }
305
- return null;
306
- }
307
- };
308
- var customValidators = /* @__PURE__ */ new Map();
309
- function getValidator(name) {
310
- return ruleValidators[name] ?? customValidators.get(name);
311
- }
312
- function getTypeValidator(type) {
313
- return typeValidators[type];
314
- }
315
-
316
- // src/core/engine.ts
317
- function validateField(fieldDef, value, context) {
318
- const errors = [];
319
- const typeValidator = getTypeValidator(fieldDef.type);
320
- if (typeValidator) {
321
- const typeError = typeValidator(value, void 0, context);
322
- if (typeError) {
323
- errors.push(typeError);
324
- return errors;
325
- }
326
- }
327
- if (fieldDef.required) {
328
- const requiredValidator = getValidator("required");
329
- if (requiredValidator) {
330
- const error = requiredValidator(value, void 0, context);
331
- if (error) {
332
- errors.push(error);
333
- return errors;
334
- }
335
- }
336
- } else if (value === void 0 || value === null || value === "") {
337
- return errors;
338
- }
339
- for (const rule of fieldDef.rules) {
340
- const validator = getValidator(rule.type);
341
- if (!validator) {
342
- console.warn(`Unknown validator: ${rule.type}`);
343
- continue;
344
- }
345
- const params = {
346
- ...rule.params,
347
- soft: rule.soft,
348
- message: rule.message
349
- };
350
- const error = validator(value, params, context);
351
- if (error) {
352
- errors.push(error);
353
- }
354
- }
355
- if (fieldDef.type === "object" && fieldDef.schema && value !== null && value !== void 0) {
356
- const nestedResult = validateSchema(fieldDef.schema, value, context.path, context.root);
357
- errors.push(...nestedResult.hardErrors, ...nestedResult.softErrors);
358
- }
359
- if (fieldDef.type === "array" && fieldDef.items && Array.isArray(value)) {
360
- for (let i = 0; i < value.length; i++) {
361
- const itemContext = {
362
- path: `${context.path}[${i}]`,
363
- root: context.root,
364
- parent: value
365
- };
366
- const itemErrors = validateField(fieldDef.items, value[i], itemContext);
367
- errors.push(...itemErrors);
368
- }
369
- }
370
- return errors;
371
- }
372
- function validateSchema(schema, data, basePath = "", root) {
373
- const hardErrors = [];
374
- const softErrors = [];
375
- const rootData = root ?? data;
376
- for (const [fieldName, fieldDef] of Object.entries(schema.fields)) {
377
- const value = data[fieldName];
378
- const path = basePath ? `${basePath}.${fieldName}` : fieldName;
379
- const context = {
380
- path,
381
- root: rootData,
382
- parent: data
383
- };
384
- const errors = validateField(fieldDef, value, context);
385
- for (const error of errors) {
386
- if (error.severity === "soft") {
387
- softErrors.push(error);
388
- } else {
389
- hardErrors.push(error);
390
- }
391
- }
392
- }
393
- return {
394
- valid: hardErrors.length === 0,
395
- hardErrors,
396
- softErrors
397
- };
398
- }
399
- function validate(schema, data) {
400
- return validateSchema(schema, data);
401
- }
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');
402
11
 
403
12
  // src/adapters/frontend/form.ts
404
13
  function createForm(schema, options = {}) {
@@ -528,7 +137,7 @@ var FormController = class {
528
137
  * Validate a single field
529
138
  */
530
139
  validateField(field) {
531
- const result = validate(this.schema.definition, this.state.values);
140
+ const result = chunkBJLVOIAP_js.validate(this.schema.definition, this.state.values);
532
141
  const fieldHardErrors = result.hardErrors.filter((e) => e.field === field);
533
142
  const fieldSoftErrors = result.softErrors.filter((e) => e.field === field);
534
143
  this.state.errors[field] = fieldHardErrors;
@@ -545,7 +154,7 @@ var FormController = class {
545
154
  * Validate entire form
546
155
  */
547
156
  validate() {
548
- const result = validate(this.schema.definition, this.state.values);
157
+ const result = chunkBJLVOIAP_js.validate(this.schema.definition, this.state.values);
549
158
  const errors = {};
550
159
  const warnings = {};
551
160
  for (const error of result.hardErrors) {
@@ -863,5 +472,3 @@ exports.hasFieldWarning = hasFieldWarning;
863
472
  exports.parseApiErrors = parseApiErrors;
864
473
  exports.toErrorMap = toErrorMap;
865
474
  exports.toWarningsMap = toWarningsMap;
866
- //# sourceMappingURL=index.js.map
867
- //# sourceMappingURL=index.js.map