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,5 +1,6 @@
1
1
  import { Request, Response, RequestHandler, NextFunction } from 'express';
2
- import { d as SchemaBuilder, V as ValidationResult, I as InferInput, q as EnterpriseValidationResponse } from '../../schema-BwQtngae.mjs';
2
+ import { S as SchemaBuilder, I as InferInput } from '../../schema-DYU1zGVm.mjs';
3
+ import { b as ValidationResult, E as EnterpriseValidationResponse } from '../../index-BQR7OrY7.mjs';
3
4
 
4
5
  /**
5
6
  * Express.js middleware adapter for FormSchema validation
@@ -1,5 +1,6 @@
1
1
  import { Request, Response, RequestHandler, NextFunction } from 'express';
2
- import { d as SchemaBuilder, V as ValidationResult, I as InferInput, q as EnterpriseValidationResponse } from '../../schema-BwQtngae.js';
2
+ import { S as SchemaBuilder, I as InferInput } from '../../schema-CpAjXgEF.js';
3
+ import { b as ValidationResult, E as EnterpriseValidationResponse } from '../../index-BQR7OrY7.js';
3
4
 
4
5
  /**
5
6
  * Express.js middleware adapter for FormSchema validation
@@ -1,422 +1,18 @@
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 error2 = requiredValidator(value, void 0, context);
331
- if (error2) {
332
- errors.push(error2);
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 error2 = validator(value, params, context);
351
- if (error2) {
352
- errors.push(error2);
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 error2 of errors) {
386
- if (error2.severity === "soft") {
387
- softErrors.push(error2);
388
- } else {
389
- hardErrors.push(error2);
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
- }
402
-
403
- // src/types/index.ts
404
- function toEnterpriseResponse(result, data) {
405
- return {
406
- status: result.valid ? "success" : "validation_error",
407
- data: result.valid ? data : void 0,
408
- errors: [...result.hardErrors, ...result.softErrors],
409
- msg: result.valid ? "Validation successful" : "Validation failed",
410
- validation: {
411
- hard_validations: result.hardErrors,
412
- soft_validations: result.softErrors
413
- }
414
- };
415
- }
3
+ var chunkBVRXGZLS_js = require('../../chunk-BVRXGZLS.js');
4
+ var chunkBJLVOIAP_js = require('../../chunk-BJLVOIAP.js');
5
+ require('../../chunk-BNIB23NQ.js');
6
+ require('../../chunk-2JYFKT3R.js');
7
+ require('../../chunk-CQYXR2LZ.js');
8
+ require('../../chunk-3FANCMEF.js');
9
+ require('../../chunk-FZ7K2PC7.js');
10
+ require('../../chunk-ASKTY6EG.js');
11
+ require('../../chunk-NUW55QTO.js');
416
12
 
417
13
  // src/adapters/backend/express.ts
418
14
  function defaultErrorHandler(result, _req, res) {
419
- const response = toEnterpriseResponse(result);
15
+ const response = chunkBVRXGZLS_js.toEnterpriseResponse(result);
420
16
  res.status(400).json(response);
421
17
  }
422
18
  function validateRequest(schema, options = {}) {
@@ -442,7 +38,7 @@ function validateRequest(schema, options = {}) {
442
38
  if (transform) {
443
39
  data = transform(data);
444
40
  }
445
- const result = validate(schema.definition, data);
41
+ const result = chunkBJLVOIAP_js.validate(schema.definition, data);
446
42
  req.validationResult = result;
447
43
  if (!result.valid) {
448
44
  onError(result, req, res);
@@ -484,7 +80,7 @@ function sendValidationSuccess(res, data, message = "Success") {
484
80
  });
485
81
  }
486
82
  function sendValidationError(res, result, statusCode = 400) {
487
- res.status(statusCode).json(toEnterpriseResponse(result));
83
+ res.status(statusCode).json(chunkBVRXGZLS_js.toEnterpriseResponse(result));
488
84
  }
489
85
 
490
86
  // src/adapters/backend/handler.ts
@@ -524,17 +120,17 @@ function createHandler(schema, handler, options = {}) {
524
120
  };
525
121
  }
526
122
  }
527
- const validation = validate(schema.definition, data);
123
+ const validation = chunkBJLVOIAP_js.validate(schema.definition, data);
528
124
  if (!validation.valid) {
529
125
  return {
530
126
  statusCode: 400,
531
- body: toEnterpriseResponse(validation)
127
+ body: chunkBVRXGZLS_js.toEnterpriseResponse(validation)
532
128
  };
533
129
  }
534
130
  if (!allowSoftErrors && validation.softErrors.length > 0) {
535
131
  return {
536
132
  statusCode: 400,
537
- body: toEnterpriseResponse(validation)
133
+ body: chunkBVRXGZLS_js.toEnterpriseResponse(validation)
538
134
  };
539
135
  }
540
136
  try {
@@ -580,12 +176,12 @@ function createHandler(schema, handler, options = {}) {
580
176
  }
581
177
  function validateInput(schema, input) {
582
178
  const data = typeof input === "string" ? JSON.parse(input) : input;
583
- const result = validate(schema.definition, data);
179
+ const result = chunkBJLVOIAP_js.validate(schema.definition, data);
584
180
  return {
585
181
  valid: result.valid,
586
182
  data: result.valid ? data : null,
587
183
  result,
588
- response: toEnterpriseResponse(result, result.valid ? data : void 0)
184
+ response: chunkBVRXGZLS_js.toEnterpriseResponse(result, result.valid ? data : void 0)
589
185
  };
590
186
  }
591
187
  function success(data, statusCode = 200) {
@@ -614,5 +210,3 @@ exports.validateParams = validateParams;
614
210
  exports.validateQuery = validateQuery;
615
211
  exports.validateRequest = validateRequest;
616
212
  exports.withValidation = withValidation;
617
- //# sourceMappingURL=index.js.map
618
- //# sourceMappingURL=index.js.map