@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
package/src/schema/array.ts
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import Schema from './schema';
|
2
|
-
import { getDefaultAdapter } from '../conf';
|
3
2
|
import {
|
4
3
|
defaultTransform,
|
5
4
|
defaultTransformToAdapter,
|
6
|
-
|
5
|
+
shouldRunDataOnComplexSchemas,
|
6
|
+
transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas
|
7
7
|
} from '../utils';
|
8
8
|
import { arrayValidation, maxLength, minLength, nonEmpty } from '../validators/array';
|
9
9
|
import { nullable, optional } from '../validators/schema';
|
@@ -26,9 +26,17 @@ export default class ArraySchema<
|
|
26
26
|
validate: any[];
|
27
27
|
},
|
28
28
|
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType,
|
29
|
-
TSchemas extends readonly [Schema, ...Schema[]] | [Schema
|
29
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [[Schema]] = [[Schema]]
|
30
30
|
> extends Schema<TType, TDefinitions> {
|
31
|
-
protected __schemas: readonly [Schema, ...Schema[]] | [Schema
|
31
|
+
protected __schemas: readonly [Schema, ...Schema[]] | [[Schema]];
|
32
|
+
|
33
|
+
protected __type: {
|
34
|
+
message: string;
|
35
|
+
check: (value: TType['input']) => boolean;
|
36
|
+
} = {
|
37
|
+
message: 'Invalid type',
|
38
|
+
check: (value) => Array.isArray(value)
|
39
|
+
};
|
32
40
|
|
33
41
|
protected __minLength!: {
|
34
42
|
value: number;
|
@@ -56,11 +64,11 @@ export default class ArraySchema<
|
|
56
64
|
async (adapter) => {
|
57
65
|
const schemas = Array.isArray(this.__schemas[0]) ? this.__schemas[0] : this.__schemas;
|
58
66
|
const transformedSchemasAsString: string[] = [];
|
59
|
-
const transformedSchemas
|
67
|
+
const transformedSchemas = [] as unknown as [any, ...any[]] | [any];
|
60
68
|
let shouldBeHandledByFallback = false;
|
61
69
|
|
62
70
|
await Promise.all(
|
63
|
-
(schemas as Schema[]).map(async (schema) => {
|
71
|
+
(schemas as unknown as Schema[]).map(async (schema) => {
|
64
72
|
const [transformedData, shouldAddFallbackValidationForThisSchema] =
|
65
73
|
await transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas(schema, options);
|
66
74
|
|
@@ -79,7 +87,7 @@ export default class ArraySchema<
|
|
79
87
|
this,
|
80
88
|
arrayValidation(
|
81
89
|
Array.isArray(this.__schemas[0]) === false,
|
82
|
-
(Array.isArray(this.__schemas[0]) ? this.__schemas[0] : this.__schemas) as Schema<any, any>[]
|
90
|
+
(Array.isArray(this.__schemas[0]) ? this.__schemas[0] : this.__schemas) as unknown as Schema<any, any>[]
|
83
91
|
)
|
84
92
|
);
|
85
93
|
|
@@ -95,9 +103,11 @@ export default class ArraySchema<
|
|
95
103
|
maxLength: this.__maxLength,
|
96
104
|
minLength: this.__minLength,
|
97
105
|
nonEmpty: this.__nonEmpty,
|
106
|
+
schemas: transformedSchemas,
|
107
|
+
type: this.__type,
|
98
108
|
parsers: {
|
99
109
|
nullable: this.__nullable.allow,
|
100
|
-
optional: this.__optional.allow
|
110
|
+
optional: this.__optional.allow
|
101
111
|
}
|
102
112
|
}),
|
103
113
|
{
|
@@ -105,15 +115,16 @@ export default class ArraySchema<
|
|
105
115
|
nullable,
|
106
116
|
minLength,
|
107
117
|
maxLength,
|
108
|
-
nonEmpty
|
118
|
+
nonEmpty
|
109
119
|
},
|
110
120
|
{
|
111
121
|
shouldAddStringVersion: options.shouldAddStringVersion,
|
112
122
|
// eslint-disable-next-line ts/require-await
|
113
|
-
fallbackIfNotSupported: async () => []
|
123
|
+
fallbackIfNotSupported: async () => []
|
114
124
|
}
|
115
125
|
);
|
116
126
|
},
|
127
|
+
this,
|
117
128
|
this.__transformedSchemas,
|
118
129
|
options,
|
119
130
|
'array'
|
@@ -121,7 +132,8 @@ export default class ArraySchema<
|
|
121
132
|
}
|
122
133
|
|
123
134
|
/**
|
124
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
135
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
136
|
+
* is not supported by default by the schema adapter.
|
125
137
|
*
|
126
138
|
* @example
|
127
139
|
* ```typescript
|
@@ -133,7 +145,8 @@ export default class ArraySchema<
|
|
133
145
|
*
|
134
146
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
135
147
|
*
|
136
|
-
*
|
148
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
149
|
+
* console.log(errors);
|
137
150
|
* ```
|
138
151
|
*
|
139
152
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -141,7 +154,13 @@ export default class ArraySchema<
|
|
141
154
|
* @param options.isAsync - Whether the callback is async or not. Defaults to true.
|
142
155
|
*/
|
143
156
|
refine(
|
144
|
-
refinementCallback: (
|
157
|
+
refinementCallback: (
|
158
|
+
value: TType['input']
|
159
|
+
) =>
|
160
|
+
| Promise<void | undefined | { code: string; message: string }>
|
161
|
+
| void
|
162
|
+
| undefined
|
163
|
+
| { code: string; message: string }
|
145
164
|
) {
|
146
165
|
return super.refine(refinementCallback) as unknown as ArraySchema<
|
147
166
|
{
|
@@ -157,9 +176,10 @@ export default class ArraySchema<
|
|
157
176
|
}
|
158
177
|
|
159
178
|
/**
|
160
|
-
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can
|
161
|
-
*
|
162
|
-
* This
|
179
|
+
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can
|
180
|
+
* pass `outputOnly` as `true` to this method.
|
181
|
+
* This will allow you to pass `null` or `undefined` as a value on the {@link Schema.data} method, but it will not
|
182
|
+
* allow the value to be `null` or `undefined`. This is useful for typing purposes.
|
163
183
|
*
|
164
184
|
* @example
|
165
185
|
* ```typescript
|
@@ -186,36 +206,71 @@ export default class ArraySchema<
|
|
186
206
|
* company: companySchema.optional({ outputOnly: true })
|
187
207
|
* });
|
188
208
|
*
|
189
|
-
*
|
190
|
-
* const
|
209
|
+
* // Will not allow the company to be null or undefined on a typing level.
|
210
|
+
* const { errors, parsed } = await userSchema.data({ id: 1, name: 'John Doe' });
|
211
|
+
* // Will allow the company to be null or undefined on a typing level
|
212
|
+
* const value = await userSchema.data({ id: 1, name: 'John Doe' });
|
191
213
|
* ```
|
192
214
|
*
|
193
215
|
* @returns - The schema we are working with.
|
194
216
|
*/
|
195
|
-
optional<TOutputOnly extends boolean = false>(options?: {
|
196
|
-
|
217
|
+
optional<TOutputOnly extends boolean = false>(options?: {
|
218
|
+
message?: string;
|
219
|
+
allow?: false;
|
220
|
+
outputOnly?: TOutputOnly;
|
221
|
+
}) {
|
222
|
+
return (options?.outputOnly ? this : super.optional(options)) as unknown as ArraySchema<
|
223
|
+
TOutputOnly extends true
|
224
|
+
? {
|
225
|
+
input: TType['input'];
|
226
|
+
validate: TType['validate'];
|
227
|
+
internal: TType['internal'];
|
228
|
+
output: TType['output'] | undefined | null;
|
229
|
+
representation: TType['representation'];
|
230
|
+
}
|
231
|
+
: {
|
232
|
+
input: TType['input'] | undefined | null;
|
233
|
+
validate: TType['validate'] | undefined | null;
|
234
|
+
internal: TType['internal'] | undefined | null;
|
235
|
+
output: TType['output'] | undefined | null;
|
236
|
+
representation: TType['representation'] | undefined | null;
|
237
|
+
},
|
238
|
+
TDefinitions,
|
239
|
+
TSchemas
|
240
|
+
>;
|
241
|
+
}
|
242
|
+
|
243
|
+
/**
|
244
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
245
|
+
*
|
246
|
+
* ```typescript
|
247
|
+
* p.datetime().optional({ message: 'This value should be defined', allow: false })
|
248
|
+
* ```
|
249
|
+
*
|
250
|
+
* @param options - The options of nonOptional function
|
251
|
+
* @param options.message - A custom message if the value is undefined.
|
252
|
+
*
|
253
|
+
* @returns - The schema.
|
254
|
+
*/
|
255
|
+
nonOptional(options?: { message: string }) {
|
256
|
+
return super.optional({
|
257
|
+
message: options?.message,
|
258
|
+
allow: false
|
259
|
+
}) as unknown as ArraySchema<
|
197
260
|
{
|
198
261
|
input: TType['input'];
|
199
262
|
validate: TType['validate'];
|
200
263
|
internal: TType['internal'];
|
201
|
-
output: TType['output']
|
264
|
+
output: TType['output'];
|
202
265
|
representation: TType['representation'];
|
203
|
-
}
|
204
|
-
|
205
|
-
input: TType['input'] | undefined | null;
|
206
|
-
validate: TType['validate'] | undefined | null;
|
207
|
-
internal: TType['internal'] | undefined | null;
|
208
|
-
output: TType['output'] | undefined | null;
|
209
|
-
representation: TType['representation'] | undefined | null;
|
210
|
-
}),
|
211
|
-
TDefinitions,
|
212
|
-
TSchemas
|
266
|
+
},
|
267
|
+
TDefinitions
|
213
268
|
>;
|
214
269
|
}
|
215
270
|
|
216
271
|
/**
|
217
|
-
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
218
|
-
* the { message: 'Your custom message', allow: false } on the options.
|
272
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
273
|
+
* is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
219
274
|
*
|
220
275
|
* @example
|
221
276
|
* ```typescript
|
@@ -252,16 +307,44 @@ export default class ArraySchema<
|
|
252
307
|
>;
|
253
308
|
}
|
254
309
|
|
310
|
+
/**
|
311
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
312
|
+
*
|
313
|
+
* ```typescript
|
314
|
+
* p.datetime().nullable({ message: 'This value cannot be null', allow: false })
|
315
|
+
* ```
|
316
|
+
*
|
317
|
+
* @param options - The options of nonNullable function
|
318
|
+
* @param options.message - A custom message if the value is null.
|
319
|
+
*
|
320
|
+
* @returns - The schema.
|
321
|
+
*/
|
322
|
+
nonNullable(options?: { message: string }) {
|
323
|
+
return super.nullable({
|
324
|
+
message: options?.message || '',
|
325
|
+
allow: false
|
326
|
+
}) as unknown as ArraySchema<
|
327
|
+
{
|
328
|
+
input: TType['input'];
|
329
|
+
validate: TType['validate'];
|
330
|
+
internal: TType['internal'];
|
331
|
+
output: TType['output'];
|
332
|
+
representation: TType['representation'];
|
333
|
+
},
|
334
|
+
TDefinitions
|
335
|
+
>;
|
336
|
+
}
|
255
337
|
|
256
338
|
/**
|
257
|
-
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
258
|
-
* otherwise it will set the value to undefined after it's validated.
|
339
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
340
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
259
341
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
260
342
|
* function. This will remove the value from the representation of the schema.
|
261
343
|
*
|
262
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
263
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
264
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
344
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
345
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
346
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
347
|
+
* the argument `toRepresentation` as true as well.
|
265
348
|
*
|
266
349
|
* @example
|
267
350
|
* ```typescript
|
@@ -283,16 +366,19 @@ export default class ArraySchema<
|
|
283
366
|
* ```
|
284
367
|
*
|
285
368
|
*
|
286
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
287
|
-
*
|
288
|
-
*
|
369
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
370
|
+
* the {@link data} function.
|
371
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
372
|
+
* as true.
|
373
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument
|
374
|
+
* `toRepresentation` as true as well.
|
289
375
|
*
|
290
376
|
* @returns The schema.
|
291
377
|
*/
|
292
378
|
omit<
|
293
379
|
TToInternal extends boolean,
|
294
380
|
TToRepresentation extends boolean = boolean extends TToInternal ? true : false
|
295
|
-
>(args?: { toInternal?: TToInternal
|
381
|
+
>(args?: { toInternal?: TToInternal; toRepresentation?: TToRepresentation }) {
|
296
382
|
return super.omit(args) as unknown as ArraySchema<
|
297
383
|
{
|
298
384
|
input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
|
@@ -307,9 +393,9 @@ export default class ArraySchema<
|
|
307
393
|
}
|
308
394
|
|
309
395
|
/**
|
310
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
311
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
312
|
-
* of the schema after the save operation.
|
396
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
397
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
398
|
+
* the correct type of the schema after the save operation.
|
313
399
|
*
|
314
400
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
315
401
|
*
|
@@ -364,9 +450,9 @@ export default class ArraySchema<
|
|
364
450
|
>;
|
365
451
|
}
|
366
452
|
|
367
|
-
|
368
453
|
/**
|
369
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
454
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
455
|
+
* value will be used.
|
370
456
|
*
|
371
457
|
* @example
|
372
458
|
* ```typescript
|
@@ -396,8 +482,57 @@ export default class ArraySchema<
|
|
396
482
|
}
|
397
483
|
|
398
484
|
/**
|
399
|
-
* This function
|
400
|
-
*
|
485
|
+
* This function is used to transform the value to the representation without validating it.
|
486
|
+
* This is useful when you want to return a data from a query directly to the user. But for example
|
487
|
+
* you are returning the data of a user, you can clean the password or any other sensitive data.
|
488
|
+
*
|
489
|
+
* @example
|
490
|
+
* ```typescript
|
491
|
+
* import * as p from '@palmares/schemas';
|
492
|
+
*
|
493
|
+
* const userSchema = p.object({
|
494
|
+
* id: p.number().optional(),
|
495
|
+
* name: p.string(),
|
496
|
+
* email: p.string().email(),
|
497
|
+
* password: p.string().optional()
|
498
|
+
* }).toRepresentation(async (value) => {
|
499
|
+
* return {
|
500
|
+
* id: value.id,
|
501
|
+
* name: value.name,
|
502
|
+
* email: value.email
|
503
|
+
* }
|
504
|
+
* });
|
505
|
+
*
|
506
|
+
* const user = await userSchema.data({
|
507
|
+
* id: 1,
|
508
|
+
* name: 'John Doe',
|
509
|
+
* email: 'john@gmail.com',
|
510
|
+
* password: '123456'
|
511
|
+
* });
|
512
|
+
* ```
|
513
|
+
*/
|
514
|
+
async data(value: TType['output']): Promise<TType['representation']> {
|
515
|
+
let parsedValue = await super.data(value);
|
516
|
+
if (Array.isArray(parsedValue)) {
|
517
|
+
parsedValue = (await Promise.all(
|
518
|
+
Array.isArray(this.__schemas[0])
|
519
|
+
? parsedValue.map((value: any) => {
|
520
|
+
const schema = (this.__schemas as unknown as [[Schema]])[0][0];
|
521
|
+
|
522
|
+
return shouldRunDataOnComplexSchemas(schema) ? schema.data(value) : value;
|
523
|
+
})
|
524
|
+
: (this.__schemas as [Schema, ...Schema[]]).map(async (schema) => {
|
525
|
+
return shouldRunDataOnComplexSchemas(schema) ? schema.data(value) : value;
|
526
|
+
})
|
527
|
+
)) as unknown as TType['representation'];
|
528
|
+
}
|
529
|
+
return parsedValue;
|
530
|
+
}
|
531
|
+
|
532
|
+
/**
|
533
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
534
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
535
|
+
* No problem, you can use this function to customize the zod schema.
|
401
536
|
*
|
402
537
|
* @example
|
403
538
|
* ```typescript
|
@@ -409,12 +544,13 @@ export default class ArraySchema<
|
|
409
544
|
*
|
410
545
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
411
546
|
*
|
412
|
-
*
|
547
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
548
|
+
* console.log(errors);
|
413
549
|
* ```
|
414
550
|
*
|
415
551
|
* @param callback - The callback that will be called to customize the schema.
|
416
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
417
|
-
* to a string so you can save it for future runs.
|
552
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
553
|
+
* compile the underlying schema to a string so you can save it for future runs.
|
418
554
|
*
|
419
555
|
* @returns The schema.
|
420
556
|
*/
|
@@ -428,8 +564,9 @@ export default class ArraySchema<
|
|
428
564
|
}
|
429
565
|
|
430
566
|
/**
|
431
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
432
|
-
* control to add data cleaning for example, transforming the data and
|
567
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
568
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
569
|
+
* whatever. Another use case is when you want to return deeply nested recursive data.
|
433
570
|
* The schema maps to itself.
|
434
571
|
*
|
435
572
|
* @example
|
@@ -487,8 +624,9 @@ export default class ArraySchema<
|
|
487
624
|
}
|
488
625
|
|
489
626
|
/**
|
490
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when you
|
491
|
-
* to a type that the schema adapter can understand. For example, you might want to
|
627
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when you
|
628
|
+
* want to transform the value to a type that the schema adapter can understand. For example, you might want to
|
629
|
+
* transform a string to a date. This is the function you use.
|
492
630
|
*
|
493
631
|
* @example
|
494
632
|
* ```typescript
|
@@ -533,8 +671,9 @@ export default class ArraySchema<
|
|
533
671
|
}
|
534
672
|
|
535
673
|
/**
|
536
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
537
|
-
* here BEFORE the validation. This pretty much transforms the value to a type
|
674
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
675
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
|
676
|
+
* that the schema adapter can understand.
|
538
677
|
*
|
539
678
|
* @example
|
540
679
|
* ```
|
@@ -566,41 +705,69 @@ export default class ArraySchema<
|
|
566
705
|
>;
|
567
706
|
}
|
568
707
|
|
569
|
-
minLength(value: number,
|
570
|
-
message = message || `The array must have a minimum length of ${value}`;
|
708
|
+
minLength(value: number, options?: Omit<ArraySchema['__minLength'], 'value'>) {
|
709
|
+
const message = options?.message || `The array must have a minimum length of ${value}`;
|
571
710
|
this.__minLength = {
|
572
711
|
value: value,
|
573
|
-
inclusive: inclusive,
|
574
|
-
message: message
|
712
|
+
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : true,
|
713
|
+
message: message
|
575
714
|
};
|
576
715
|
|
577
|
-
return this
|
716
|
+
return this as unknown as ArraySchema<
|
717
|
+
{
|
718
|
+
input: [TType['input'][number], ...TType['input'][number][]];
|
719
|
+
validate: [TType['validate'][number], ...TType['validate'][number][]];
|
720
|
+
internal: [TType['internal'][number], ...TType['internal'][number][]];
|
721
|
+
output: [TType['output'][number], ...TType['output'][number][]];
|
722
|
+
representation: [TType['representation'][number], ...TType['representation'][number][]];
|
723
|
+
},
|
724
|
+
TDefinitions,
|
725
|
+
TSchemas
|
726
|
+
>;
|
578
727
|
}
|
579
728
|
|
580
|
-
maxLength(value: number,
|
581
|
-
message = message || `The array must have a maximum length of ${value}`;
|
729
|
+
maxLength(value: number, options?: Omit<ArraySchema['__maxLength'], 'value'>) {
|
730
|
+
const message = options?.message || `The array must have a maximum length of ${value}`;
|
582
731
|
this.__maxLength = {
|
583
732
|
value: value,
|
584
|
-
inclusive: inclusive,
|
585
|
-
message: message
|
586
|
-
};
|
587
|
-
|
588
|
-
return this;
|
589
|
-
}
|
590
|
-
|
591
|
-
nonEmpty(message?: string) {
|
592
|
-
message = message || 'The array must not be empty';
|
593
|
-
this.__nonEmpty = {
|
594
|
-
message: message,
|
733
|
+
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : true,
|
734
|
+
message: message
|
595
735
|
};
|
596
736
|
|
597
737
|
return this;
|
598
738
|
}
|
599
739
|
|
600
740
|
static new<
|
601
|
-
TSchemas extends readonly [Schema, ...Schema[]] | [Schema
|
602
|
-
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
603
|
-
>(
|
741
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [[Schema]],
|
742
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
743
|
+
>(
|
744
|
+
...schemas: TSchemas
|
745
|
+
): TSchemas extends [[Schema]]
|
746
|
+
? ArraySchema<
|
747
|
+
{
|
748
|
+
input: ExtractTypeFromArrayOfSchemas<TSchemas, 'input'>;
|
749
|
+
validate: ExtractTypeFromArrayOfSchemas<TSchemas, 'validate'>;
|
750
|
+
internal: ExtractTypeFromArrayOfSchemas<TSchemas, 'internal'>;
|
751
|
+
output: ExtractTypeFromArrayOfSchemas<TSchemas, 'output'>;
|
752
|
+
representation: ExtractTypeFromArrayOfSchemas<TSchemas, 'representation'>;
|
753
|
+
},
|
754
|
+
TDefinitions,
|
755
|
+
TSchemas
|
756
|
+
>
|
757
|
+
: ArraySchema<
|
758
|
+
{
|
759
|
+
input: ExtractTypeFromArrayOfSchemas<TSchemas, 'input'>;
|
760
|
+
validate: ExtractTypeFromArrayOfSchemas<TSchemas, 'validate'>;
|
761
|
+
internal: ExtractTypeFromArrayOfSchemas<TSchemas, 'internal'>;
|
762
|
+
output: ExtractTypeFromArrayOfSchemas<TSchemas, 'output'>;
|
763
|
+
representation: ExtractTypeFromArrayOfSchemas<TSchemas, 'representation'>;
|
764
|
+
},
|
765
|
+
TDefinitions,
|
766
|
+
TSchemas
|
767
|
+
> & {
|
768
|
+
maxLength: never;
|
769
|
+
minLength: never;
|
770
|
+
} {
|
604
771
|
const returnValue = new ArraySchema<
|
605
772
|
{
|
606
773
|
input: ExtractTypeFromArrayOfSchemas<TSchemas, 'input'>;
|
@@ -613,21 +780,38 @@ export default class ArraySchema<
|
|
613
780
|
TSchemas
|
614
781
|
>(...schemas);
|
615
782
|
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
783
|
+
return returnValue as TSchemas extends [[Schema]]
|
784
|
+
? ArraySchema<
|
785
|
+
{
|
786
|
+
input: ExtractTypeFromArrayOfSchemas<TSchemas, 'input'>;
|
787
|
+
validate: ExtractTypeFromArrayOfSchemas<TSchemas, 'validate'>;
|
788
|
+
internal: ExtractTypeFromArrayOfSchemas<TSchemas, 'internal'>;
|
789
|
+
output: ExtractTypeFromArrayOfSchemas<TSchemas, 'output'>;
|
790
|
+
representation: ExtractTypeFromArrayOfSchemas<TSchemas, 'representation'>;
|
791
|
+
},
|
792
|
+
TDefinitions,
|
793
|
+
TSchemas
|
794
|
+
>
|
795
|
+
: ArraySchema<
|
796
|
+
{
|
797
|
+
input: ExtractTypeFromArrayOfSchemas<TSchemas, 'input'>;
|
798
|
+
validate: ExtractTypeFromArrayOfSchemas<TSchemas, 'validate'>;
|
799
|
+
internal: ExtractTypeFromArrayOfSchemas<TSchemas, 'internal'>;
|
800
|
+
output: ExtractTypeFromArrayOfSchemas<TSchemas, 'output'>;
|
801
|
+
representation: ExtractTypeFromArrayOfSchemas<TSchemas, 'representation'>;
|
802
|
+
},
|
803
|
+
TDefinitions,
|
804
|
+
TSchemas
|
805
|
+
> & {
|
806
|
+
maxLength: never;
|
807
|
+
minLength: never;
|
808
|
+
};
|
625
809
|
}
|
626
810
|
}
|
627
811
|
|
628
812
|
export const array = <
|
629
|
-
TSchemas extends readonly [Schema, ...Schema[]] | [Schema
|
630
|
-
TDefinitions extends DefinitionsOfSchemaType
|
813
|
+
TSchemas extends readonly [Schema, ...Schema[]] | [[Schema]],
|
814
|
+
TDefinitions extends DefinitionsOfSchemaType
|
631
815
|
>(
|
632
816
|
...schemas: TSchemas
|
633
817
|
) => ArraySchema.new<TSchemas, TDefinitions>(...schemas);
|