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,416 +1,12 @@
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
- enum: (value, params, context) => {
201
- if (value === void 0 || value === null) return null;
202
- const values = params?.values;
203
- const soft = params?.soft;
204
- const message = params?.message;
205
- if (!values.includes(value)) {
206
- return createError(
207
- context,
208
- "INVALID_ENUM",
209
- message || `Value must be one of: ${values.join(", ")}`,
210
- soft
211
- );
212
- }
213
- return null;
214
- },
215
- custom: (value, params, context) => {
216
- const validate2 = params?.validate;
217
- const soft = params?.soft;
218
- const message = params?.message;
219
- if (!validate2) return null;
220
- const result = validate2(value, context);
221
- if (typeof result === "boolean") {
222
- if (!result) {
223
- return createError(
224
- context,
225
- "CUSTOM_VALIDATION",
226
- message || "Validation failed",
227
- soft
228
- );
229
- }
230
- return null;
231
- }
232
- if (!result.valid) {
233
- return createError(
234
- context,
235
- "CUSTOM_VALIDATION",
236
- result.message || message || "Validation failed",
237
- soft
238
- );
239
- }
240
- return null;
241
- },
242
- // Enterprise patterns - matches field against another field
243
- matches: (value, params, context) => {
244
- if (value === void 0 || value === null) return null;
245
- const otherField = params?.field;
246
- const soft = params?.soft;
247
- const message = params?.message;
248
- const root = context.root;
249
- const otherValue = root[otherField];
250
- if (value !== otherValue) {
251
- return createError(
252
- context,
253
- "FIELD_MISMATCH",
254
- message || `Must match ${otherField}`,
255
- soft
256
- );
257
- }
258
- return null;
259
- },
260
- // Integer validation
261
- integer: (value, params, context) => {
262
- if (value === void 0 || value === null) return null;
263
- const soft = params?.soft;
264
- const message = params?.message;
265
- if (typeof value !== "number" || !Number.isInteger(value)) {
266
- return createError(
267
- context,
268
- "NOT_INTEGER",
269
- message || "Value must be an integer",
270
- soft
271
- );
272
- }
273
- return null;
274
- },
275
- // Positive number validation
276
- positive: (value, params, context) => {
277
- if (value === void 0 || value === null) return null;
278
- const soft = params?.soft;
279
- const message = params?.message;
280
- if (typeof value === "number" && value <= 0) {
281
- return createError(
282
- context,
283
- "NOT_POSITIVE",
284
- message || "Value must be positive",
285
- soft
286
- );
287
- }
288
- return null;
289
- },
290
- // Negative number validation
291
- negative: (value, params, context) => {
292
- if (value === void 0 || value === null) return null;
293
- const soft = params?.soft;
294
- const message = params?.message;
295
- if (typeof value === "number" && value >= 0) {
296
- return createError(
297
- context,
298
- "NOT_NEGATIVE",
299
- message || "Value must be negative",
300
- soft
301
- );
302
- }
303
- return null;
304
- }
305
- };
306
- var customValidators = /* @__PURE__ */ new Map();
307
- function getValidator(name) {
308
- return ruleValidators[name] ?? customValidators.get(name);
309
- }
310
- function getTypeValidator(type) {
311
- return typeValidators[type];
312
- }
313
-
314
- // src/core/engine.ts
315
- function validateField(fieldDef, value, context) {
316
- const errors = [];
317
- const typeValidator = getTypeValidator(fieldDef.type);
318
- if (typeValidator) {
319
- const typeError = typeValidator(value, void 0, context);
320
- if (typeError) {
321
- errors.push(typeError);
322
- return errors;
323
- }
324
- }
325
- if (fieldDef.required) {
326
- const requiredValidator = getValidator("required");
327
- if (requiredValidator) {
328
- const error2 = requiredValidator(value, void 0, context);
329
- if (error2) {
330
- errors.push(error2);
331
- return errors;
332
- }
333
- }
334
- } else if (value === void 0 || value === null || value === "") {
335
- return errors;
336
- }
337
- for (const rule of fieldDef.rules) {
338
- const validator = getValidator(rule.type);
339
- if (!validator) {
340
- console.warn(`Unknown validator: ${rule.type}`);
341
- continue;
342
- }
343
- const params = {
344
- ...rule.params,
345
- soft: rule.soft,
346
- message: rule.message
347
- };
348
- const error2 = validator(value, params, context);
349
- if (error2) {
350
- errors.push(error2);
351
- }
352
- }
353
- if (fieldDef.type === "object" && fieldDef.schema && value !== null && value !== void 0) {
354
- const nestedResult = validateSchema(fieldDef.schema, value, context.path, context.root);
355
- errors.push(...nestedResult.hardErrors, ...nestedResult.softErrors);
356
- }
357
- if (fieldDef.type === "array" && fieldDef.items && Array.isArray(value)) {
358
- for (let i = 0; i < value.length; i++) {
359
- const itemContext = {
360
- path: `${context.path}[${i}]`,
361
- root: context.root,
362
- parent: value
363
- };
364
- const itemErrors = validateField(fieldDef.items, value[i], itemContext);
365
- errors.push(...itemErrors);
366
- }
367
- }
368
- return errors;
369
- }
370
- function validateSchema(schema, data, basePath = "", root) {
371
- const hardErrors = [];
372
- const softErrors = [];
373
- const rootData = root ?? data;
374
- for (const [fieldName, fieldDef] of Object.entries(schema.fields)) {
375
- const value = data[fieldName];
376
- const path = basePath ? `${basePath}.${fieldName}` : fieldName;
377
- const context = {
378
- path,
379
- root: rootData,
380
- parent: data
381
- };
382
- const errors = validateField(fieldDef, value, context);
383
- for (const error2 of errors) {
384
- if (error2.severity === "soft") {
385
- softErrors.push(error2);
386
- } else {
387
- hardErrors.push(error2);
388
- }
389
- }
390
- }
391
- return {
392
- valid: hardErrors.length === 0,
393
- hardErrors,
394
- softErrors
395
- };
396
- }
397
- function validate(schema, data) {
398
- return validateSchema(schema, data);
399
- }
400
-
401
- // src/types/index.ts
402
- function toEnterpriseResponse(result, data) {
403
- return {
404
- status: result.valid ? "success" : "validation_error",
405
- data: result.valid ? data : void 0,
406
- errors: [...result.hardErrors, ...result.softErrors],
407
- msg: result.valid ? "Validation successful" : "Validation failed",
408
- validation: {
409
- hard_validations: result.hardErrors,
410
- soft_validations: result.softErrors
411
- }
412
- };
413
- }
1
+ import { toEnterpriseResponse } from '../../chunk-TTK77YBI.mjs';
2
+ import { validate } from '../../chunk-3TS35CVJ.mjs';
3
+ import '../../chunk-KHHJD6QK.mjs';
4
+ import '../../chunk-XC4DKEXP.mjs';
5
+ import '../../chunk-FRBZHN4K.mjs';
6
+ import '../../chunk-VWP24NYS.mjs';
7
+ import '../../chunk-ELL7U7IC.mjs';
8
+ import '../../chunk-XGTUU27F.mjs';
9
+ import '../../chunk-FKDWSZIV.mjs';
414
10
 
415
11
  // src/adapters/backend/express.ts
416
12
  function defaultErrorHandler(result, _req, res) {
@@ -602,5 +198,3 @@ function error(message, statusCode = 400) {
602
198
  }
603
199
 
604
200
  export { createHandler, error, sendValidationError, sendValidationSuccess, success, validateBody, validateInput, validateParams, validateQuery, validateRequest, withValidation };
605
- //# sourceMappingURL=index.mjs.map
606
- //# sourceMappingURL=index.mjs.map
@@ -1,4 +1,5 @@
1
- import { d as SchemaBuilder, j as ValidationError, V as ValidationResult, I as InferInput } from '../../schema-BwQtngae.mjs';
1
+ import { S as SchemaBuilder, I as InferInput } from '../../schema-DYU1zGVm.mjs';
2
+ import { a as ValidationError, b as ValidationResult } from '../../index-BQR7OrY7.mjs';
2
3
 
3
4
  /**
4
5
  * Headless form utilities for frontend frameworks
@@ -1,4 +1,5 @@
1
- import { d as SchemaBuilder, j as ValidationError, V as ValidationResult, I as InferInput } from '../../schema-BwQtngae.js';
1
+ import { S as SchemaBuilder, I as InferInput } from '../../schema-CpAjXgEF.js';
2
+ import { a as ValidationError, b as ValidationResult } from '../../index-BQR7OrY7.js';
2
3
 
3
4
  /**
4
5
  * Headless form utilities for frontend frameworks