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