@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
package/src/schema/object.ts
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
import Schema from './schema';
|
2
|
-
import { getDefaultAdapter } from '../conf';
|
3
2
|
import {
|
4
3
|
defaultTransform,
|
5
4
|
defaultTransformToAdapter,
|
6
|
-
transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas
|
5
|
+
transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas
|
7
6
|
} from '../utils';
|
8
7
|
import { objectValidation } from '../validators/object';
|
9
8
|
import Validator from '../validators/utils';
|
@@ -26,11 +25,19 @@ export default class ObjectSchema<
|
|
26
25
|
representation: Record<any, any>;
|
27
26
|
},
|
28
27
|
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType,
|
29
|
-
TData extends Record<any, any> = Record<any, any
|
28
|
+
TData extends Record<any, any> = Record<any, any>
|
30
29
|
> extends Schema<TType, TDefinitions> {
|
31
30
|
protected __data: Record<any, Schema>;
|
32
31
|
protected __cachedDataAsEntries!: [string, Schema][];
|
33
32
|
|
33
|
+
protected __type: {
|
34
|
+
message: string;
|
35
|
+
check: (value: TType['input']) => boolean;
|
36
|
+
} = {
|
37
|
+
message: 'Invalid type',
|
38
|
+
check: (value) => typeof value === 'object' && value !== null
|
39
|
+
};
|
40
|
+
|
34
41
|
constructor(data: TData) {
|
35
42
|
super();
|
36
43
|
this.__data = data;
|
@@ -39,7 +46,7 @@ export default class ObjectSchema<
|
|
39
46
|
protected __retrieveDataAsEntriesAndCache(): [string, Schema][] {
|
40
47
|
const dataAsEntries = Array.isArray(this.__cachedDataAsEntries)
|
41
48
|
? this.__cachedDataAsEntries
|
42
|
-
:
|
49
|
+
: Object.entries(this.__data);
|
43
50
|
this.__cachedDataAsEntries = dataAsEntries;
|
44
51
|
return this.__cachedDataAsEntries;
|
45
52
|
}
|
@@ -57,10 +64,10 @@ export default class ObjectSchema<
|
|
57
64
|
// This is needed because we will create other objects based this one by reference
|
58
65
|
const transformedDataByKeys = {
|
59
66
|
transformed: {},
|
60
|
-
asString: {}
|
67
|
+
asString: {}
|
61
68
|
} as { transformed: Record<any, any>; asString: Record<any, string> };
|
62
69
|
const transformedDataByKeysArray: { transformed: Record<any, any>; asString: Record<any, string> }[] = [
|
63
|
-
transformedDataByKeys
|
70
|
+
transformedDataByKeys
|
64
71
|
];
|
65
72
|
|
66
73
|
let shouldValidateWithFallback = false;
|
@@ -90,7 +97,7 @@ export default class ObjectSchema<
|
|
90
97
|
if (transformedDataByKeysArray[indexOnTransformedDataByKeys] === undefined)
|
91
98
|
transformedDataByKeysArray[indexOnTransformedDataByKeys] = {
|
92
99
|
transformed: { ...transformedDataByKeys.transformed },
|
93
|
-
asString: { ...transformedDataByKeys.asString }
|
100
|
+
asString: { ...transformedDataByKeys.asString }
|
94
101
|
};
|
95
102
|
transformedDataByKeysArray[indexOnTransformedDataByKeys].transformed[key] =
|
96
103
|
transformedDataAndString[transformedDataIndex].transformed;
|
@@ -121,20 +128,22 @@ export default class ObjectSchema<
|
|
121
128
|
data: isStringVersion ? asString : transformed,
|
122
129
|
nullable: this.__nullable,
|
123
130
|
optional: this.__optional,
|
131
|
+
type: this.__type,
|
124
132
|
parsers: {
|
125
133
|
nullable: this.__nullable.allow,
|
126
|
-
optional: this.__optional.allow
|
134
|
+
optional: this.__optional.allow
|
127
135
|
}
|
128
136
|
}),
|
129
137
|
{},
|
130
138
|
{
|
131
|
-
shouldAddStringVersion: options.shouldAddStringVersion
|
139
|
+
shouldAddStringVersion: options.shouldAddStringVersion
|
132
140
|
}
|
133
141
|
)
|
134
142
|
)
|
135
143
|
)
|
136
144
|
).flat();
|
137
145
|
},
|
146
|
+
this,
|
138
147
|
this.__transformedSchemas,
|
139
148
|
options,
|
140
149
|
'object'
|
@@ -142,8 +151,10 @@ export default class ObjectSchema<
|
|
142
151
|
}
|
143
152
|
|
144
153
|
/**
|
145
|
-
* Transform the data to the representation without validating it. This is useful when you want to return a data
|
146
|
-
* from
|
154
|
+
* Transform the data to the representation without validating it. This is useful when you want to return a data
|
155
|
+
* from a query directly to the user. The core idea of this is that you can join the data from the database "by hand".
|
156
|
+
* In other words, you can do the joins by yourself directly on code. For more complex cases, this can be really
|
157
|
+
* helpful.
|
147
158
|
*
|
148
159
|
* @param value - The value to be transformed.
|
149
160
|
*
|
@@ -173,7 +184,8 @@ export default class ObjectSchema<
|
|
173
184
|
}
|
174
185
|
|
175
186
|
/**
|
176
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
187
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
188
|
+
* that is not supported by default by the schema adapter.
|
177
189
|
*
|
178
190
|
* @example
|
179
191
|
* ```typescript
|
@@ -185,7 +197,8 @@ export default class ObjectSchema<
|
|
185
197
|
*
|
186
198
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
187
199
|
*
|
188
|
-
*
|
200
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
201
|
+
* console.log(errors);
|
189
202
|
* ```
|
190
203
|
*
|
191
204
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -193,7 +206,13 @@ export default class ObjectSchema<
|
|
193
206
|
* @param options.isAsync - Whether the callback is async or not. Defaults to true.
|
194
207
|
*/
|
195
208
|
refine(
|
196
|
-
refinementCallback: (
|
209
|
+
refinementCallback: (
|
210
|
+
value: TType['input']
|
211
|
+
) =>
|
212
|
+
| Promise<void | undefined | { code: string; message: string }>
|
213
|
+
| void
|
214
|
+
| undefined
|
215
|
+
| { code: string; message: string }
|
197
216
|
) {
|
198
217
|
return super.refine(refinementCallback) as unknown as ObjectSchema<
|
199
218
|
{
|
@@ -202,14 +221,17 @@ export default class ObjectSchema<
|
|
202
221
|
internal: TType['internal'];
|
203
222
|
output: TType['output'];
|
204
223
|
representation: TType['representation'];
|
205
|
-
},
|
224
|
+
},
|
225
|
+
TDefinitions,
|
226
|
+
TData
|
206
227
|
>;
|
207
228
|
}
|
208
229
|
|
209
230
|
/**
|
210
|
-
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can
|
211
|
-
* This will allow you to pass `null` or `undefined` as a value on the
|
212
|
-
* This is useful for
|
231
|
+
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can
|
232
|
+
* pass `outputOnly` as `true` to this method. This will allow you to pass `null` or `undefined` as a value on the
|
233
|
+
* {@link Schema.data} method, but it will not allow the value to be `null` or `undefined`. This is useful for
|
234
|
+
* typing purposes
|
213
235
|
*
|
214
236
|
* @example
|
215
237
|
* ```typescript
|
@@ -237,35 +259,72 @@ export default class ObjectSchema<
|
|
237
259
|
* company: companySchema.optional({ outputOnly: true })
|
238
260
|
* });
|
239
261
|
*
|
240
|
-
*
|
241
|
-
* const
|
262
|
+
* // Will not allow the company to be null or undefined on a typing level.
|
263
|
+
* const { errors, parsed } = await userSchema.data({ id: 1, name: 'John Doe' });
|
264
|
+
*
|
265
|
+
* // Will allow the company to be null or undefined on a typing level
|
266
|
+
* const value = await userSchema.data({ id: 1, name: 'John Doe' });
|
242
267
|
* ```
|
243
268
|
*
|
244
269
|
* @returns - The schema we are working with.
|
245
270
|
*/
|
246
|
-
optional<TOutputOnly extends boolean = false>(options?: {
|
247
|
-
|
271
|
+
optional<TOutputOnly extends boolean = false>(options?: {
|
272
|
+
message?: string;
|
273
|
+
allow?: false;
|
274
|
+
outputOnly?: TOutputOnly;
|
275
|
+
}) {
|
276
|
+
return (options?.outputOnly ? this : super.optional(options)) as unknown as ObjectSchema<
|
277
|
+
TOutputOnly extends true
|
278
|
+
? {
|
279
|
+
input: TType['input'];
|
280
|
+
validate: TType['validate'];
|
281
|
+
internal: TType['internal'];
|
282
|
+
output: TType['output'] | undefined | null;
|
283
|
+
representation: TType['representation'];
|
284
|
+
}
|
285
|
+
: {
|
286
|
+
input: TType['input'] | undefined | null;
|
287
|
+
validate: TType['validate'] | undefined | null;
|
288
|
+
internal: TType['internal'] | undefined | null;
|
289
|
+
output: TType['output'] | undefined | null;
|
290
|
+
representation: TType['representation'] | undefined | null;
|
291
|
+
},
|
292
|
+
TDefinitions,
|
293
|
+
TData
|
294
|
+
>;
|
295
|
+
}
|
296
|
+
|
297
|
+
/**
|
298
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
299
|
+
*
|
300
|
+
* ```typescript
|
301
|
+
* p.datetime().optional({ message: 'This value should be defined', allow: false })
|
302
|
+
* ```
|
303
|
+
*
|
304
|
+
* @param options - The options of nonOptional function
|
305
|
+
* @param options.message - A custom message if the value is undefined.
|
306
|
+
*
|
307
|
+
* @returns - The schema.
|
308
|
+
*/
|
309
|
+
nonOptional(options?: { message: string }) {
|
310
|
+
return super.optional({
|
311
|
+
message: options?.message,
|
312
|
+
allow: false
|
313
|
+
}) as unknown as ObjectSchema<
|
248
314
|
{
|
249
315
|
input: TType['input'];
|
250
316
|
validate: TType['validate'];
|
251
317
|
internal: TType['internal'];
|
252
|
-
output: TType['output']
|
318
|
+
output: TType['output'];
|
253
319
|
representation: TType['representation'];
|
254
|
-
} :
|
255
|
-
{
|
256
|
-
input: TType['input'] | undefined | null;
|
257
|
-
validate: TType['validate'] | undefined | null;
|
258
|
-
internal: TType['internal'] | undefined | null;
|
259
|
-
output: TType['output'] | undefined | null;
|
260
|
-
representation: TType['representation'] | undefined | null;
|
261
320
|
},
|
262
|
-
TDefinitions
|
263
|
-
TData
|
321
|
+
TDefinitions
|
264
322
|
>;
|
265
323
|
}
|
266
324
|
|
267
325
|
/**
|
268
|
-
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
326
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
327
|
+
* is NULL by setting
|
269
328
|
* the { message: 'Your custom message', allow: false } on the options.
|
270
329
|
*
|
271
330
|
* @example
|
@@ -304,14 +363,45 @@ export default class ObjectSchema<
|
|
304
363
|
}
|
305
364
|
|
306
365
|
/**
|
307
|
-
*
|
308
|
-
*
|
366
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
367
|
+
*
|
368
|
+
* ```typescript
|
369
|
+
* p.datetime().nullable({ message: 'This value cannot be null', allow: false })
|
370
|
+
* ```
|
371
|
+
*
|
372
|
+
* @param options - The options of nonNullable function
|
373
|
+
* @param options.message - A custom message if the value is null.
|
374
|
+
*
|
375
|
+
* @returns - The schema.
|
376
|
+
*/
|
377
|
+
nonNullable(options?: { message: string }) {
|
378
|
+
return super.nullable({
|
379
|
+
message: options?.message || '',
|
380
|
+
allow: false
|
381
|
+
}) as unknown as ObjectSchema<
|
382
|
+
{
|
383
|
+
input: TType['input'];
|
384
|
+
validate: TType['validate'];
|
385
|
+
internal: TType['internal'];
|
386
|
+
output: TType['output'];
|
387
|
+
representation: TType['representation'];
|
388
|
+
},
|
389
|
+
TDefinitions
|
390
|
+
>;
|
391
|
+
}
|
392
|
+
|
393
|
+
/**
|
394
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
395
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
309
396
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
310
397
|
* function. This will remove the value from the representation of the schema.
|
311
398
|
*
|
312
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
313
|
-
*
|
314
|
-
*
|
399
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
400
|
+
* function.
|
401
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
402
|
+
* as true.
|
403
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument
|
404
|
+
* `toRepresentation` as true as well.
|
315
405
|
*
|
316
406
|
* @example
|
317
407
|
* ```typescript
|
@@ -333,16 +423,19 @@ export default class ObjectSchema<
|
|
333
423
|
* ```
|
334
424
|
*
|
335
425
|
*
|
336
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
337
|
-
*
|
338
|
-
*
|
426
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
427
|
+
* the {@link data} function.
|
428
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
429
|
+
* as true.
|
430
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument
|
431
|
+
* `toRepresentation` as true as well.
|
339
432
|
*
|
340
433
|
* @returns The schema.
|
341
434
|
*/
|
342
435
|
omit<
|
343
436
|
TToInternal extends boolean,
|
344
437
|
TToRepresentation extends boolean = boolean extends TToInternal ? true : false
|
345
|
-
>(args?: { toInternal?: TToInternal
|
438
|
+
>(args?: { toInternal?: TToInternal; toRepresentation?: TToRepresentation }) {
|
346
439
|
return super.omit(args) as unknown as ObjectSchema<
|
347
440
|
{
|
348
441
|
input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
|
@@ -357,9 +450,9 @@ export default class ObjectSchema<
|
|
357
450
|
}
|
358
451
|
|
359
452
|
/**
|
360
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
361
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
362
|
-
* of the schema after the save operation.
|
453
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
454
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
455
|
+
* the correct type of the schema after the save operation.
|
363
456
|
*
|
364
457
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
365
458
|
*
|
@@ -414,9 +507,9 @@ export default class ObjectSchema<
|
|
414
507
|
>;
|
415
508
|
}
|
416
509
|
|
417
|
-
|
418
510
|
/**
|
419
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
511
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
512
|
+
* value will be used.
|
420
513
|
*
|
421
514
|
* @example
|
422
515
|
* ```typescript
|
@@ -446,8 +539,9 @@ export default class ObjectSchema<
|
|
446
539
|
}
|
447
540
|
|
448
541
|
/**
|
449
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
450
|
-
* the custom schema your own way. Our API does not support passthrough?
|
542
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
543
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
544
|
+
* No problem, you can use this function to customize the zod schema.
|
451
545
|
*
|
452
546
|
* @example
|
453
547
|
* ```typescript
|
@@ -459,12 +553,13 @@ export default class ObjectSchema<
|
|
459
553
|
*
|
460
554
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
461
555
|
*
|
462
|
-
*
|
556
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
557
|
+
* console.log(errors);
|
463
558
|
* ```
|
464
559
|
*
|
465
560
|
* @param callback - The callback that will be called to customize the schema.
|
466
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
467
|
-
* to a string so you can save it for future runs.
|
561
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
562
|
+
* compile the underlying schema to a string so you can save it for future runs.
|
468
563
|
*
|
469
564
|
* @returns The schema.
|
470
565
|
*/
|
@@ -478,8 +573,9 @@ export default class ObjectSchema<
|
|
478
573
|
}
|
479
574
|
|
480
575
|
/**
|
481
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
482
|
-
* control to add data cleaning for example, transforming the data and
|
576
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
577
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
578
|
+
* whatever. Another use case is when you want to return deeply nested recursive data.
|
483
579
|
* The schema maps to itself.
|
484
580
|
*
|
485
581
|
* @example
|
@@ -518,8 +614,10 @@ export default class ObjectSchema<
|
|
518
614
|
* ```
|
519
615
|
* @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
|
520
616
|
* @param options - Options for the toRepresentation function.
|
521
|
-
* @param options.after - Whether the toRepresentationCallback should be called after the existing
|
522
|
-
*
|
617
|
+
* @param options.after - Whether the toRepresentationCallback should be called after the existing
|
618
|
+
* toRepresentationCallback. Defaults to true.
|
619
|
+
* @param options.before - Whether the toRepresentationCallback should be called before the existing
|
620
|
+
* toRepresentationCallback. Defaults to true.
|
523
621
|
*
|
524
622
|
* @returns The schema with a new return type
|
525
623
|
*/
|
@@ -544,8 +642,9 @@ export default class ObjectSchema<
|
|
544
642
|
}
|
545
643
|
|
546
644
|
/**
|
547
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
548
|
-
* to a type that the schema adapter can understand. For example, you might want
|
645
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
646
|
+
* you want to transform the value to a type that the schema adapter can understand. For example, you might want
|
647
|
+
* to transform a string to a date. This is the function you use.
|
549
648
|
*
|
550
649
|
* @example
|
551
650
|
* ```typescript
|
@@ -590,8 +689,9 @@ export default class ObjectSchema<
|
|
590
689
|
}
|
591
690
|
|
592
691
|
/**
|
593
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
594
|
-
* here BEFORE the validation. This pretty much transforms the value to a type
|
692
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
693
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
|
694
|
+
* that the schema adapter can understand.
|
595
695
|
*
|
596
696
|
* @example
|
597
697
|
* ```
|
@@ -648,10 +748,11 @@ export default class ObjectSchema<
|
|
648
748
|
*/
|
649
749
|
removeExtraneous() {
|
650
750
|
this.__parsers.medium.set('removeExtraneous', (value) => {
|
651
|
-
if (typeof value !== 'object' || value === null)
|
652
|
-
|
653
|
-
|
654
|
-
|
751
|
+
if (typeof value !== 'object' || value === null)
|
752
|
+
return {
|
753
|
+
value,
|
754
|
+
preventNextParsers: false
|
755
|
+
};
|
655
756
|
|
656
757
|
const valueKeys = Object.keys(value);
|
657
758
|
for (const key of valueKeys) {
|
@@ -661,7 +762,7 @@ export default class ObjectSchema<
|
|
661
762
|
|
662
763
|
return {
|
663
764
|
value,
|
664
|
-
preventNextParsers: false
|
765
|
+
preventNextParsers: false
|
665
766
|
};
|
666
767
|
});
|
667
768
|
|
@@ -670,7 +771,7 @@ export default class ObjectSchema<
|
|
670
771
|
|
671
772
|
static new<
|
672
773
|
TData extends Record<any, Schema<any, TDefinitions>>,
|
673
|
-
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
774
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
674
775
|
>(data: TData) {
|
675
776
|
const returnValue = new ObjectSchema<
|
676
777
|
{
|
@@ -684,14 +785,6 @@ export default class ObjectSchema<
|
|
684
785
|
TData
|
685
786
|
>(data);
|
686
787
|
|
687
|
-
const adapterInstance = getDefaultAdapter();
|
688
|
-
|
689
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
690
|
-
transformed: false,
|
691
|
-
adapter: adapterInstance,
|
692
|
-
schemas: [],
|
693
|
-
};
|
694
|
-
|
695
788
|
return returnValue;
|
696
789
|
}
|
697
790
|
}
|