@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.
- package/.turbo/turbo-build$colon$watch.log +12 -410
- package/CHANGELOG.md +17 -0
- package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +22 -0
- package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +156 -0
- package/__tests__/.drizzle/migrations/meta/_journal.json +13 -0
- package/__tests__/.drizzle/schema.ts +35 -0
- package/__tests__/drizzle.config.ts +11 -0
- package/__tests__/eslint.config.js +10 -0
- package/__tests__/manage.ts +5 -0
- package/__tests__/node_modules/.bin/drizzle-kit +17 -0
- package/__tests__/node_modules/.bin/esbuild +14 -0
- package/__tests__/node_modules/.bin/tsc +17 -0
- package/__tests__/node_modules/.bin/tsserver +17 -0
- package/__tests__/node_modules/.bin/tsx +17 -0
- package/__tests__/package.json +36 -0
- package/__tests__/sqlite.db +0 -0
- package/__tests__/src/core/array.test.ts +130 -0
- package/__tests__/src/core/boolean.test.ts +66 -0
- package/__tests__/src/core/datetime.test.ts +102 -0
- package/__tests__/src/core/index.ts +35 -0
- package/__tests__/src/core/model.test.ts +260 -0
- package/__tests__/src/core/models.ts +50 -0
- package/__tests__/src/core/numbers.test.ts +177 -0
- package/__tests__/src/core/object.test.ts +198 -0
- package/__tests__/src/core/string.test.ts +222 -0
- package/__tests__/src/core/test.test.ts +59 -0
- package/__tests__/src/core/types.test.ts +97 -0
- package/__tests__/src/core/union.test.ts +99 -0
- package/__tests__/src/settings.ts +71 -0
- package/__tests__/tsconfig.json +11 -0
- package/dist/cjs/src/adapter/fields/index.js +2 -2
- package/dist/cjs/src/adapter/fields/object.js +9 -0
- package/dist/cjs/src/adapter/index.js +1 -0
- package/dist/cjs/src/constants.js +1 -7
- package/dist/cjs/src/domain.js +146 -1
- package/dist/cjs/src/index.js +69 -74
- package/dist/cjs/src/model.js +206 -206
- package/dist/cjs/src/schema/array.js +185 -58
- package/dist/cjs/src/schema/boolean.js +105 -44
- package/dist/cjs/src/schema/datetime.js +104 -38
- package/dist/cjs/src/schema/number.js +134 -114
- package/dist/cjs/src/schema/object.js +106 -43
- package/dist/cjs/src/schema/schema.js +123 -75
- package/dist/cjs/src/schema/string.js +152 -58
- package/dist/cjs/src/schema/union.js +412 -290
- package/dist/cjs/src/utils.js +42 -15
- package/dist/cjs/src/validators/array.js +6 -1
- package/dist/cjs/src/validators/boolean.js +2 -0
- package/dist/cjs/src/validators/datetime.js +4 -0
- package/dist/cjs/src/validators/number.js +12 -40
- package/dist/cjs/src/validators/object.js +1 -0
- package/dist/cjs/src/validators/schema.js +5 -1
- package/dist/cjs/src/validators/string.js +30 -2
- package/dist/cjs/src/validators/union.js +5 -4
- package/dist/cjs/src/validators/utils.js +99 -27
- package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
- package/dist/cjs/types/adapter/fields/array.d.ts +2 -2
- package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/index.d.ts +2 -2
- package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/object.d.ts +2 -1
- package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -1
- package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -1
- package/dist/cjs/types/adapter/index.d.ts +1 -0
- package/dist/cjs/types/adapter/index.d.ts.map +1 -1
- package/dist/cjs/types/adapter/types.d.ts +28 -18
- package/dist/cjs/types/adapter/types.d.ts.map +1 -1
- package/dist/cjs/types/constants.d.ts +0 -1
- package/dist/cjs/types/constants.d.ts.map +1 -1
- package/dist/cjs/types/domain.d.ts +5 -4
- package/dist/cjs/types/domain.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +78 -55
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/model.d.ts +17 -17
- package/dist/cjs/types/model.d.ts.map +1 -1
- package/dist/cjs/types/schema/array.d.ts +168 -47
- package/dist/cjs/types/schema/array.d.ts.map +1 -1
- package/dist/cjs/types/schema/boolean.d.ts +103 -44
- package/dist/cjs/types/schema/boolean.d.ts.map +1 -1
- package/dist/cjs/types/schema/datetime.d.ts +90 -30
- package/dist/cjs/types/schema/datetime.d.ts.map +1 -1
- package/dist/cjs/types/schema/number.d.ts +133 -125
- package/dist/cjs/types/schema/number.d.ts.map +1 -1
- package/dist/cjs/types/schema/object.d.ts +104 -35
- package/dist/cjs/types/schema/object.d.ts.map +1 -1
- package/dist/cjs/types/schema/schema.d.ts +62 -44
- package/dist/cjs/types/schema/schema.d.ts.map +1 -1
- package/dist/cjs/types/schema/string.d.ts +152 -65
- package/dist/cjs/types/schema/string.d.ts.map +1 -1
- package/dist/cjs/types/schema/types.d.ts +11 -2
- package/dist/cjs/types/schema/types.d.ts.map +1 -1
- package/dist/cjs/types/schema/union.d.ts +133 -40
- package/dist/cjs/types/schema/union.d.ts.map +1 -1
- package/dist/cjs/types/types.d.ts +35 -0
- package/dist/cjs/types/types.d.ts.map +1 -1
- package/dist/cjs/types/utils.d.ts +41 -27
- package/dist/cjs/types/utils.d.ts.map +1 -1
- package/dist/cjs/types/validators/array.d.ts.map +1 -1
- package/dist/cjs/types/validators/boolean.d.ts.map +1 -1
- package/dist/cjs/types/validators/datetime.d.ts.map +1 -1
- package/dist/cjs/types/validators/number.d.ts +5 -6
- package/dist/cjs/types/validators/number.d.ts.map +1 -1
- package/dist/cjs/types/validators/object.d.ts.map +1 -1
- package/dist/cjs/types/validators/schema.d.ts +2 -2
- package/dist/cjs/types/validators/schema.d.ts.map +1 -1
- package/dist/cjs/types/validators/string.d.ts +9 -9
- package/dist/cjs/types/validators/string.d.ts.map +1 -1
- package/dist/cjs/types/validators/utils.d.ts +44 -27
- package/dist/cjs/types/validators/utils.d.ts.map +1 -1
- package/dist/esm/src/adapter/fields/index.js +2 -2
- package/dist/esm/src/adapter/fields/object.js +6 -0
- package/dist/esm/src/adapter/index.js +1 -0
- package/dist/esm/src/constants.js +1 -2
- package/dist/esm/src/domain.js +11 -1
- package/dist/esm/src/index.js +38 -73
- package/dist/esm/src/model.js +83 -78
- package/dist/esm/src/schema/array.js +136 -54
- package/dist/esm/src/schema/boolean.js +98 -44
- package/dist/esm/src/schema/datetime.js +91 -38
- package/dist/esm/src/schema/number.js +127 -110
- package/dist/esm/src/schema/object.js +98 -43
- package/dist/esm/src/schema/schema.js +102 -67
- package/dist/esm/src/schema/string.js +147 -59
- package/dist/esm/src/schema/union.js +119 -40
- package/dist/esm/src/types.js +14 -1
- package/dist/esm/src/utils.js +56 -27
- package/dist/esm/src/validators/array.js +6 -1
- package/dist/esm/src/validators/boolean.js +2 -0
- package/dist/esm/src/validators/datetime.js +4 -0
- package/dist/esm/src/validators/number.js +9 -23
- package/dist/esm/src/validators/object.js +1 -0
- package/dist/esm/src/validators/schema.js +5 -1
- package/dist/esm/src/validators/string.js +30 -2
- package/dist/esm/src/validators/union.js +5 -4
- package/dist/esm/src/validators/utils.js +62 -36
- package/package.json +3 -3
- package/src/adapter/fields/array.ts +2 -2
- package/src/adapter/fields/boolean.ts +3 -8
- package/src/adapter/fields/datetime.ts +3 -9
- package/src/adapter/fields/index.ts +11 -11
- package/src/adapter/fields/number.ts +3 -9
- package/src/adapter/fields/object.ts +13 -10
- package/src/adapter/fields/string.ts +3 -9
- package/src/adapter/fields/union.ts +3 -9
- package/src/adapter/index.ts +1 -0
- package/src/adapter/types.ts +60 -45
- package/src/constants.ts +1 -3
- package/src/domain.ts +15 -1
- package/src/index.ts +189 -211
- package/src/model.ts +119 -115
- package/src/schema/array.ts +274 -90
- package/src/schema/boolean.ts +145 -60
- package/src/schema/datetime.ts +133 -49
- package/src/schema/number.ts +210 -173
- package/src/schema/object.ts +167 -74
- package/src/schema/schema.ts +205 -126
- package/src/schema/string.ts +221 -94
- package/src/schema/types.ts +44 -16
- package/src/schema/union.ts +193 -68
- package/src/types.ts +53 -0
- package/src/utils.ts +115 -57
- package/src/validators/array.ts +46 -27
- package/src/validators/boolean.ts +13 -7
- package/src/validators/datetime.ts +24 -16
- package/src/validators/number.ts +53 -63
- package/src/validators/object.ts +6 -5
- package/src/validators/schema.ts +33 -25
- package/src/validators/string.ts +122 -59
- package/src/validators/union.ts +8 -8
- 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: "
|
335
|
-
value: function
|
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
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
var
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
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
|
-
|
396
|
-
2
|
397
|
-
];
|
398
|
+
}
|
398
399
|
}
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
400
|
+
return [
|
401
|
+
2
|
402
|
+
];
|
403
|
+
}
|
404
|
+
});
|
405
|
+
});
|
406
|
+
return function awaitableTransformer() {
|
407
|
+
return _ref.apply(this, arguments);
|
406
408
|
};
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
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
|
-
|
423
|
-
|
424
|
-
|
425
|
-
removeCurrent: true
|
426
|
-
});
|
421
|
+
} finally{
|
422
|
+
if (_didIteratorError) {
|
423
|
+
throw _iteratorError;
|
427
424
|
}
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
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
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
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
|
-
|
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
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
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
|
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
|
-
*
|
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
|
-
*
|
663
|
-
*
|
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
|
-
*
|
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}
|
698
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
699
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
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
|
722
|
-
* But if you want to remove the value from the internal representation, you can pass
|
723
|
-
* Then if you still want to remove the value from the representation, you
|
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
|
734
|
-
* like a database. You should always return the schema after you save the value, that way we will always
|
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
|
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
|
800
|
-
* the custom schema your own way. Our API does not support passthrough?
|
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
|
-
*
|
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
|
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}
|
828
|
-
* control to add data cleaning for example, transforming the data and
|
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
|
876
|
-
* to a type that the schema adapter can understand. For example, you might want to
|
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,
|
913
|
-
* here BEFORE the validation. This pretty much transforms the value to a type
|
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
|
}
|