@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
@@ -1,11 +1,15 @@
|
|
1
1
|
import { getDefaultAdapter } from '../conf';
|
2
2
|
import { formatErrorFromParseMethod } from '../utils';
|
3
3
|
export default class Schema {
|
4
|
-
// Those functions will assume control of the validation process on adapters, instead of the schema.
|
5
|
-
//
|
6
|
-
//
|
7
|
-
//
|
8
|
-
//
|
4
|
+
// Those functions will assume control of the validation process on adapters, instead of the schema.
|
5
|
+
// Why this is used? The idea is that the Schema has NO idea
|
6
|
+
// that one of it's children might be an UnionSchema for example. The adapter might not support unions,
|
7
|
+
// so then we give control to the union. The parent schema will already have an array of translated
|
8
|
+
// adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number
|
9
|
+
// and one for the value as String. Of course this gets multiplied. So if we have a union with Number and String.
|
10
|
+
// We should take those two schemas from the array and validate them individually. This logic is
|
11
|
+
// handled by the union schema. If we have an intersection type for example, instead of validating
|
12
|
+
// One schema OR the other, we validate one schema AND the other. This will be handled
|
9
13
|
// by the schema that contains that intersection logic.
|
10
14
|
__beforeValidationCallbacks = new Map();
|
11
15
|
__cachedGetParent;
|
@@ -15,7 +19,7 @@ export default class Schema {
|
|
15
19
|
get __getParent() {
|
16
20
|
return this.__cachedGetParent;
|
17
21
|
}
|
18
|
-
__alreadyAppliedModel
|
22
|
+
__alreadyAppliedModel;
|
19
23
|
__runBeforeParseAndData;
|
20
24
|
__rootFallbacksValidator;
|
21
25
|
__saveCallback;
|
@@ -45,21 +49,33 @@ export default class Schema {
|
|
45
49
|
message: 'Invalid type',
|
46
50
|
check: ()=>true
|
47
51
|
};
|
52
|
+
__getDefaultTransformedSchemas() {
|
53
|
+
const adapterInstance = getDefaultAdapter();
|
54
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
55
|
+
if (this.__transformedSchemas[adapterInstance.constructor.name] === undefined) this.__transformedSchemas[adapterInstance.constructor.name] = {
|
56
|
+
transformed: false,
|
57
|
+
adapter: adapterInstance,
|
58
|
+
schemas: []
|
59
|
+
};
|
60
|
+
}
|
48
61
|
/**
|
49
|
-
* This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
|
62
|
+
* This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
|
63
|
+
* This is nice because we can support things that the schema adapter is not able to support by default.
|
50
64
|
*
|
51
65
|
* @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors.
|
52
66
|
* @param path - The path of the error.
|
53
67
|
* @param parseResult - The result of the parse method.
|
54
68
|
*/ async __validateByFallbacks(path, parseResult, options) {
|
55
69
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
56
|
-
if (this.__rootFallbacksValidator) return this.__rootFallbacksValidator.validate(options.errorsAsHashedSet
|
70
|
+
if (this.__rootFallbacksValidator) return this.__rootFallbacksValidator.validate(options.errorsAsHashedSet, path, parseResult, options);
|
57
71
|
return parseResult;
|
58
72
|
}
|
59
73
|
/**
|
60
|
-
* This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
|
61
|
-
*
|
62
|
-
*
|
74
|
+
* This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
|
75
|
+
* that data.
|
76
|
+
* So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate
|
77
|
+
* from the adapter we validate with the fallbacks so we can do all of the extra validations not handled by
|
78
|
+
* the adapter.
|
63
79
|
*
|
64
80
|
* @param value - The value to be validated.
|
65
81
|
* @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors on the validator.
|
@@ -80,11 +96,12 @@ export default class Schema {
|
|
80
96
|
const adapterParseResult = await fieldAdapter.parse(adapter, adapter.field, schema.transformed, value, options.args);
|
81
97
|
parseResult.parsed = adapterParseResult.parsed;
|
82
98
|
if (adapterParseResult.errors) {
|
83
|
-
if (Array.isArray(adapterParseResult.errors)) parseResult.errors = await Promise.all(adapterParseResult.errors.map(async (error)=>formatErrorFromParseMethod(adapter, fieldAdapter, error, path, options.errorsAsHashedSet || new Set())));
|
99
|
+
if (Array.isArray(adapterParseResult.errors)) parseResult.errors = await Promise.all(adapterParseResult.errors.map(async (error)=>formatErrorFromParseMethod(adapter, fieldAdapter, error, value, schema.transformed, path, options.errorsAsHashedSet || new Set())));
|
84
100
|
else parseResult.errors = [
|
85
|
-
await formatErrorFromParseMethod(adapter, fieldAdapter, parseResult.errors, path, options.errorsAsHashedSet || new Set())
|
101
|
+
await formatErrorFromParseMethod(adapter, fieldAdapter, parseResult.errors, value, schema.transformed, path, options.errorsAsHashedSet || new Set())
|
86
102
|
];
|
87
103
|
}
|
104
|
+
parseResult.errors = parseResult.errors.filter((error)=>typeof error !== 'undefined');
|
88
105
|
return parseResult;
|
89
106
|
}
|
90
107
|
// eslint-disable-next-line ts/require-await
|
@@ -121,11 +138,15 @@ export default class Schema {
|
|
121
138
|
return value;
|
122
139
|
}
|
123
140
|
async __parse(value, path = [], options) {
|
141
|
+
this.__getDefaultTransformedSchemas();
|
124
142
|
if (typeof this.__runBeforeParseAndData === 'function') await this.__runBeforeParseAndData(this);
|
125
|
-
// This is used to run the toInternal command. If we didn't do this, we would need to parse through all of
|
126
|
-
// from the leafs (ObjectSchemas) to the root schema. This is not
|
127
|
-
//
|
128
|
-
//
|
143
|
+
// This is used to run the toInternal command. If we didn't do this, we would need to parse through all of
|
144
|
+
// the schemas to run the toInternal command, from the leafs (ObjectSchemas) to the root schema. This is not
|
145
|
+
// a good idea, so what we do is that during validation the leafs attach a function to the
|
146
|
+
// options.toInternalToBubbleUp like
|
147
|
+
// `options.toInternalToBubbleUp.push(async () => (value[key] = await (schema as any).__toInternal(parsed)));``
|
148
|
+
// This way, when the root schema finishes the validation, it will run all of the functions in the
|
149
|
+
// toInternalToBubbleUp array, modifying the parsed value.
|
129
150
|
const shouldRunToInternalToBubbleUp = options.toInternalToBubbleUp === undefined;
|
130
151
|
if (shouldRunToInternalToBubbleUp) options.toInternalToBubbleUp = [];
|
131
152
|
if (options.errorsAsHashedSet instanceof Set === false) options.errorsAsHashedSet = new Set();
|
@@ -138,25 +159,31 @@ export default class Schema {
|
|
138
159
|
errors: [],
|
139
160
|
parsed: value
|
140
161
|
};
|
141
|
-
|
142
|
-
|
162
|
+
value = await this.__parsersToTransformValue(value, this.__parsers._fallbacks);
|
163
|
+
if (options.appendFallbacksBeforeAdapterValidation === undefined) options.appendFallbacksBeforeAdapterValidation = (schema, name, callback)=>{
|
164
|
+
// We just need this if the union adapter is net defined but the parent is not of the same type.
|
165
|
+
// For example, it's a union child of o object schema
|
166
|
+
if (this !== schema) this.__beforeValidationCallbacks.set(name, callback);
|
143
167
|
};
|
144
168
|
if (this.__transformedSchemas[options.schemaAdapter?.constructor.name || getDefaultAdapter().constructor.name].transformed === false) await this.__transformToAdapter(options);
|
145
|
-
value = await this.__parsersToTransformValue(value, this.__parsers._fallbacks);
|
146
169
|
const adapterToUse = options.schemaAdapter ? options.schemaAdapter : Object.values(this.__transformedSchemas)[0].adapter;
|
147
170
|
const parsedResultsAfterFallbacks = await this.__validateByFallbacks(path, {
|
148
|
-
errors:
|
171
|
+
errors: parseResult.errors,
|
149
172
|
parsed: value
|
150
173
|
}, options);
|
151
174
|
parseResult.parsed = parsedResultsAfterFallbacks.parsed;
|
152
175
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
153
176
|
parseResult.errors = (parseResult.errors || []).concat(parsedResultsAfterFallbacks.errors || []);
|
154
|
-
// With this, the children takes control of validating by the adapter. For example on a union schema we
|
177
|
+
// With this, the children takes control of validating by the adapter. For example on a union schema we
|
178
|
+
// want to validate all of the schemas and choose the one that has no errors.
|
155
179
|
if (this.__beforeValidationCallbacks.size > 0) {
|
156
180
|
for (const callback of this.__beforeValidationCallbacks.values()){
|
157
181
|
const parsedValuesAfterValidationCallbacks = await callback(adapterToUse, adapterToUse[schemaAdapterFieldType], this, this.__transformedSchemas[adapterToUse.constructor.name].schemas, value, path, options);
|
158
182
|
parseResult.parsed = parsedValuesAfterValidationCallbacks.parsed;
|
159
|
-
parseResult.errors = parsedValuesAfterValidationCallbacks.errors
|
183
|
+
parseResult.errors = Array.isArray(parseResult.errors) && Array.isArray(parsedValuesAfterValidationCallbacks.errors) ? [
|
184
|
+
...parseResult.errors,
|
185
|
+
...parsedValuesAfterValidationCallbacks.errors
|
186
|
+
] : Array.isArray(parseResult.errors) ? parseResult.errors : parsedValuesAfterValidationCallbacks.errors;
|
160
187
|
}
|
161
188
|
} else {
|
162
189
|
const parsedValuesAfterValidatingByAdapter = await this.__validateByAdapter(adapterToUse, adapterToUse[schemaAdapterFieldType], this.__transformedSchemas[adapterToUse.constructor.name].schemas[0], value, path, options);
|
@@ -164,9 +191,8 @@ export default class Schema {
|
|
164
191
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
165
192
|
parseResult.errors = (parseResult.errors || []).concat(parsedValuesAfterValidatingByAdapter.errors);
|
166
193
|
}
|
167
|
-
const doesNotHaveErrors = !Array.isArray(parseResult.errors) || parseResult.errors.length === 0;
|
168
194
|
const hasToInternalCallback = typeof this.__toInternal === 'function';
|
169
|
-
const shouldCallToInternalDuringParse =
|
195
|
+
const shouldCallToInternalDuringParse = hasToInternalCallback && (options.toInternalToBubbleUp?.length === 0 || Array.isArray(options.toInternalToBubbleUp) === false);
|
170
196
|
// eslint-disable-next-line ts/no-unnecessary-condition
|
171
197
|
const hasNoErrors = parseResult.errors === undefined || (parseResult.errors || []).length === 0;
|
172
198
|
await Promise.all(this.__refinements.map(async (refinement)=>{
|
@@ -176,6 +202,7 @@ export default class Schema {
|
|
176
202
|
isValid: false,
|
177
203
|
code: errorOrNothing.code,
|
178
204
|
message: errorOrNothing.message,
|
205
|
+
received: parseResult.parsed,
|
179
206
|
path
|
180
207
|
});
|
181
208
|
}));
|
@@ -184,7 +211,8 @@ export default class Schema {
|
|
184
211
|
return parseResult;
|
185
212
|
}
|
186
213
|
/**
|
187
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
214
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
215
|
+
* that is not supported by default by the schema adapter.
|
188
216
|
*
|
189
217
|
* @example
|
190
218
|
* ```typescript
|
@@ -196,7 +224,8 @@ export default class Schema {
|
|
196
224
|
*
|
197
225
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
198
226
|
*
|
199
|
-
* console.log(errors);
|
227
|
+
* console.log(errors);
|
228
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
200
229
|
* ```
|
201
230
|
*
|
202
231
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -237,8 +266,8 @@ export default class Schema {
|
|
237
266
|
return this;
|
238
267
|
}
|
239
268
|
/**
|
240
|
-
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when
|
241
|
-
* the { message: 'Your custom message', allow: false } on the options.
|
269
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when
|
270
|
+
* the value is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
242
271
|
*
|
243
272
|
* @example
|
244
273
|
* ```typescript
|
@@ -270,8 +299,8 @@ export default class Schema {
|
|
270
299
|
/**
|
271
300
|
* Appends a custom schema to the schema, this way it will bypass the creation of the schema in runtime.
|
272
301
|
*
|
273
|
-
* By default when validating, on the first validation we create the schema. Just during the first validation.
|
274
|
-
* so you can speed up the validation process.
|
302
|
+
* By default when validating, on the first validation we create the schema. Just during the first validation.
|
303
|
+
* With this function, you bypass that, so you can speed up the validation process.
|
275
304
|
*
|
276
305
|
* @example
|
277
306
|
* ```typescript
|
@@ -300,14 +329,15 @@ export default class Schema {
|
|
300
329
|
return this;
|
301
330
|
}
|
302
331
|
/**
|
303
|
-
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
304
|
-
* otherwise it will set the value to undefined after it's validated.
|
332
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
333
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
305
334
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
306
335
|
* function. This will remove the value from the representation of the schema.
|
307
336
|
*
|
308
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
309
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
310
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
337
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
338
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
339
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
340
|
+
* the argument `toRepresentation` as true as well.
|
311
341
|
*
|
312
342
|
* @example
|
313
343
|
* ```typescript
|
@@ -329,9 +359,11 @@ export default class Schema {
|
|
329
359
|
* ```
|
330
360
|
*
|
331
361
|
*
|
332
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
333
|
-
*
|
334
|
-
*
|
362
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
363
|
+
* the {@link data} function.
|
364
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
365
|
+
* as true. Then if you still want to remove the value from the representation, you will need to pass the
|
366
|
+
* argument `toRepresentation` as true as well.
|
335
367
|
*
|
336
368
|
* @returns The schema.
|
337
369
|
*/ omit(args) {
|
@@ -360,9 +392,9 @@ export default class Schema {
|
|
360
392
|
return this;
|
361
393
|
}
|
362
394
|
/**
|
363
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
364
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
365
|
-
* of the schema after the save operation.
|
395
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
396
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
397
|
+
* the correct type of the schema after the save operation.
|
366
398
|
*
|
367
399
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
368
400
|
*
|
@@ -408,10 +440,11 @@ export default class Schema {
|
|
408
440
|
* This function is used to validate the schema and save the value to the database. It is used in
|
409
441
|
* conjunction with the {@link onSave} function.
|
410
442
|
*
|
411
|
-
* Different from other validation libraries, palmares schemas is aware that you want to save. On your
|
412
|
-
* we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
|
413
|
-
* will return an object with the property `save` or the `errors`. If the errors are present,
|
414
|
-
* to the user. If the save property is present, you can use to save the value to an
|
443
|
+
* Different from other validation libraries, palmares schemas is aware that you want to save. On your
|
444
|
+
* routes/functions we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
|
445
|
+
* this function by default will return an object with the property `save` or the `errors`. If the errors are present,
|
446
|
+
* you can return the errors to the user. If the save property is present, you can use to save the value to an
|
447
|
+
* external source. e.g. a database.
|
415
448
|
*
|
416
449
|
* @example
|
417
450
|
* ```typescript
|
@@ -524,6 +557,7 @@ export default class Schema {
|
|
524
557
|
* });
|
525
558
|
* ```
|
526
559
|
*/ async data(value) {
|
560
|
+
this.__getDefaultTransformedSchemas();
|
527
561
|
if (typeof this.__runBeforeParseAndData === 'function') await this.__runBeforeParseAndData(this);
|
528
562
|
value = await this.__parsersToTransformValue(value);
|
529
563
|
if (this.__toRepresentation) value = await Promise.resolve(this.__toRepresentation(value));
|
@@ -536,7 +570,8 @@ export default class Schema {
|
|
536
570
|
return this;
|
537
571
|
}
|
538
572
|
/**
|
539
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null,
|
573
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null,
|
574
|
+
* the default value will be used.
|
540
575
|
*
|
541
576
|
* @example
|
542
577
|
* ```typescript
|
@@ -555,8 +590,9 @@ export default class Schema {
|
|
555
590
|
return this;
|
556
591
|
}
|
557
592
|
/**
|
558
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
559
|
-
* the custom schema your own way. Our API does not support passthrough?
|
593
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
594
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
595
|
+
* No problem, you can use this function to customize the zod schema.
|
560
596
|
*
|
561
597
|
* @example
|
562
598
|
* ```typescript
|
@@ -568,12 +604,13 @@ export default class Schema {
|
|
568
604
|
*
|
569
605
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
570
606
|
*
|
571
|
-
* console.log(errors);
|
607
|
+
* console.log(errors);
|
608
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
572
609
|
* ```
|
573
610
|
*
|
574
611
|
* @param callback - The callback that will be called to customize the schema.
|
575
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want
|
576
|
-
* to a string so you can save it for future runs.
|
612
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want
|
613
|
+
* to compile the underlying schema to a string so you can save it for future runs.
|
577
614
|
*
|
578
615
|
* @returns The schema.
|
579
616
|
*/ extends(callback, toStringCallback) {
|
@@ -584,8 +621,9 @@ export default class Schema {
|
|
584
621
|
return this;
|
585
622
|
}
|
586
623
|
/**
|
587
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
588
|
-
* control to add data cleaning for example, transforming the data
|
624
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
625
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data
|
626
|
+
* and whatever. Another use case is when you want to return deeply nested recursive data.
|
589
627
|
* The schema maps to itself.
|
590
628
|
*
|
591
629
|
* @example
|
@@ -624,15 +662,16 @@ export default class Schema {
|
|
624
662
|
* ```
|
625
663
|
* @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
|
626
664
|
* @param options - Options for the toRepresentation function.
|
627
|
-
* @param options.after - Whether the toRepresentationCallback should be called after the existing
|
628
|
-
*
|
665
|
+
* @param options.after - Whether the toRepresentationCallback should be called after the existing
|
666
|
+
* toRepresentationCallback. Defaults to true.
|
667
|
+
* @param options.before - Whether the toRepresentationCallback should be called before the existing
|
668
|
+
* toRepresentationCallback. Defaults to true.
|
629
669
|
*
|
630
670
|
* @returns The schema with a new return type
|
631
671
|
*/ toRepresentation(toRepresentationCallback, options) {
|
632
672
|
if (this.__toRepresentation) {
|
633
673
|
const before = typeof options?.before === 'boolean' ? options.before : typeof options?.after === 'boolean' ? !options.after : true;
|
634
674
|
const existingToRepresentation = this.__toRepresentation;
|
635
|
-
console.log('existing to representation', existingToRepresentation, before, options?.after);
|
636
675
|
this.__toRepresentation = async (value)=>{
|
637
676
|
if (before) return toRepresentationCallback(await existingToRepresentation(value));
|
638
677
|
else return existingToRepresentation(await toRepresentationCallback(value));
|
@@ -641,8 +680,9 @@ export default class Schema {
|
|
641
680
|
return this;
|
642
681
|
}
|
643
682
|
/**
|
644
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful
|
645
|
-
* to a type that the schema adapter can understand. For example, you
|
683
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful
|
684
|
+
* when you want to transform the value to a type that the schema adapter can understand. For example, you
|
685
|
+
* might want to transform a string to a date. This is the function you use.
|
646
686
|
*
|
647
687
|
* @example
|
648
688
|
* ```typescript
|
@@ -682,8 +722,9 @@ export default class Schema {
|
|
682
722
|
return this;
|
683
723
|
}
|
684
724
|
/**
|
685
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a
|
686
|
-
* here BEFORE the validation. This pretty much transforms the value
|
725
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a
|
726
|
+
* string, you can convert that string to a date here BEFORE the validation. This pretty much transforms the value
|
727
|
+
* to a type that the schema adapter can understand.
|
687
728
|
*
|
688
729
|
* @example
|
689
730
|
* ```
|
@@ -716,12 +757,6 @@ export default class Schema {
|
|
716
757
|
}
|
717
758
|
static new(..._args) {
|
718
759
|
const result = new Schema();
|
719
|
-
const adapterInstance = getDefaultAdapter();
|
720
|
-
result.__transformedSchemas[adapterInstance.constructor.name] = {
|
721
|
-
transformed: false,
|
722
|
-
adapter: adapterInstance,
|
723
|
-
schemas: []
|
724
|
-
};
|
725
760
|
return result;
|
726
761
|
}
|
727
762
|
}
|