@palmares/schemas 0.0.1 → 0.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 (174) hide show
  1. package/.turbo/turbo-build$colon$watch.log +12 -410
  2. package/CHANGELOG.md +17 -0
  3. package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +22 -0
  4. package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +156 -0
  5. package/__tests__/.drizzle/migrations/meta/_journal.json +13 -0
  6. package/__tests__/.drizzle/schema.ts +35 -0
  7. package/__tests__/drizzle.config.ts +11 -0
  8. package/__tests__/eslint.config.js +10 -0
  9. package/__tests__/manage.ts +5 -0
  10. package/__tests__/node_modules/.bin/drizzle-kit +17 -0
  11. package/__tests__/node_modules/.bin/esbuild +14 -0
  12. package/__tests__/node_modules/.bin/tsc +17 -0
  13. package/__tests__/node_modules/.bin/tsserver +17 -0
  14. package/__tests__/node_modules/.bin/tsx +17 -0
  15. package/__tests__/package.json +36 -0
  16. package/__tests__/sqlite.db +0 -0
  17. package/__tests__/src/core/array.test.ts +130 -0
  18. package/__tests__/src/core/boolean.test.ts +66 -0
  19. package/__tests__/src/core/datetime.test.ts +102 -0
  20. package/__tests__/src/core/index.ts +35 -0
  21. package/__tests__/src/core/model.test.ts +260 -0
  22. package/__tests__/src/core/models.ts +50 -0
  23. package/__tests__/src/core/numbers.test.ts +177 -0
  24. package/__tests__/src/core/object.test.ts +198 -0
  25. package/__tests__/src/core/string.test.ts +222 -0
  26. package/__tests__/src/core/test.test.ts +59 -0
  27. package/__tests__/src/core/types.test.ts +97 -0
  28. package/__tests__/src/core/union.test.ts +99 -0
  29. package/__tests__/src/settings.ts +71 -0
  30. package/__tests__/tsconfig.json +11 -0
  31. package/dist/cjs/src/adapter/fields/index.js +2 -2
  32. package/dist/cjs/src/adapter/fields/object.js +9 -0
  33. package/dist/cjs/src/adapter/index.js +1 -0
  34. package/dist/cjs/src/constants.js +1 -7
  35. package/dist/cjs/src/domain.js +146 -1
  36. package/dist/cjs/src/index.js +69 -74
  37. package/dist/cjs/src/model.js +206 -206
  38. package/dist/cjs/src/schema/array.js +185 -58
  39. package/dist/cjs/src/schema/boolean.js +105 -44
  40. package/dist/cjs/src/schema/datetime.js +104 -38
  41. package/dist/cjs/src/schema/number.js +134 -114
  42. package/dist/cjs/src/schema/object.js +106 -43
  43. package/dist/cjs/src/schema/schema.js +123 -75
  44. package/dist/cjs/src/schema/string.js +152 -58
  45. package/dist/cjs/src/schema/union.js +412 -290
  46. package/dist/cjs/src/utils.js +42 -15
  47. package/dist/cjs/src/validators/array.js +6 -1
  48. package/dist/cjs/src/validators/boolean.js +2 -0
  49. package/dist/cjs/src/validators/datetime.js +4 -0
  50. package/dist/cjs/src/validators/number.js +12 -40
  51. package/dist/cjs/src/validators/object.js +1 -0
  52. package/dist/cjs/src/validators/schema.js +5 -1
  53. package/dist/cjs/src/validators/string.js +30 -2
  54. package/dist/cjs/src/validators/union.js +5 -4
  55. package/dist/cjs/src/validators/utils.js +99 -27
  56. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
  57. package/dist/cjs/types/adapter/fields/array.d.ts +2 -2
  58. package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -1
  59. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -1
  60. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -1
  61. package/dist/cjs/types/adapter/fields/index.d.ts +2 -2
  62. package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -1
  63. package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -1
  64. package/dist/cjs/types/adapter/fields/object.d.ts +2 -1
  65. package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -1
  66. package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -1
  67. package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -1
  68. package/dist/cjs/types/adapter/index.d.ts +1 -0
  69. package/dist/cjs/types/adapter/index.d.ts.map +1 -1
  70. package/dist/cjs/types/adapter/types.d.ts +28 -18
  71. package/dist/cjs/types/adapter/types.d.ts.map +1 -1
  72. package/dist/cjs/types/constants.d.ts +0 -1
  73. package/dist/cjs/types/constants.d.ts.map +1 -1
  74. package/dist/cjs/types/domain.d.ts +5 -4
  75. package/dist/cjs/types/domain.d.ts.map +1 -1
  76. package/dist/cjs/types/index.d.ts +78 -55
  77. package/dist/cjs/types/index.d.ts.map +1 -1
  78. package/dist/cjs/types/model.d.ts +17 -17
  79. package/dist/cjs/types/model.d.ts.map +1 -1
  80. package/dist/cjs/types/schema/array.d.ts +168 -47
  81. package/dist/cjs/types/schema/array.d.ts.map +1 -1
  82. package/dist/cjs/types/schema/boolean.d.ts +103 -44
  83. package/dist/cjs/types/schema/boolean.d.ts.map +1 -1
  84. package/dist/cjs/types/schema/datetime.d.ts +90 -30
  85. package/dist/cjs/types/schema/datetime.d.ts.map +1 -1
  86. package/dist/cjs/types/schema/number.d.ts +133 -125
  87. package/dist/cjs/types/schema/number.d.ts.map +1 -1
  88. package/dist/cjs/types/schema/object.d.ts +104 -35
  89. package/dist/cjs/types/schema/object.d.ts.map +1 -1
  90. package/dist/cjs/types/schema/schema.d.ts +62 -44
  91. package/dist/cjs/types/schema/schema.d.ts.map +1 -1
  92. package/dist/cjs/types/schema/string.d.ts +152 -65
  93. package/dist/cjs/types/schema/string.d.ts.map +1 -1
  94. package/dist/cjs/types/schema/types.d.ts +11 -2
  95. package/dist/cjs/types/schema/types.d.ts.map +1 -1
  96. package/dist/cjs/types/schema/union.d.ts +133 -40
  97. package/dist/cjs/types/schema/union.d.ts.map +1 -1
  98. package/dist/cjs/types/types.d.ts +35 -0
  99. package/dist/cjs/types/types.d.ts.map +1 -1
  100. package/dist/cjs/types/utils.d.ts +41 -27
  101. package/dist/cjs/types/utils.d.ts.map +1 -1
  102. package/dist/cjs/types/validators/array.d.ts.map +1 -1
  103. package/dist/cjs/types/validators/boolean.d.ts.map +1 -1
  104. package/dist/cjs/types/validators/datetime.d.ts.map +1 -1
  105. package/dist/cjs/types/validators/number.d.ts +5 -6
  106. package/dist/cjs/types/validators/number.d.ts.map +1 -1
  107. package/dist/cjs/types/validators/object.d.ts.map +1 -1
  108. package/dist/cjs/types/validators/schema.d.ts +2 -2
  109. package/dist/cjs/types/validators/schema.d.ts.map +1 -1
  110. package/dist/cjs/types/validators/string.d.ts +9 -9
  111. package/dist/cjs/types/validators/string.d.ts.map +1 -1
  112. package/dist/cjs/types/validators/utils.d.ts +44 -27
  113. package/dist/cjs/types/validators/utils.d.ts.map +1 -1
  114. package/dist/esm/src/adapter/fields/index.js +2 -2
  115. package/dist/esm/src/adapter/fields/object.js +6 -0
  116. package/dist/esm/src/adapter/index.js +1 -0
  117. package/dist/esm/src/constants.js +1 -2
  118. package/dist/esm/src/domain.js +11 -1
  119. package/dist/esm/src/index.js +38 -73
  120. package/dist/esm/src/model.js +83 -78
  121. package/dist/esm/src/schema/array.js +136 -54
  122. package/dist/esm/src/schema/boolean.js +98 -44
  123. package/dist/esm/src/schema/datetime.js +91 -38
  124. package/dist/esm/src/schema/number.js +127 -110
  125. package/dist/esm/src/schema/object.js +98 -43
  126. package/dist/esm/src/schema/schema.js +102 -67
  127. package/dist/esm/src/schema/string.js +147 -59
  128. package/dist/esm/src/schema/union.js +119 -40
  129. package/dist/esm/src/types.js +14 -1
  130. package/dist/esm/src/utils.js +56 -27
  131. package/dist/esm/src/validators/array.js +6 -1
  132. package/dist/esm/src/validators/boolean.js +2 -0
  133. package/dist/esm/src/validators/datetime.js +4 -0
  134. package/dist/esm/src/validators/number.js +9 -23
  135. package/dist/esm/src/validators/object.js +1 -0
  136. package/dist/esm/src/validators/schema.js +5 -1
  137. package/dist/esm/src/validators/string.js +30 -2
  138. package/dist/esm/src/validators/union.js +5 -4
  139. package/dist/esm/src/validators/utils.js +62 -36
  140. package/package.json +3 -3
  141. package/src/adapter/fields/array.ts +2 -2
  142. package/src/adapter/fields/boolean.ts +3 -8
  143. package/src/adapter/fields/datetime.ts +3 -9
  144. package/src/adapter/fields/index.ts +11 -11
  145. package/src/adapter/fields/number.ts +3 -9
  146. package/src/adapter/fields/object.ts +13 -10
  147. package/src/adapter/fields/string.ts +3 -9
  148. package/src/adapter/fields/union.ts +3 -9
  149. package/src/adapter/index.ts +1 -0
  150. package/src/adapter/types.ts +60 -45
  151. package/src/constants.ts +1 -3
  152. package/src/domain.ts +15 -1
  153. package/src/index.ts +189 -211
  154. package/src/model.ts +119 -115
  155. package/src/schema/array.ts +274 -90
  156. package/src/schema/boolean.ts +145 -60
  157. package/src/schema/datetime.ts +133 -49
  158. package/src/schema/number.ts +210 -173
  159. package/src/schema/object.ts +167 -74
  160. package/src/schema/schema.ts +205 -126
  161. package/src/schema/string.ts +221 -94
  162. package/src/schema/types.ts +44 -16
  163. package/src/schema/union.ts +193 -68
  164. package/src/types.ts +53 -0
  165. package/src/utils.ts +115 -57
  166. package/src/validators/array.ts +46 -27
  167. package/src/validators/boolean.ts +13 -7
  168. package/src/validators/datetime.ts +24 -16
  169. package/src/validators/number.ts +53 -63
  170. package/src/validators/object.ts +6 -5
  171. package/src/validators/schema.ts +33 -25
  172. package/src/validators/string.ts +122 -59
  173. package/src/validators/union.ts +8 -8
  174. package/src/validators/utils.ts +67 -42
@@ -17,7 +17,6 @@ _export(exports, {
17
17
  }
18
18
  });
19
19
  var _schema = /*#__PURE__*/ _interop_require_default(require("./schema"));
20
- var _conf = require("../conf");
21
20
  var _utils = require("../utils");
22
21
  var _union = require("../validators/union");
23
22
  var _utils1 = /*#__PURE__*/ _interop_require_default(require("../validators/utils"));
@@ -325,287 +324,280 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
325
324
  _class_call_check(this, UnionSchema);
326
325
  var _this;
327
326
  _this = _super.call(this);
327
+ _define_property(_assert_this_initialized(_this), "__type", {
328
+ message: 'Invalid type',
329
+ check: function(value) {
330
+ return Array.from(_this.__schemas).some(function(schema) {
331
+ return schema['__type'].check(value);
332
+ });
333
+ }
334
+ });
328
335
  _define_property(_assert_this_initialized(_this), "__schemas", new Set());
329
336
  _this.__schemas = new Set(schemas);
330
337
  return _this;
331
338
  }
332
339
  _create_class(UnionSchema, [
333
340
  {
334
- key: "_transformToAdapter",
335
- value: function _transformToAdapter(options) {
341
+ key: "__transformToAdapter",
342
+ value: function __transformToAdapter(options) {
336
343
  var _this = this;
337
344
  return _async_to_generator(function() {
338
345
  return _ts_generator(this, function(_state) {
339
- switch(_state.label){
340
- case 0:
341
- return [
342
- 4,
343
- (0, _utils.defaultTransformToAdapter)(function() {
344
- var _ref = _async_to_generator(function(adapter) {
345
- var promises, shouldBeHighPriorityFallback, transformedSchemasAsString, transformedSchemas, shouldBeHandledByFallback, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step;
346
- return _ts_generator(this, function(_state) {
347
- switch(_state.label){
348
- case 0:
349
- promises = [];
350
- shouldBeHighPriorityFallback = adapter.union === undefined;
351
- transformedSchemasAsString = [];
352
- transformedSchemas = [];
353
- shouldBeHandledByFallback = shouldBeHighPriorityFallback;
354
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
355
- try {
356
- _loop = function() {
357
- var schemaToTransform = _step.value;
358
- var awaitableTransformer = function() {
359
- var _ref = _async_to_generator(function() {
360
- var _ref, transformedData, shouldAddFallbackValidationForThisKey, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, transformedSchema;
361
- return _ts_generator(this, function(_state) {
362
- switch(_state.label){
363
- case 0:
364
- return [
365
- 4,
366
- (0, _utils.transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas)(schemaToTransform, options)
367
- ];
368
- case 1:
369
- _ref = _sliced_to_array.apply(void 0, [
370
- _state.sent(),
371
- 2
372
- ]), transformedData = _ref[0], shouldAddFallbackValidationForThisKey = _ref[1];
373
- if (shouldAddFallbackValidationForThisKey) shouldBeHandledByFallback = true;
374
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
375
- try {
376
- for(_iterator = transformedData[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
377
- transformedSchema = _step.value;
378
- transformedSchemasAsString.push(transformedSchema.asString);
379
- transformedSchemas.push(transformedSchema.transformed);
380
- }
381
- } catch (err) {
382
- _didIteratorError = true;
383
- _iteratorError = err;
384
- } finally{
385
- try {
386
- if (!_iteratorNormalCompletion && _iterator.return != null) {
387
- _iterator.return();
388
- }
389
- } finally{
390
- if (_didIteratorError) {
391
- throw _iteratorError;
392
- }
393
- }
346
+ return [
347
+ 2,
348
+ (0, _utils.defaultTransformToAdapter)(function() {
349
+ var _ref = _async_to_generator(function(adapter) {
350
+ var promises, shouldBeHighPriorityFallback, transformedSchemasAsString, transformedSchemas, shouldBeHandledByFallback, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _loop, _iterator, _step;
351
+ return _ts_generator(this, function(_state) {
352
+ switch(_state.label){
353
+ case 0:
354
+ promises = [];
355
+ shouldBeHighPriorityFallback = adapter.union === undefined;
356
+ transformedSchemasAsString = [];
357
+ transformedSchemas = [];
358
+ shouldBeHandledByFallback = shouldBeHighPriorityFallback;
359
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
360
+ try {
361
+ _loop = function() {
362
+ var schemaToTransform = _step.value;
363
+ var awaitableTransformer = function() {
364
+ var _ref = _async_to_generator(function() {
365
+ var _ref, transformedData, shouldAddFallbackValidationForThisKey, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, transformedSchema;
366
+ return _ts_generator(this, function(_state) {
367
+ switch(_state.label){
368
+ case 0:
369
+ return [
370
+ 4,
371
+ (0, _utils.transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas)(schemaToTransform, options)
372
+ ];
373
+ case 1:
374
+ _ref = _sliced_to_array.apply(void 0, [
375
+ _state.sent(),
376
+ 2
377
+ ]), transformedData = _ref[0], shouldAddFallbackValidationForThisKey = _ref[1];
378
+ if (shouldAddFallbackValidationForThisKey) shouldBeHandledByFallback = true;
379
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
380
+ try {
381
+ for(_iterator = transformedData[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
382
+ transformedSchema = _step.value;
383
+ transformedSchemasAsString.push(transformedSchema.asString);
384
+ transformedSchemas.push(transformedSchema.transformed);
385
+ }
386
+ } catch (err) {
387
+ _didIteratorError = true;
388
+ _iteratorError = err;
389
+ } finally{
390
+ try {
391
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
392
+ _iterator.return();
393
+ }
394
+ } finally{
395
+ if (_didIteratorError) {
396
+ throw _iteratorError;
394
397
  }
395
- return [
396
- 2
397
- ];
398
+ }
398
399
  }
399
- });
400
- });
401
- return function awaitableTransformer() {
402
- return _ref.apply(this, arguments);
403
- };
404
- }();
405
- promises.push(awaitableTransformer());
400
+ return [
401
+ 2
402
+ ];
403
+ }
404
+ });
405
+ });
406
+ return function awaitableTransformer() {
407
+ return _ref.apply(this, arguments);
406
408
  };
407
- for(_iterator = _this.__schemas.values()[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
408
- } catch (err) {
409
- _didIteratorError = true;
410
- _iteratorError = err;
411
- } finally{
412
- try {
413
- if (!_iteratorNormalCompletion && _iterator.return != null) {
414
- _iterator.return();
415
- }
416
- } finally{
417
- if (_didIteratorError) {
418
- throw _iteratorError;
419
- }
420
- }
409
+ }();
410
+ promises.push(awaitableTransformer());
411
+ };
412
+ for(_iterator = _this.__schemas.values()[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true)_loop();
413
+ } catch (err) {
414
+ _didIteratorError = true;
415
+ _iteratorError = err;
416
+ } finally{
417
+ try {
418
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
419
+ _iterator.return();
421
420
  }
422
- if (shouldBeHandledByFallback) {
423
- _utils1.default.createAndAppendFallback(_this, (0, _union.unionValidation)(Array.from(_this.__schemas)), {
424
- at: 0,
425
- removeCurrent: true
426
- });
421
+ } finally{
422
+ if (_didIteratorError) {
423
+ throw _iteratorError;
427
424
  }
428
- return [
429
- 4,
430
- Promise.all(promises)
431
- ];
432
- case 1:
433
- _state.sent();
434
- return [
435
- 2,
436
- (0, _utils.defaultTransform)('union', _this, adapter, adapter.union, function(isStringVersion) {
437
- return {
438
- nullable: _this.__nullable,
439
- optional: _this.__optional,
440
- schemas: isStringVersion ? transformedSchemasAsString : transformedSchemas,
441
- parsers: {
442
- nullable: _this.__nullable.allow,
443
- optional: _this.__optional.allow
444
- }
445
- };
446
- }, {}, {
447
- shouldAddStringVersion: options.shouldAddStringVersion,
448
- fallbackIfNotSupported: /*#__PURE__*/ _async_to_generator(function() {
449
- var transformedSchemasAsPromises, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, schema, _;
450
- return _ts_generator(this, function(_state) {
451
- switch(_state.label){
452
- case 0:
453
- if (options.appendFallbacksBeforeAdapterValidation) options.appendFallbacksBeforeAdapterValidation('union', function() {
454
- var _ref = _async_to_generator(function(adapter, fieldAdapter, schema, translatedSchemas, value, path, options) {
455
- var parsedValues, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, translatedSchema, _ref, parsed, errors, err;
456
- return _ts_generator(this, function(_state) {
457
- switch(_state.label){
458
- case 0:
459
- parsedValues = {
460
- parsed: value,
461
- errors: []
462
- };
463
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
464
- _state.label = 1;
465
- case 1:
466
- _state.trys.push([
467
- 1,
468
- 6,
469
- 7,
470
- 8
471
- ]);
472
- _iterator = translatedSchemas[Symbol.iterator]();
473
- _state.label = 2;
474
- case 2:
475
- if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
476
- 3,
477
- 5
478
- ];
479
- translatedSchema = _step.value;
480
- return [
481
- 4,
482
- schema.__validateByAdapter(adapter, fieldAdapter, translatedSchema, value, path, options)
483
- ];
484
- case 3:
485
- _ref = _state.sent(), parsed = _ref.parsed, errors = _ref.errors;
486
- // eslint-disable-next-line ts/no-unnecessary-condition
487
- if ((errors || []).length <= 0) return [
488
- 2,
489
- {
490
- parsed: parsed,
491
- errors: errors
492
- }
493
- ];
494
- else {
495
- parsedValues.parsed = parsed;
496
- // eslint-disable-next-line ts/no-unnecessary-condition
497
- parsedValues.errors = (parsedValues.errors || []).concat(errors || []);
498
- }
499
- _state.label = 4;
500
- case 4:
501
- _iteratorNormalCompletion = true;
502
- return [
503
- 3,
504
- 2
505
- ];
506
- case 5:
507
- return [
508
- 3,
509
- 8
510
- ];
511
- case 6:
512
- err = _state.sent();
513
- _didIteratorError = true;
514
- _iteratorError = err;
515
- return [
516
- 3,
517
- 8
518
- ];
519
- case 7:
520
- try {
521
- if (!_iteratorNormalCompletion && _iterator.return != null) {
522
- _iterator.return();
523
- }
524
- } finally{
525
- if (_didIteratorError) {
526
- throw _iteratorError;
527
- }
528
- }
529
- return [
530
- 7
531
- ];
532
- case 8:
533
- return [
534
- 2,
535
- parsedValues
536
- ];
425
+ }
426
+ }
427
+ if (shouldBeHandledByFallback) {
428
+ _utils1.default.createAndAppendFallback(_this, (0, _union.unionValidation)(Array.from(_this.__schemas)), {
429
+ at: 0,
430
+ removeCurrent: true
431
+ });
432
+ }
433
+ return [
434
+ 4,
435
+ Promise.all(promises)
436
+ ];
437
+ case 1:
438
+ _state.sent();
439
+ return [
440
+ 2,
441
+ (0, _utils.defaultTransform)('union', _this, adapter, adapter.union, function(isStringVersion) {
442
+ return {
443
+ nullable: _this.__nullable,
444
+ type: _this.__type,
445
+ optional: _this.__optional,
446
+ schemas: isStringVersion ? transformedSchemasAsString : transformedSchemas,
447
+ parsers: {
448
+ nullable: _this.__nullable.allow,
449
+ optional: _this.__optional.allow
450
+ }
451
+ };
452
+ }, {}, {
453
+ shouldAddStringVersion: options.shouldAddStringVersion,
454
+ fallbackIfNotSupported: /*#__PURE__*/ _async_to_generator(function() {
455
+ var transformedSchemasAsPromises, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, schema;
456
+ return _ts_generator(this, function(_state) {
457
+ switch(_state.label){
458
+ case 0:
459
+ if (options.appendFallbacksBeforeAdapterValidation) options.appendFallbacksBeforeAdapterValidation(_this, 'union', function() {
460
+ var _ref = _async_to_generator(function(adapter, fieldAdapter, schema, translatedSchemas, value, path, options) {
461
+ var parsedValues, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, translatedSchema, _ref, parsed, errors, err;
462
+ return _ts_generator(this, function(_state) {
463
+ switch(_state.label){
464
+ case 0:
465
+ parsedValues = {
466
+ parsed: value,
467
+ errors: []
468
+ };
469
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
470
+ _state.label = 1;
471
+ case 1:
472
+ _state.trys.push([
473
+ 1,
474
+ 6,
475
+ 7,
476
+ 8
477
+ ]);
478
+ _iterator = translatedSchemas[Symbol.iterator]();
479
+ _state.label = 2;
480
+ case 2:
481
+ if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
482
+ 3,
483
+ 5
484
+ ];
485
+ translatedSchema = _step.value;
486
+ return [
487
+ 4,
488
+ schema.__validateByAdapter(adapter, fieldAdapter, translatedSchema, value, path, options)
489
+ ];
490
+ case 3:
491
+ _ref = _state.sent(), parsed = _ref.parsed, errors = _ref.errors;
492
+ // eslint-disable-next-line ts/no-unnecessary-condition
493
+ if ((errors || []).length <= 0) return [
494
+ 2,
495
+ {
496
+ parsed: parsed,
497
+ errors: errors
498
+ }
499
+ ];
500
+ else {
501
+ parsedValues.parsed = parsed;
502
+ // eslint-disable-next-line ts/no-unnecessary-condition
503
+ parsedValues.errors = (parsedValues.errors || []).concat(errors || []);
537
504
  }
538
- });
539
- });
540
- return function(adapter, fieldAdapter, schema, translatedSchemas, value, path, options) {
541
- return _ref.apply(this, arguments);
542
- };
543
- }());
544
- transformedSchemasAsPromises = [];
545
- _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
546
- try {
547
- for(_iterator = _this.__schemas[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
548
- schema = _step.value;
549
- transformedSchemasAsPromises.push(schema.__transformToAdapter(options));
550
- }
551
- } catch (err) {
552
- _didIteratorError = true;
553
- _iteratorError = err;
554
- } finally{
555
- try {
556
- if (!_iteratorNormalCompletion && _iterator.return != null) {
557
- _iterator.return();
558
- }
559
- } finally{
560
- if (_didIteratorError) {
561
- throw _iteratorError;
562
- }
505
+ _state.label = 4;
506
+ case 4:
507
+ _iteratorNormalCompletion = true;
508
+ return [
509
+ 3,
510
+ 2
511
+ ];
512
+ case 5:
513
+ return [
514
+ 3,
515
+ 8
516
+ ];
517
+ case 6:
518
+ err = _state.sent();
519
+ _didIteratorError = true;
520
+ _iteratorError = err;
521
+ return [
522
+ 3,
523
+ 8
524
+ ];
525
+ case 7:
526
+ try {
527
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
528
+ _iterator.return();
529
+ }
530
+ } finally{
531
+ if (_didIteratorError) {
532
+ throw _iteratorError;
533
+ }
534
+ }
535
+ return [
536
+ 7
537
+ ];
538
+ case 8:
539
+ return [
540
+ 2,
541
+ parsedValues
542
+ ];
563
543
  }
544
+ });
545
+ });
546
+ return function(adapter, fieldAdapter, schema, translatedSchemas, value, path, options) {
547
+ return _ref.apply(this, arguments);
548
+ };
549
+ }());
550
+ transformedSchemasAsPromises = [];
551
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
552
+ try {
553
+ for(_iterator = _this.__schemas[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
554
+ schema = _step.value;
555
+ transformedSchemasAsPromises.push(schema.__transformToAdapter(options));
556
+ }
557
+ } catch (err) {
558
+ _didIteratorError = true;
559
+ _iteratorError = err;
560
+ } finally{
561
+ try {
562
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
563
+ _iterator.return();
564
+ }
565
+ } finally{
566
+ if (_didIteratorError) {
567
+ throw _iteratorError;
564
568
  }
565
- _ = console.log;
566
- return [
567
- 4,
568
- Promise.all(transformedSchemasAsPromises)
569
- ];
570
- case 1:
571
- _.apply(console, [
572
- _state.sent().flat()
573
- ]);
574
- return [
575
- 4,
576
- Promise.all(transformedSchemasAsPromises)
577
- ];
578
- case 2:
579
- return [
580
- 2,
581
- _state.sent().flat()
582
- ];
569
+ }
583
570
  }
584
- });
585
- })
586
- })
587
- ];
588
- }
589
- });
590
- });
591
- return function(adapter) {
592
- return _ref.apply(this, arguments);
593
- };
594
- }(), _this.__transformedSchemas, options, 'union')
595
- ];
596
- case 1:
597
- return [
598
- 2,
599
- _state.sent()
600
- ];
601
- }
571
+ return [
572
+ 4,
573
+ Promise.all(transformedSchemasAsPromises)
574
+ ];
575
+ case 1:
576
+ return [
577
+ 2,
578
+ _state.sent().flat()
579
+ ];
580
+ }
581
+ });
582
+ })
583
+ })
584
+ ];
585
+ }
586
+ });
587
+ });
588
+ return function(adapter) {
589
+ return _ref.apply(this, arguments);
590
+ };
591
+ }(), _this, _this.__transformedSchemas, options, 'union')
592
+ ];
602
593
  });
603
594
  })();
604
595
  }
605
596
  },
606
597
  {
607
598
  /**
608
- * This let's you refine the schema with custom validations. This is useful when you want to validate something that is not supported by default by the schema adapter.
599
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something that
600
+ * is not supported by default by the schema adapter.
609
601
  *
610
602
  * @example
611
603
  * ```typescript
@@ -617,7 +609,8 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
617
609
  *
618
610
  * const { errors, parsed } = await numberSchema.parse(-1);
619
611
  *
620
- * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
612
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
613
+ * console.log(errors);
621
614
  * ```
622
615
  *
623
616
  * @param refinementCallback - The callback that will be called to validate the value.
@@ -659,8 +652,28 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
659
652
  },
660
653
  {
661
654
  /**
662
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value is NULL by setting
663
- * the { message: 'Your custom message', allow: false } on the options.
655
+ * Just adds a message when the value is undefined. It's just a syntax sugar for
656
+ *
657
+ * ```typescript
658
+ * p.string().optional({ message: 'This value cannot be null', allow: false })
659
+ * ```
660
+ *
661
+ * @param options - The options of nonOptional function
662
+ * @param options.message - A custom message if the value is undefined.
663
+ *
664
+ * @returns - The schema.
665
+ */ key: "nonOptional",
666
+ value: function nonOptional(options) {
667
+ return _get(_get_prototype_of(UnionSchema.prototype), "optional", this).call(this, {
668
+ message: options === null || options === void 0 ? void 0 : options.message,
669
+ allow: false
670
+ });
671
+ }
672
+ },
673
+ {
674
+ /**
675
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
676
+ * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
664
677
  *
665
678
  * @example
666
679
  * ```typescript
@@ -689,14 +702,36 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
689
702
  },
690
703
  {
691
704
  /**
692
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
705
+ * Just adds a message when the value is null. It's just a syntax sugar for
706
+ *
707
+ * ```typescript
708
+ * p.string().nullable({ message: 'This value cannot be null', allow: false })
709
+ * ```
710
+ *
711
+ * @param options - The options of nonNullable function
712
+ * @param options.message - A custom message if the value is null.
713
+ *
714
+ * @returns - The schema.
715
+ */ key: "nonNullable",
716
+ value: function nonNullable(options) {
717
+ return _get(_get_prototype_of(UnionSchema.prototype), "nullable", this).call(this, {
718
+ message: (options === null || options === void 0 ? void 0 : options.message) || '',
719
+ allow: false
720
+ });
721
+ }
722
+ },
723
+ {
724
+ /**
725
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
726
+ * that way
693
727
  * otherwise it will set the value to undefined after it's validated.
694
728
  * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
695
729
  * function. This will remove the value from the representation of the schema.
696
730
  *
697
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
698
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
699
- * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
731
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
732
+ * function. But if you want to remove the value from the internal representation, you can pass the argument
733
+ * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
734
+ * the argument `toRepresentation` as true as well.
700
735
  *
701
736
  * @example
702
737
  * ```typescript
@@ -718,9 +753,10 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
718
753
  * ```
719
754
  *
720
755
  *
721
- * @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
722
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
723
- * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
756
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
757
+ * the {@link data} function. But if you want to remove the value from the internal representation, you can pass
758
+ * the argument `toInternal` as true. Then if you still want to remove the value from the representation, you
759
+ * will need to pass the argument `toRepresentation` as true as well.
724
760
  *
725
761
  * @returns The schema.
726
762
  */ key: "omit",
@@ -730,9 +766,9 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
730
766
  },
731
767
  {
732
768
  /**
733
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
734
- * like a database. You should always return the schema after you save the value, that way we will always have the correct type
735
- * of the schema after the save operation.
769
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
770
+ * source like a database. You should always return the schema after you save the value, that way we will always
771
+ * have the correct type of the schema after the save operation.
736
772
  *
737
773
  * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
738
774
  *
@@ -777,7 +813,8 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
777
813
  },
778
814
  {
779
815
  /**
780
- * This function is used to add a default value to the schema. If the value is either undefined or null, the default value will be used.
816
+ * This function is used to add a default value to the schema. If the value is either undefined or null, the default
817
+ * value will be used.
781
818
  *
782
819
  * @example
783
820
  * ```typescript
@@ -794,10 +831,98 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
794
831
  return _get(_get_prototype_of(UnionSchema.prototype), "default", this).call(this, defaultValueOrFunction);
795
832
  }
796
833
  },
834
+ {
835
+ key: "data",
836
+ value: /**
837
+ * This function is used to transform the value to the representation without validating it.
838
+ * This is useful when you want to return a data from a query directly to the user. But for example
839
+ * you are returning the data of a user, you can clean the password or any other sensitive data.
840
+ *
841
+ * @example
842
+ * ```typescript
843
+ * import * as p from '@palmares/schemas';
844
+ *
845
+ * const userSchema = p.object({
846
+ * id: p.number().optional(),
847
+ * name: p.string(),
848
+ * email: p.string().email(),
849
+ * password: p.string().optional()
850
+ * }).toRepresentation(async (value) => {
851
+ * return {
852
+ * id: value.id,
853
+ * name: value.name,
854
+ * email: value.email
855
+ * }
856
+ * });
857
+ *
858
+ * const user = await userSchema.data({
859
+ * id: 1,
860
+ * name: 'John Doe',
861
+ * email: 'john@gmail.com',
862
+ * password: '123456'
863
+ * });
864
+ * ```
865
+ */ function data(value) {
866
+ var _this = this;
867
+ var _this1 = this, _superprop_get_data = function() {
868
+ return _get(_get_prototype_of(UnionSchema.prototype), "data", _this);
869
+ };
870
+ return _async_to_generator(function() {
871
+ var parsedValue, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, schema;
872
+ return _ts_generator(this, function(_state) {
873
+ switch(_state.label){
874
+ case 0:
875
+ return [
876
+ 4,
877
+ _superprop_get_data().call(_this1, value)
878
+ ];
879
+ case 1:
880
+ parsedValue = _state.sent();
881
+ _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
882
+ try {
883
+ for(_iterator = Array.from(_this1.__schemas)[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
884
+ schema = _step.value;
885
+ if (schema['__optional'].allow && value === undefined) return [
886
+ 2,
887
+ schema.data(parsedValue)
888
+ ];
889
+ if (schema['__nullable'].allow && value === null) return [
890
+ 2,
891
+ schema.data(parsedValue)
892
+ ];
893
+ if (schema['__type'].check(parsedValue)) return [
894
+ 2,
895
+ schema.data(parsedValue)
896
+ ];
897
+ }
898
+ } catch (err) {
899
+ _didIteratorError = true;
900
+ _iteratorError = err;
901
+ } finally{
902
+ try {
903
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
904
+ _iterator.return();
905
+ }
906
+ } finally{
907
+ if (_didIteratorError) {
908
+ throw _iteratorError;
909
+ }
910
+ }
911
+ }
912
+ return [
913
+ 2,
914
+ parsedValue
915
+ ];
916
+ }
917
+ });
918
+ })();
919
+ }
920
+ },
797
921
  {
798
922
  /**
799
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call this function to let you customize
800
- * the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
923
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
924
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
925
+ * No problem, you can use this function to customize the zod schema.
801
926
  *
802
927
  * @example
803
928
  * ```typescript
@@ -809,12 +934,13 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
809
934
  *
810
935
  * const { errors, parsed } = await numberSchema.parse(-1);
811
936
  *
812
- * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
937
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
938
+ * console.log(errors);
813
939
  * ```
814
940
  *
815
941
  * @param callback - The callback that will be called to customize the schema.
816
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to compile the underlying schema
817
- * to a string so you can save it for future runs.
942
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
943
+ * to compile the underlying schema to a string so you can save it for future runs.
818
944
  *
819
945
  * @returns The schema.
820
946
  */ key: "extends",
@@ -824,9 +950,9 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
824
950
  },
825
951
  {
826
952
  /**
827
- * This function is used to transform the value to the representation of the schema. When using the {@link data} function. With this function you have full
828
- * control to add data cleaning for example, transforming the data and whatever. Another use case is when you want to return deeply nested recursive data.
829
- * The schema maps to itself.
953
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
954
+ * function. With this function you have full control to add data cleaning for example, transforming the data and
955
+ * whatever. Another use case is when you want to return deeply nested recursive data. The schema maps to itself.
830
956
  *
831
957
  * @example
832
958
  * ```typescript
@@ -872,8 +998,9 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
872
998
  },
873
999
  {
874
1000
  /**
875
- * This function is used to transform the value to the internal representation of the schema. This is useful when you want to transform the value
876
- * to a type that the schema adapter can understand. For example, you might want to transform a string to a date. This is the function you use.
1001
+ * This function is used to transform the value to the internal representation of the schema. This is useful when you
1002
+ * want to transform the value to a type that the schema adapter can understand. For example, you might want to
1003
+ * transform a string to a date. This is the function you use.
877
1004
  *
878
1005
  * @example
879
1006
  * ```typescript
@@ -909,8 +1036,9 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
909
1036
  },
910
1037
  {
911
1038
  /**
912
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string, you can convert that string to a date
913
- * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
1039
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
1040
+ * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
1041
+ * that the schema adapter can understand.
914
1042
  *
915
1043
  * @example
916
1044
  * ```
@@ -937,12 +1065,6 @@ var UnionSchema = /*#__PURE__*/ function(Schema) {
937
1065
  key: "new",
938
1066
  value: function _new(schemas) {
939
1067
  var returnValue = new UnionSchema(schemas);
940
- var adapterInstance = (0, _conf.getDefaultAdapter)();
941
- returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
942
- transformed: false,
943
- adapter: adapterInstance,
944
- schemas: []
945
- };
946
1068
  return returnValue;
947
1069
  }
948
1070
  }