@palmares/schemas 0.0.1 → 0.1.1
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/.turbo/turbo-build.log +13 -4
- package/CHANGELOG.md +26 -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
@@ -26,6 +26,9 @@ function _array_like_to_array(arr, len) {
|
|
26
26
|
function _array_with_holes(arr) {
|
27
27
|
if (Array.isArray(arr)) return arr;
|
28
28
|
}
|
29
|
+
function _array_without_holes(arr) {
|
30
|
+
if (Array.isArray(arr)) return _array_like_to_array(arr);
|
31
|
+
}
|
29
32
|
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
30
33
|
try {
|
31
34
|
var info = gen[key](arg);
|
@@ -94,6 +97,9 @@ function _instanceof(left, right) {
|
|
94
97
|
return left instanceof right;
|
95
98
|
}
|
96
99
|
}
|
100
|
+
function _iterable_to_array(iter) {
|
101
|
+
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
|
102
|
+
}
|
97
103
|
function _iterable_to_array_limit(arr, i) {
|
98
104
|
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
99
105
|
if (_i == null) return;
|
@@ -121,9 +127,15 @@ function _iterable_to_array_limit(arr, i) {
|
|
121
127
|
function _non_iterable_rest() {
|
122
128
|
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
123
129
|
}
|
130
|
+
function _non_iterable_spread() {
|
131
|
+
throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
132
|
+
}
|
124
133
|
function _sliced_to_array(arr, i) {
|
125
134
|
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
126
135
|
}
|
136
|
+
function _to_consumable_array(arr) {
|
137
|
+
return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
|
138
|
+
}
|
127
139
|
function _unsupported_iterable_to_array(o, minLen) {
|
128
140
|
if (!o) return;
|
129
141
|
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
@@ -231,15 +243,19 @@ var Schema = /*#__PURE__*/ function() {
|
|
231
243
|
"use strict";
|
232
244
|
function Schema() {
|
233
245
|
_class_call_check(this, Schema);
|
234
|
-
// Those functions will assume control of the validation process on adapters, instead of the schema.
|
235
|
-
//
|
236
|
-
//
|
237
|
-
//
|
238
|
-
//
|
246
|
+
// Those functions will assume control of the validation process on adapters, instead of the schema.
|
247
|
+
// Why this is used? The idea is that the Schema has NO idea
|
248
|
+
// that one of it's children might be an UnionSchema for example. The adapter might not support unions,
|
249
|
+
// so then we give control to the union. The parent schema will already have an array of translated
|
250
|
+
// adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number
|
251
|
+
// and one for the value as String. Of course this gets multiplied. So if we have a union with Number and String.
|
252
|
+
// We should take those two schemas from the array and validate them individually. This logic is
|
253
|
+
// handled by the union schema. If we have an intersection type for example, instead of validating
|
254
|
+
// One schema OR the other, we validate one schema AND the other. This will be handled
|
239
255
|
// by the schema that contains that intersection logic.
|
240
256
|
_define_property(this, "__beforeValidationCallbacks", new Map());
|
241
257
|
_define_property(this, "__cachedGetParent", void 0);
|
242
|
-
_define_property(this, "__alreadyAppliedModel",
|
258
|
+
_define_property(this, "__alreadyAppliedModel", void 0);
|
243
259
|
_define_property(this, "__runBeforeParseAndData", void 0);
|
244
260
|
_define_property(this, "__rootFallbacksValidator", void 0);
|
245
261
|
_define_property(this, "__saveCallback", void 0);
|
@@ -282,10 +298,23 @@ var Schema = /*#__PURE__*/ function() {
|
|
282
298
|
this.__cachedGetParent = value;
|
283
299
|
}
|
284
300
|
},
|
301
|
+
{
|
302
|
+
key: "__getDefaultTransformedSchemas",
|
303
|
+
value: function __getDefaultTransformedSchemas() {
|
304
|
+
var adapterInstance = (0, _conf.getDefaultAdapter)();
|
305
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
306
|
+
if (this.__transformedSchemas[adapterInstance.constructor.name] === undefined) this.__transformedSchemas[adapterInstance.constructor.name] = {
|
307
|
+
transformed: false,
|
308
|
+
adapter: adapterInstance,
|
309
|
+
schemas: []
|
310
|
+
};
|
311
|
+
}
|
312
|
+
},
|
285
313
|
{
|
286
314
|
key: "__validateByFallbacks",
|
287
315
|
value: /**
|
288
|
-
* This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
|
316
|
+
* This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
|
317
|
+
* This is nice because we can support things that the schema adapter is not able to support by default.
|
289
318
|
*
|
290
319
|
* @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors.
|
291
320
|
* @param path - The path of the error.
|
@@ -297,7 +326,7 @@ var Schema = /*#__PURE__*/ function() {
|
|
297
326
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
298
327
|
if (_this.__rootFallbacksValidator) return [
|
299
328
|
2,
|
300
|
-
_this.__rootFallbacksValidator.validate(options.errorsAsHashedSet
|
329
|
+
_this.__rootFallbacksValidator.validate(options.errorsAsHashedSet, path, parseResult, options)
|
301
330
|
];
|
302
331
|
return [
|
303
332
|
2,
|
@@ -310,9 +339,11 @@ var Schema = /*#__PURE__*/ function() {
|
|
310
339
|
{
|
311
340
|
key: "__validateByAdapter",
|
312
341
|
value: /**
|
313
|
-
* This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
|
314
|
-
*
|
315
|
-
*
|
342
|
+
* This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
|
343
|
+
* that data.
|
344
|
+
* So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate
|
345
|
+
* from the adapter we validate with the fallbacks so we can do all of the extra validations not handled by
|
346
|
+
* the adapter.
|
316
347
|
*
|
317
348
|
* @param value - The value to be validated.
|
318
349
|
* @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors on the validator.
|
@@ -360,7 +391,7 @@ var Schema = /*#__PURE__*/ function() {
|
|
360
391
|
return _ts_generator(this, function(_state) {
|
361
392
|
return [
|
362
393
|
2,
|
363
|
-
(0, _utils.formatErrorFromParseMethod)(adapter, fieldAdapter, error, path, options.errorsAsHashedSet || new Set())
|
394
|
+
(0, _utils.formatErrorFromParseMethod)(adapter, fieldAdapter, error, value, schema.transformed, path, options.errorsAsHashedSet || new Set())
|
364
395
|
];
|
365
396
|
});
|
366
397
|
});
|
@@ -378,7 +409,7 @@ var Schema = /*#__PURE__*/ function() {
|
|
378
409
|
case 3:
|
379
410
|
return [
|
380
411
|
4,
|
381
|
-
(0, _utils.formatErrorFromParseMethod)(adapter, fieldAdapter, parseResult.errors, path, options.errorsAsHashedSet || new Set())
|
412
|
+
(0, _utils.formatErrorFromParseMethod)(adapter, fieldAdapter, parseResult.errors, value, schema.transformed, path, options.errorsAsHashedSet || new Set())
|
382
413
|
];
|
383
414
|
case 4:
|
384
415
|
parseResult.errors = [
|
@@ -386,6 +417,9 @@ var Schema = /*#__PURE__*/ function() {
|
|
386
417
|
];
|
387
418
|
_state.label = 5;
|
388
419
|
case 5:
|
420
|
+
parseResult.errors = parseResult.errors.filter(function(error) {
|
421
|
+
return typeof error !== 'undefined';
|
422
|
+
});
|
389
423
|
return [
|
390
424
|
2,
|
391
425
|
parseResult
|
@@ -652,10 +686,11 @@ var Schema = /*#__PURE__*/ function() {
|
|
652
686
|
var path = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : [], options = arguments.length > 2 ? arguments[2] : void 0;
|
653
687
|
var _this = this;
|
654
688
|
return _async_to_generator(function() {
|
655
|
-
var _options_schemaAdapter, shouldRunToInternalToBubbleUp, shouldCallDefaultFunction, shouldCallToValidateCallback, schemaAdapterFieldType, parseResult, adapterToUse, parsedResultsAfterFallbacks, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, callback, parsedValuesAfterValidationCallbacks, err, parsedValuesAfterValidatingByAdapter,
|
689
|
+
var _options_schemaAdapter, _options_toInternalToBubbleUp, shouldRunToInternalToBubbleUp, shouldCallDefaultFunction, shouldCallToValidateCallback, schemaAdapterFieldType, parseResult, adapterToUse, parsedResultsAfterFallbacks, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, callback, parsedValuesAfterValidationCallbacks, err, parsedValuesAfterValidatingByAdapter, hasToInternalCallback, shouldCallToInternalDuringParse, hasNoErrors, _iteratorNormalCompletion1, _didIteratorError1, _iteratorError1, _iterator1, _step1, functionToModifyResult, err;
|
656
690
|
return _ts_generator(this, function(_state) {
|
657
691
|
switch(_state.label){
|
658
692
|
case 0:
|
693
|
+
_this.__getDefaultTransformedSchemas();
|
659
694
|
if (!(typeof _this.__runBeforeParseAndData === 'function')) return [
|
660
695
|
3,
|
661
696
|
2
|
@@ -668,10 +703,13 @@ var Schema = /*#__PURE__*/ function() {
|
|
668
703
|
_state.sent();
|
669
704
|
_state.label = 2;
|
670
705
|
case 2:
|
671
|
-
// This is used to run the toInternal command. If we didn't do this, we would need to parse through all of
|
672
|
-
// from the leafs (ObjectSchemas) to the root schema. This is not
|
673
|
-
//
|
674
|
-
//
|
706
|
+
// This is used to run the toInternal command. If we didn't do this, we would need to parse through all of
|
707
|
+
// the schemas to run the toInternal command, from the leafs (ObjectSchemas) to the root schema. This is not
|
708
|
+
// a good idea, so what we do is that during validation the leafs attach a function to the
|
709
|
+
// options.toInternalToBubbleUp like
|
710
|
+
// `options.toInternalToBubbleUp.push(async () => (value[key] = await (schema as any).__toInternal(parsed)));``
|
711
|
+
// This way, when the root schema finishes the validation, it will run all of the functions in the
|
712
|
+
// toInternalToBubbleUp array, modifying the parsed value.
|
675
713
|
shouldRunToInternalToBubbleUp = options.toInternalToBubbleUp === undefined;
|
676
714
|
if (shouldRunToInternalToBubbleUp) options.toInternalToBubbleUp = [];
|
677
715
|
if (_instanceof(options.errorsAsHashedSet, Set) === false) options.errorsAsHashedSet = new Set();
|
@@ -706,32 +744,34 @@ var Schema = /*#__PURE__*/ function() {
|
|
706
744
|
errors: [],
|
707
745
|
parsed: value
|
708
746
|
};
|
709
|
-
|
710
|
-
|
747
|
+
return [
|
748
|
+
4,
|
749
|
+
_this.__parsersToTransformValue(value, _this.__parsers._fallbacks)
|
750
|
+
];
|
751
|
+
case 7:
|
752
|
+
value = _state.sent();
|
753
|
+
if (options.appendFallbacksBeforeAdapterValidation === undefined) options.appendFallbacksBeforeAdapterValidation = function(schema, name, callback) {
|
754
|
+
// We just need this if the union adapter is net defined but the parent is not of the same type.
|
755
|
+
// For example, it's a union child of o object schema
|
756
|
+
if (_this !== schema) _this.__beforeValidationCallbacks.set(name, callback);
|
711
757
|
};
|
712
758
|
if (!(_this.__transformedSchemas[((_options_schemaAdapter = options.schemaAdapter) === null || _options_schemaAdapter === void 0 ? void 0 : _options_schemaAdapter.constructor.name) || (0, _conf.getDefaultAdapter)().constructor.name].transformed === false)) return [
|
713
759
|
3,
|
714
|
-
|
760
|
+
9
|
715
761
|
];
|
716
762
|
return [
|
717
763
|
4,
|
718
764
|
_this.__transformToAdapter(options)
|
719
765
|
];
|
720
|
-
case 7:
|
721
|
-
_state.sent();
|
722
|
-
_state.label = 8;
|
723
766
|
case 8:
|
724
|
-
|
725
|
-
|
726
|
-
_this.__parsersToTransformValue(value, _this.__parsers._fallbacks)
|
727
|
-
];
|
767
|
+
_state.sent();
|
768
|
+
_state.label = 9;
|
728
769
|
case 9:
|
729
|
-
value = _state.sent();
|
730
770
|
adapterToUse = options.schemaAdapter ? options.schemaAdapter : Object.values(_this.__transformedSchemas)[0].adapter;
|
731
771
|
return [
|
732
772
|
4,
|
733
773
|
_this.__validateByFallbacks(path, {
|
734
|
-
errors:
|
774
|
+
errors: parseResult.errors,
|
735
775
|
parsed: value
|
736
776
|
}, options)
|
737
777
|
];
|
@@ -768,7 +808,7 @@ var Schema = /*#__PURE__*/ function() {
|
|
768
808
|
case 13:
|
769
809
|
parsedValuesAfterValidationCallbacks = _state.sent();
|
770
810
|
parseResult.parsed = parsedValuesAfterValidationCallbacks.parsed;
|
771
|
-
parseResult.errors = parsedValuesAfterValidationCallbacks.errors;
|
811
|
+
parseResult.errors = Array.isArray(parseResult.errors) && Array.isArray(parsedValuesAfterValidationCallbacks.errors) ? _to_consumable_array(parseResult.errors).concat(_to_consumable_array(parsedValuesAfterValidationCallbacks.errors)) : Array.isArray(parseResult.errors) ? parseResult.errors : parsedValuesAfterValidationCallbacks.errors;
|
772
812
|
_state.label = 14;
|
773
813
|
case 14:
|
774
814
|
_iteratorNormalCompletion = true;
|
@@ -819,9 +859,8 @@ var Schema = /*#__PURE__*/ function() {
|
|
819
859
|
parseResult.errors = (parseResult.errors || []).concat(parsedValuesAfterValidatingByAdapter.errors);
|
820
860
|
_state.label = 21;
|
821
861
|
case 21:
|
822
|
-
doesNotHaveErrors = !Array.isArray(parseResult.errors) || parseResult.errors.length === 0;
|
823
862
|
hasToInternalCallback = typeof _this.__toInternal === 'function';
|
824
|
-
shouldCallToInternalDuringParse =
|
863
|
+
shouldCallToInternalDuringParse = hasToInternalCallback && (((_options_toInternalToBubbleUp = options.toInternalToBubbleUp) === null || _options_toInternalToBubbleUp === void 0 ? void 0 : _options_toInternalToBubbleUp.length) === 0 || Array.isArray(options.toInternalToBubbleUp) === false);
|
825
864
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
826
865
|
hasNoErrors = parseResult.errors === undefined || (parseResult.errors || []).length === 0;
|
827
866
|
return [
|
@@ -845,6 +884,7 @@ var Schema = /*#__PURE__*/ function() {
|
|
845
884
|
isValid: false,
|
846
885
|
code: errorOrNothing.code,
|
847
886
|
message: errorOrNothing.message,
|
887
|
+
received: parseResult.parsed,
|
848
888
|
path: path
|
849
889
|
});
|
850
890
|
return [
|
@@ -944,7 +984,8 @@ var Schema = /*#__PURE__*/ function() {
|
|
944
984
|
},
|
945
985
|
{
|
946
986
|
/**
|
947
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
987
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
988
|
+
* that is not supported by default by the schema adapter.
|
948
989
|
*
|
949
990
|
* @example
|
950
991
|
* ```typescript
|
@@ -956,7 +997,8 @@ var Schema = /*#__PURE__*/ function() {
|
|
956
997
|
*
|
957
998
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
958
999
|
*
|
959
|
-
* console.log(errors);
|
1000
|
+
* console.log(errors);
|
1001
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
960
1002
|
* ```
|
961
1003
|
*
|
962
1004
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -1003,8 +1045,8 @@ var Schema = /*#__PURE__*/ function() {
|
|
1003
1045
|
},
|
1004
1046
|
{
|
1005
1047
|
/**
|
1006
|
-
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when
|
1007
|
-
* the { message: 'Your custom message', allow: false } on the options.
|
1048
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when
|
1049
|
+
* the value is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
1008
1050
|
*
|
1009
1051
|
* @example
|
1010
1052
|
* ```typescript
|
@@ -1039,8 +1081,8 @@ var Schema = /*#__PURE__*/ function() {
|
|
1039
1081
|
/**
|
1040
1082
|
* Appends a custom schema to the schema, this way it will bypass the creation of the schema in runtime.
|
1041
1083
|
*
|
1042
|
-
* By default when validating, on the first validation we create the schema. Just during the first validation.
|
1043
|
-
* so you can speed up the validation process.
|
1084
|
+
* By default when validating, on the first validation we create the schema. Just during the first validation.
|
1085
|
+
* With this function, you bypass that, so you can speed up the validation process.
|
1044
1086
|
*
|
1045
1087
|
* @example
|
1046
1088
|
* ```typescript
|
@@ -1072,14 +1114,15 @@ var Schema = /*#__PURE__*/ function() {
|
|
1072
1114
|
},
|
1073
1115
|
{
|
1074
1116
|
/**
|
1075
|
-
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
1076
|
-
* otherwise it will set the value to undefined after it's validated.
|
1117
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
1118
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
1077
1119
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
1078
1120
|
* function. This will remove the value from the representation of the schema.
|
1079
1121
|
*
|
1080
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
1081
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
1082
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
1122
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
1123
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
1124
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
1125
|
+
* the argument `toRepresentation` as true as well.
|
1083
1126
|
*
|
1084
1127
|
* @example
|
1085
1128
|
* ```typescript
|
@@ -1101,9 +1144,11 @@ var Schema = /*#__PURE__*/ function() {
|
|
1101
1144
|
* ```
|
1102
1145
|
*
|
1103
1146
|
*
|
1104
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
1105
|
-
*
|
1106
|
-
*
|
1147
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
1148
|
+
* the {@link data} function.
|
1149
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
1150
|
+
* as true. Then if you still want to remove the value from the representation, you will need to pass the
|
1151
|
+
* argument `toRepresentation` as true as well.
|
1107
1152
|
*
|
1108
1153
|
* @returns The schema.
|
1109
1154
|
*/ key: "omit",
|
@@ -1185,9 +1230,9 @@ var Schema = /*#__PURE__*/ function() {
|
|
1185
1230
|
},
|
1186
1231
|
{
|
1187
1232
|
/**
|
1188
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
1189
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
1190
|
-
* of the schema after the save operation.
|
1233
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
1234
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
1235
|
+
* the correct type of the schema after the save operation.
|
1191
1236
|
*
|
1192
1237
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
1193
1238
|
*
|
@@ -1237,10 +1282,11 @@ var Schema = /*#__PURE__*/ function() {
|
|
1237
1282
|
* This function is used to validate the schema and save the value to the database. It is used in
|
1238
1283
|
* conjunction with the {@link onSave} function.
|
1239
1284
|
*
|
1240
|
-
* Different from other validation libraries, palmares schemas is aware that you want to save. On your
|
1241
|
-
* we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
|
1242
|
-
* will return an object with the property `save` or the `errors`. If the errors are present,
|
1243
|
-
* to the user. If the save property is present, you can use to save the value to an
|
1285
|
+
* Different from other validation libraries, palmares schemas is aware that you want to save. On your
|
1286
|
+
* routes/functions we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
|
1287
|
+
* this function by default will return an object with the property `save` or the `errors`. If the errors are present,
|
1288
|
+
* you can return the errors to the user. If the save property is present, you can use to save the value to an
|
1289
|
+
* external source. e.g. a database.
|
1244
1290
|
*
|
1245
1291
|
* @example
|
1246
1292
|
* ```typescript
|
@@ -1425,6 +1471,7 @@ var Schema = /*#__PURE__*/ function() {
|
|
1425
1471
|
return _ts_generator(this, function(_state) {
|
1426
1472
|
switch(_state.label){
|
1427
1473
|
case 0:
|
1474
|
+
_this.__getDefaultTransformedSchemas();
|
1428
1475
|
if (!(typeof _this.__runBeforeParseAndData === 'function')) return [
|
1429
1476
|
3,
|
1430
1477
|
2
|
@@ -1486,7 +1533,8 @@ var Schema = /*#__PURE__*/ function() {
|
|
1486
1533
|
},
|
1487
1534
|
{
|
1488
1535
|
/**
|
1489
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null,
|
1536
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null,
|
1537
|
+
* the default value will be used.
|
1490
1538
|
*
|
1491
1539
|
* @example
|
1492
1540
|
* ```typescript
|
@@ -1515,8 +1563,9 @@ var Schema = /*#__PURE__*/ function() {
|
|
1515
1563
|
},
|
1516
1564
|
{
|
1517
1565
|
/**
|
1518
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
1519
|
-
* the custom schema your own way. Our API does not support passthrough?
|
1566
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
1567
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
1568
|
+
* No problem, you can use this function to customize the zod schema.
|
1520
1569
|
*
|
1521
1570
|
* @example
|
1522
1571
|
* ```typescript
|
@@ -1528,12 +1577,13 @@ var Schema = /*#__PURE__*/ function() {
|
|
1528
1577
|
*
|
1529
1578
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
1530
1579
|
*
|
1531
|
-
* console.log(errors);
|
1580
|
+
* console.log(errors);
|
1581
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
1532
1582
|
* ```
|
1533
1583
|
*
|
1534
1584
|
* @param callback - The callback that will be called to customize the schema.
|
1535
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want
|
1536
|
-
* to a string so you can save it for future runs.
|
1585
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want
|
1586
|
+
* to compile the underlying schema to a string so you can save it for future runs.
|
1537
1587
|
*
|
1538
1588
|
* @returns The schema.
|
1539
1589
|
*/ key: "extends",
|
@@ -1547,8 +1597,9 @@ var Schema = /*#__PURE__*/ function() {
|
|
1547
1597
|
},
|
1548
1598
|
{
|
1549
1599
|
/**
|
1550
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
1551
|
-
* control to add data cleaning for example, transforming the data
|
1600
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
1601
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data
|
1602
|
+
* and whatever. Another use case is when you want to return deeply nested recursive data.
|
1552
1603
|
* The schema maps to itself.
|
1553
1604
|
*
|
1554
1605
|
* @example
|
@@ -1587,8 +1638,10 @@ var Schema = /*#__PURE__*/ function() {
|
|
1587
1638
|
* ```
|
1588
1639
|
* @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
|
1589
1640
|
* @param options - Options for the toRepresentation function.
|
1590
|
-
* @param options.after - Whether the toRepresentationCallback should be called after the existing
|
1591
|
-
*
|
1641
|
+
* @param options.after - Whether the toRepresentationCallback should be called after the existing
|
1642
|
+
* toRepresentationCallback. Defaults to true.
|
1643
|
+
* @param options.before - Whether the toRepresentationCallback should be called before the existing
|
1644
|
+
* toRepresentationCallback. Defaults to true.
|
1592
1645
|
*
|
1593
1646
|
* @returns The schema with a new return type
|
1594
1647
|
*/ key: "toRepresentation",
|
@@ -1596,7 +1649,6 @@ var Schema = /*#__PURE__*/ function() {
|
|
1596
1649
|
if (this.__toRepresentation) {
|
1597
1650
|
var before = typeof (options === null || options === void 0 ? void 0 : options.before) === 'boolean' ? options.before : typeof (options === null || options === void 0 ? void 0 : options.after) === 'boolean' ? !options.after : true;
|
1598
1651
|
var existingToRepresentation = this.__toRepresentation;
|
1599
|
-
console.log('existing to representation', existingToRepresentation, before, options === null || options === void 0 ? void 0 : options.after);
|
1600
1652
|
this.__toRepresentation = function() {
|
1601
1653
|
var _ref = _async_to_generator(function(value) {
|
1602
1654
|
return _ts_generator(this, function(_state) {
|
@@ -1646,8 +1698,9 @@ var Schema = /*#__PURE__*/ function() {
|
|
1646
1698
|
},
|
1647
1699
|
{
|
1648
1700
|
/**
|
1649
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful
|
1650
|
-
* to a type that the schema adapter can understand. For example, you
|
1701
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful
|
1702
|
+
* when you want to transform the value to a type that the schema adapter can understand. For example, you
|
1703
|
+
* might want to transform a string to a date. This is the function you use.
|
1651
1704
|
*
|
1652
1705
|
* @example
|
1653
1706
|
* ```typescript
|
@@ -1709,8 +1762,9 @@ var Schema = /*#__PURE__*/ function() {
|
|
1709
1762
|
},
|
1710
1763
|
{
|
1711
1764
|
/**
|
1712
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a
|
1713
|
-
* here BEFORE the validation. This pretty much transforms the value
|
1765
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a
|
1766
|
+
* string, you can convert that string to a date here BEFORE the validation. This pretty much transforms the value
|
1767
|
+
* to a type that the schema adapter can understand.
|
1714
1768
|
*
|
1715
1769
|
* @example
|
1716
1770
|
* ```
|
@@ -1773,12 +1827,6 @@ var Schema = /*#__PURE__*/ function() {
|
|
1773
1827
|
_args[_key] = arguments[_key];
|
1774
1828
|
}
|
1775
1829
|
var result = new Schema();
|
1776
|
-
var adapterInstance = (0, _conf.getDefaultAdapter)();
|
1777
|
-
result.__transformedSchemas[adapterInstance.constructor.name] = {
|
1778
|
-
transformed: false,
|
1779
|
-
adapter: adapterInstance,
|
1780
|
-
schemas: []
|
1781
|
-
};
|
1782
1830
|
return result;
|
1783
1831
|
}
|
1784
1832
|
}
|