unischema 1.0.1 → 1.2.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 +780 -228
  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-5A4ITJVD.mjs +124 -0
  11. package/dist/chunk-66RFUBVU.js +131 -0
  12. package/dist/chunk-75YSYC4K.mjs +85 -0
  13. package/dist/chunk-76BBWQDH.js +90 -0
  14. package/dist/chunk-7XES4A3M.mjs +237 -0
  15. package/dist/chunk-BVRXGZLS.js +17 -0
  16. package/dist/chunk-COMVAVFU.mjs +335 -0
  17. package/dist/chunk-DT2TQZU7.js +796 -0
  18. package/dist/chunk-FPCCH55A.js +103 -0
  19. package/dist/chunk-IUXRLMET.js +206 -0
  20. package/dist/chunk-JEW6U6CB.js +353 -0
  21. package/dist/chunk-KZCV5IW4.mjs +97 -0
  22. package/dist/chunk-KZZ7NVU3.mjs +41 -0
  23. package/dist/chunk-MFEBMQAU.mjs +779 -0
  24. package/dist/chunk-OIYG5D2I.js +50 -0
  25. package/dist/chunk-RW6HDA5H.mjs +194 -0
  26. package/dist/chunk-TTK77YBI.mjs +15 -0
  27. package/dist/chunk-TXT36BCE.js +248 -0
  28. package/dist/index-C17xs-fU.d.mts +140 -0
  29. package/dist/index-C17xs-fU.d.ts +140 -0
  30. package/dist/index.d.mts +26 -7
  31. package/dist/index.d.ts +26 -7
  32. package/dist/index.js +769 -499
  33. package/dist/index.mjs +695 -487
  34. package/dist/{schema-D9DGC9E_.d.ts → schema-DYE8Wz8X.d.mts} +264 -79
  35. package/dist/{schema-D9DGC9E_.d.mts → schema-Dtp-joeT.d.ts} +264 -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 +82 -5
  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-D9DGC9E_.mjs';
2
+ import { S as SchemaBuilder, I as InferInput } from '../../schema-DYE8Wz8X.mjs';
3
+ import { b as ValidationResult, E as EnterpriseValidationResponse } from '../../index-C17xs-fU.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-D9DGC9E_.js';
2
+ import { S as SchemaBuilder, I as InferInput } from '../../schema-Dtp-joeT.js';
3
+ import { b as ValidationResult, E as EnterpriseValidationResponse } from '../../index-C17xs-fU.js';
3
4
 
4
5
  /**
5
6
  * Express.js middleware adapter for FormSchema validation
@@ -1,440 +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
- 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 error2 = requiredValidator(value, void 0, context);
349
- if (error2) {
350
- errors.push(error2);
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 error2 = validator(value, params, context);
369
- if (error2) {
370
- errors.push(error2);
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 error2 of errors) {
404
- if (error2.severity === "soft") {
405
- softErrors.push(error2);
406
- } else {
407
- hardErrors.push(error2);
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
- }
420
-
421
- // src/types/index.ts
422
- function toEnterpriseResponse(result, data) {
423
- return {
424
- status: result.valid ? "success" : "validation_error",
425
- data: result.valid ? data : void 0,
426
- errors: [...result.hardErrors, ...result.softErrors],
427
- msg: result.valid ? "Validation successful" : "Validation failed",
428
- validation: {
429
- hard_validations: result.hardErrors,
430
- soft_validations: result.softErrors
431
- }
432
- };
433
- }
3
+ var chunkBVRXGZLS_js = require('../../chunk-BVRXGZLS.js');
4
+ var chunkDT2TQZU7_js = require('../../chunk-DT2TQZU7.js');
5
+ require('../../chunk-76BBWQDH.js');
6
+ require('../../chunk-FPCCH55A.js');
7
+ require('../../chunk-JEW6U6CB.js');
8
+ require('../../chunk-IUXRLMET.js');
9
+ require('../../chunk-TXT36BCE.js');
10
+ require('../../chunk-66RFUBVU.js');
11
+ require('../../chunk-OIYG5D2I.js');
434
12
 
435
13
  // src/adapters/backend/express.ts
436
14
  function defaultErrorHandler(result, _req, res) {
437
- const response = toEnterpriseResponse(result);
15
+ const response = chunkBVRXGZLS_js.toEnterpriseResponse(result);
438
16
  res.status(400).json(response);
439
17
  }
440
18
  function validateRequest(schema, options = {}) {
@@ -460,7 +38,7 @@ function validateRequest(schema, options = {}) {
460
38
  if (transform) {
461
39
  data = transform(data);
462
40
  }
463
- const result = validate(schema.definition, data);
41
+ const result = chunkDT2TQZU7_js.validate(schema.definition, data);
464
42
  req.validationResult = result;
465
43
  if (!result.valid) {
466
44
  onError(result, req, res);
@@ -502,7 +80,7 @@ function sendValidationSuccess(res, data, message = "Success") {
502
80
  });
503
81
  }
504
82
  function sendValidationError(res, result, statusCode = 400) {
505
- res.status(statusCode).json(toEnterpriseResponse(result));
83
+ res.status(statusCode).json(chunkBVRXGZLS_js.toEnterpriseResponse(result));
506
84
  }
507
85
 
508
86
  // src/adapters/backend/handler.ts
@@ -542,17 +120,17 @@ function createHandler(schema, handler, options = {}) {
542
120
  };
543
121
  }
544
122
  }
545
- const validation = validate(schema.definition, data);
123
+ const validation = chunkDT2TQZU7_js.validate(schema.definition, data);
546
124
  if (!validation.valid) {
547
125
  return {
548
126
  statusCode: 400,
549
- body: toEnterpriseResponse(validation)
127
+ body: chunkBVRXGZLS_js.toEnterpriseResponse(validation)
550
128
  };
551
129
  }
552
130
  if (!allowSoftErrors && validation.softErrors.length > 0) {
553
131
  return {
554
132
  statusCode: 400,
555
- body: toEnterpriseResponse(validation)
133
+ body: chunkBVRXGZLS_js.toEnterpriseResponse(validation)
556
134
  };
557
135
  }
558
136
  try {
@@ -598,12 +176,12 @@ function createHandler(schema, handler, options = {}) {
598
176
  }
599
177
  function validateInput(schema, input) {
600
178
  const data = typeof input === "string" ? JSON.parse(input) : input;
601
- const result = validate(schema.definition, data);
179
+ const result = chunkDT2TQZU7_js.validate(schema.definition, data);
602
180
  return {
603
181
  valid: result.valid,
604
182
  data: result.valid ? data : null,
605
183
  result,
606
- response: toEnterpriseResponse(result, result.valid ? data : void 0)
184
+ response: chunkBVRXGZLS_js.toEnterpriseResponse(result, result.valid ? data : void 0)
607
185
  };
608
186
  }
609
187
  function success(data, statusCode = 200) {
@@ -632,5 +210,3 @@ exports.validateParams = validateParams;
632
210
  exports.validateQuery = validateQuery;
633
211
  exports.validateRequest = validateRequest;
634
212
  exports.withValidation = withValidation;
635
- //# sourceMappingURL=index.js.map
636
- //# sourceMappingURL=index.js.map