@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,14 @@
|
|
1
1
|
import Schema from './schema';
|
2
|
-
import {
|
3
|
-
import { defaultTransform, defaultTransformToAdapter, transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas } from '../utils';
|
2
|
+
import { defaultTransform, defaultTransformToAdapter, shouldRunDataOnComplexSchemas, transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas } from '../utils';
|
4
3
|
import { arrayValidation, maxLength, minLength, nonEmpty } from '../validators/array';
|
5
4
|
import { nullable, optional } from '../validators/schema';
|
6
5
|
import Validator from '../validators/utils';
|
7
6
|
export default class ArraySchema extends Schema {
|
8
7
|
__schemas;
|
8
|
+
__type = {
|
9
|
+
message: 'Invalid type',
|
10
|
+
check: (value)=>Array.isArray(value)
|
11
|
+
};
|
9
12
|
__minLength;
|
10
13
|
__maxLength;
|
11
14
|
__nonEmpty;
|
@@ -36,6 +39,8 @@ export default class ArraySchema extends Schema {
|
|
36
39
|
maxLength: this.__maxLength,
|
37
40
|
minLength: this.__minLength,
|
38
41
|
nonEmpty: this.__nonEmpty,
|
42
|
+
schemas: transformedSchemas,
|
43
|
+
type: this.__type,
|
39
44
|
parsers: {
|
40
45
|
nullable: this.__nullable.allow,
|
41
46
|
optional: this.__optional.allow
|
@@ -51,10 +56,11 @@ export default class ArraySchema extends Schema {
|
|
51
56
|
// eslint-disable-next-line ts/require-await
|
52
57
|
fallbackIfNotSupported: async ()=>[]
|
53
58
|
});
|
54
|
-
}, this.__transformedSchemas, options, 'array');
|
59
|
+
}, this, this.__transformedSchemas, options, 'array');
|
55
60
|
}
|
56
61
|
/**
|
57
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
62
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
63
|
+
* is not supported by default by the schema adapter.
|
58
64
|
*
|
59
65
|
* @example
|
60
66
|
* ```typescript
|
@@ -66,7 +72,8 @@ export default class ArraySchema extends Schema {
|
|
66
72
|
*
|
67
73
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
68
74
|
*
|
69
|
-
*
|
75
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
76
|
+
* console.log(errors);
|
70
77
|
* ```
|
71
78
|
*
|
72
79
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -76,9 +83,10 @@ export default class ArraySchema extends Schema {
|
|
76
83
|
return super.refine(refinementCallback);
|
77
84
|
}
|
78
85
|
/**
|
79
|
-
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can
|
80
|
-
*
|
81
|
-
* This
|
86
|
+
* Allows the value to be either undefined or null. Different from the `optional` method on other schemas, You can
|
87
|
+
* pass `outputOnly` as `true` to this method.
|
88
|
+
* This will allow you to pass `null` or `undefined` as a value on the {@link Schema.data} method, but it will not
|
89
|
+
* allow the value to be `null` or `undefined`. This is useful for typing purposes.
|
82
90
|
*
|
83
91
|
* @example
|
84
92
|
* ```typescript
|
@@ -105,8 +113,10 @@ export default class ArraySchema extends Schema {
|
|
105
113
|
* company: companySchema.optional({ outputOnly: true })
|
106
114
|
* });
|
107
115
|
*
|
108
|
-
*
|
109
|
-
* const
|
116
|
+
* // Will not allow the company to be null or undefined on a typing level.
|
117
|
+
* const { errors, parsed } = await userSchema.data({ id: 1, name: 'John Doe' });
|
118
|
+
* // Will allow the company to be null or undefined on a typing level
|
119
|
+
* const value = await userSchema.data({ id: 1, name: 'John Doe' });
|
110
120
|
* ```
|
111
121
|
*
|
112
122
|
* @returns - The schema we are working with.
|
@@ -114,8 +124,25 @@ export default class ArraySchema extends Schema {
|
|
114
124
|
return options?.outputOnly ? this : super.optional(options);
|
115
125
|
}
|
116
126
|
/**
|
117
|
-
*
|
118
|
-
*
|
127
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
128
|
+
*
|
129
|
+
* ```typescript
|
130
|
+
* p.datetime().optional({ message: 'This value should be defined', allow: false })
|
131
|
+
* ```
|
132
|
+
*
|
133
|
+
* @param options - The options of nonOptional function
|
134
|
+
* @param options.message - A custom message if the value is undefined.
|
135
|
+
*
|
136
|
+
* @returns - The schema.
|
137
|
+
*/ nonOptional(options) {
|
138
|
+
return super.optional({
|
139
|
+
message: options?.message,
|
140
|
+
allow: false
|
141
|
+
});
|
142
|
+
}
|
143
|
+
/**
|
144
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
145
|
+
* is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
119
146
|
*
|
120
147
|
* @example
|
121
148
|
* ```typescript
|
@@ -141,14 +168,32 @@ export default class ArraySchema extends Schema {
|
|
141
168
|
return super.nullable(options);
|
142
169
|
}
|
143
170
|
/**
|
144
|
-
*
|
145
|
-
*
|
171
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
172
|
+
*
|
173
|
+
* ```typescript
|
174
|
+
* p.datetime().nullable({ message: 'This value cannot be null', allow: false })
|
175
|
+
* ```
|
176
|
+
*
|
177
|
+
* @param options - The options of nonNullable function
|
178
|
+
* @param options.message - A custom message if the value is null.
|
179
|
+
*
|
180
|
+
* @returns - The schema.
|
181
|
+
*/ nonNullable(options) {
|
182
|
+
return super.nullable({
|
183
|
+
message: options?.message || '',
|
184
|
+
allow: false
|
185
|
+
});
|
186
|
+
}
|
187
|
+
/**
|
188
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
189
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
146
190
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
147
191
|
* function. This will remove the value from the representation of the schema.
|
148
192
|
*
|
149
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
150
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
151
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
193
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
194
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
195
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
196
|
+
* the argument `toRepresentation` as true as well.
|
152
197
|
*
|
153
198
|
* @example
|
154
199
|
* ```typescript
|
@@ -170,18 +215,21 @@ export default class ArraySchema extends Schema {
|
|
170
215
|
* ```
|
171
216
|
*
|
172
217
|
*
|
173
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
174
|
-
*
|
175
|
-
*
|
218
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
219
|
+
* the {@link data} function.
|
220
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
221
|
+
* as true.
|
222
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument
|
223
|
+
* `toRepresentation` as true as well.
|
176
224
|
*
|
177
225
|
* @returns The schema.
|
178
226
|
*/ omit(args) {
|
179
227
|
return super.omit(args);
|
180
228
|
}
|
181
229
|
/**
|
182
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
183
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
184
|
-
* of the schema after the save operation.
|
230
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
231
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
232
|
+
* the correct type of the schema after the save operation.
|
185
233
|
*
|
186
234
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
187
235
|
*
|
@@ -223,7 +271,8 @@ export default class ArraySchema extends Schema {
|
|
223
271
|
return super.onSave(callback);
|
224
272
|
}
|
225
273
|
/**
|
226
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
274
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
275
|
+
* value will be used.
|
227
276
|
*
|
228
277
|
* @example
|
229
278
|
* ```typescript
|
@@ -239,8 +288,50 @@ export default class ArraySchema extends Schema {
|
|
239
288
|
return super.default(defaultValueOrFunction);
|
240
289
|
}
|
241
290
|
/**
|
242
|
-
* This function
|
243
|
-
*
|
291
|
+
* This function is used to transform the value to the representation without validating it.
|
292
|
+
* This is useful when you want to return a data from a query directly to the user. But for example
|
293
|
+
* you are returning the data of a user, you can clean the password or any other sensitive data.
|
294
|
+
*
|
295
|
+
* @example
|
296
|
+
* ```typescript
|
297
|
+
* import * as p from '@palmares/schemas';
|
298
|
+
*
|
299
|
+
* const userSchema = p.object({
|
300
|
+
* id: p.number().optional(),
|
301
|
+
* name: p.string(),
|
302
|
+
* email: p.string().email(),
|
303
|
+
* password: p.string().optional()
|
304
|
+
* }).toRepresentation(async (value) => {
|
305
|
+
* return {
|
306
|
+
* id: value.id,
|
307
|
+
* name: value.name,
|
308
|
+
* email: value.email
|
309
|
+
* }
|
310
|
+
* });
|
311
|
+
*
|
312
|
+
* const user = await userSchema.data({
|
313
|
+
* id: 1,
|
314
|
+
* name: 'John Doe',
|
315
|
+
* email: 'john@gmail.com',
|
316
|
+
* password: '123456'
|
317
|
+
* });
|
318
|
+
* ```
|
319
|
+
*/ async data(value) {
|
320
|
+
let parsedValue = await super.data(value);
|
321
|
+
if (Array.isArray(parsedValue)) {
|
322
|
+
parsedValue = await Promise.all(Array.isArray(this.__schemas[0]) ? parsedValue.map((value)=>{
|
323
|
+
const schema = this.__schemas[0][0];
|
324
|
+
return shouldRunDataOnComplexSchemas(schema) ? schema.data(value) : value;
|
325
|
+
}) : this.__schemas.map(async (schema)=>{
|
326
|
+
return shouldRunDataOnComplexSchemas(schema) ? schema.data(value) : value;
|
327
|
+
}));
|
328
|
+
}
|
329
|
+
return parsedValue;
|
330
|
+
}
|
331
|
+
/**
|
332
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
333
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
334
|
+
* No problem, you can use this function to customize the zod schema.
|
244
335
|
*
|
245
336
|
* @example
|
246
337
|
* ```typescript
|
@@ -252,20 +343,22 @@ export default class ArraySchema extends Schema {
|
|
252
343
|
*
|
253
344
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
254
345
|
*
|
255
|
-
*
|
346
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
347
|
+
* console.log(errors);
|
256
348
|
* ```
|
257
349
|
*
|
258
350
|
* @param callback - The callback that will be called to customize the schema.
|
259
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
260
|
-
* to a string so you can save it for future runs.
|
351
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
352
|
+
* compile the underlying schema to a string so you can save it for future runs.
|
261
353
|
*
|
262
354
|
* @returns The schema.
|
263
355
|
*/ extends(callback, toStringCallback) {
|
264
356
|
return super.extends(callback, toStringCallback);
|
265
357
|
}
|
266
358
|
/**
|
267
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
268
|
-
* control to add data cleaning for example, transforming the data and
|
359
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
360
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
361
|
+
* whatever. Another use case is when you want to return deeply nested recursive data.
|
269
362
|
* The schema maps to itself.
|
270
363
|
*
|
271
364
|
* @example
|
@@ -309,8 +402,9 @@ export default class ArraySchema extends Schema {
|
|
309
402
|
return super.toRepresentation(toRepresentationCallback);
|
310
403
|
}
|
311
404
|
/**
|
312
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when you
|
313
|
-
* to a type that the schema adapter can understand. For example, you might want to
|
405
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when you
|
406
|
+
* want to transform the value to a type that the schema adapter can understand. For example, you might want to
|
407
|
+
* transform a string to a date. This is the function you use.
|
314
408
|
*
|
315
409
|
* @example
|
316
410
|
* ```typescript
|
@@ -343,8 +437,9 @@ export default class ArraySchema extends Schema {
|
|
343
437
|
return super.toInternal(toInternalCallback);
|
344
438
|
}
|
345
439
|
/**
|
346
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
347
|
-
* here BEFORE the validation. This pretty much transforms the value to a type
|
440
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
441
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
|
442
|
+
* that the schema adapter can understand.
|
348
443
|
*
|
349
444
|
* @example
|
350
445
|
* ```
|
@@ -364,39 +459,26 @@ export default class ArraySchema extends Schema {
|
|
364
459
|
*/ toValidate(toValidateCallback) {
|
365
460
|
return super.toValidate(toValidateCallback);
|
366
461
|
}
|
367
|
-
minLength(value,
|
368
|
-
message = message || `The array must have a minimum length of ${value}`;
|
462
|
+
minLength(value, options) {
|
463
|
+
const message = options?.message || `The array must have a minimum length of ${value}`;
|
369
464
|
this.__minLength = {
|
370
465
|
value: value,
|
371
|
-
inclusive: inclusive,
|
466
|
+
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : true,
|
372
467
|
message: message
|
373
468
|
};
|
374
469
|
return this;
|
375
470
|
}
|
376
|
-
maxLength(value,
|
377
|
-
message = message || `The array must have a maximum length of ${value}`;
|
471
|
+
maxLength(value, options) {
|
472
|
+
const message = options?.message || `The array must have a maximum length of ${value}`;
|
378
473
|
this.__maxLength = {
|
379
474
|
value: value,
|
380
|
-
inclusive: inclusive,
|
381
|
-
message: message
|
382
|
-
};
|
383
|
-
return this;
|
384
|
-
}
|
385
|
-
nonEmpty(message) {
|
386
|
-
message = message || 'The array must not be empty';
|
387
|
-
this.__nonEmpty = {
|
475
|
+
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : true,
|
388
476
|
message: message
|
389
477
|
};
|
390
478
|
return this;
|
391
479
|
}
|
392
480
|
static new(...schemas) {
|
393
481
|
const returnValue = new ArraySchema(...schemas);
|
394
|
-
const adapterInstance = getDefaultAdapter();
|
395
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
396
|
-
transformed: false,
|
397
|
-
adapter: adapterInstance,
|
398
|
-
schemas: []
|
399
|
-
};
|
400
482
|
return returnValue;
|
401
483
|
}
|
402
484
|
}
|
@@ -1,5 +1,4 @@
|
|
1
1
|
import Schema from './schema';
|
2
|
-
import { getDefaultAdapter } from '../conf';
|
3
2
|
import convertFromStringBuilder from '../parsers/convert-from-string';
|
4
3
|
import { defaultTransform, defaultTransformToAdapter } from '../utils';
|
5
4
|
import { booleanValidation } from '../validators/boolean';
|
@@ -10,18 +9,30 @@ export default class BooleanSchema extends Schema {
|
|
10
9
|
__trueValues;
|
11
10
|
__falseValues;
|
12
11
|
__is;
|
12
|
+
__type = {
|
13
|
+
message: 'Invalid type',
|
14
|
+
check: (value)=>{
|
15
|
+
if (typeof value === 'string' && this.__allowString) return true;
|
16
|
+
if (typeof value === 'number' && this.__allowNumber) return true;
|
17
|
+
if (Array.isArray(this.__trueValues) && this.__trueValues.includes(value)) return true;
|
18
|
+
if (Array.isArray(this.__falseValues) && this.__falseValues.includes(value)) return true;
|
19
|
+
return typeof value === 'boolean';
|
20
|
+
}
|
21
|
+
};
|
13
22
|
async __transformToAdapter(options) {
|
14
23
|
return defaultTransformToAdapter(async (adapter)=>{
|
15
24
|
return defaultTransform('boolean', this, adapter, adapter.boolean, ()=>({
|
16
25
|
parsers: {
|
17
26
|
allowString: this.__allowString,
|
18
27
|
allowNumber: this.__allowNumber,
|
19
|
-
|
28
|
+
// eslint-disable-next-line ts/no-unnecessary-condition
|
29
|
+
is: this.__is?.value,
|
20
30
|
trueValues: this.__trueValues,
|
21
31
|
falseValues: this.__falseValues,
|
22
32
|
nullable: this.__nullable.allow,
|
23
33
|
optional: this.__optional.allow
|
24
34
|
},
|
35
|
+
type: this.__type,
|
25
36
|
is: this.__is,
|
26
37
|
nullable: this.__nullable,
|
27
38
|
optional: this.__optional
|
@@ -35,10 +46,11 @@ export default class BooleanSchema extends Schema {
|
|
35
46
|
// eslint-disable-next-line ts/require-await
|
36
47
|
fallbackIfNotSupported: async ()=>[]
|
37
48
|
});
|
38
|
-
}, this.__transformedSchemas, options, 'boolean');
|
49
|
+
}, this, this.__transformedSchemas, options, 'boolean');
|
39
50
|
}
|
40
51
|
/**
|
41
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
52
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
53
|
+
* is not supported by default by the schema adapter.
|
42
54
|
*
|
43
55
|
* @example
|
44
56
|
* ```typescript
|
@@ -50,7 +62,8 @@ export default class BooleanSchema extends Schema {
|
|
50
62
|
*
|
51
63
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
52
64
|
*
|
53
|
-
*
|
65
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
66
|
+
* console.log(errors);
|
54
67
|
* ```
|
55
68
|
*
|
56
69
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -88,8 +101,25 @@ export default class BooleanSchema extends Schema {
|
|
88
101
|
return super.optional(options);
|
89
102
|
}
|
90
103
|
/**
|
91
|
-
*
|
92
|
-
*
|
104
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
105
|
+
*
|
106
|
+
* ```typescript
|
107
|
+
* p.string().optional({ message: 'This value cannot be null', allow: false })
|
108
|
+
* ```
|
109
|
+
*
|
110
|
+
* @param options - The options of nonOptional function
|
111
|
+
* @param options.message - A custom message if the value is undefined.
|
112
|
+
*
|
113
|
+
* @returns - The schema.
|
114
|
+
*/ nonOptional(options) {
|
115
|
+
return super.optional({
|
116
|
+
message: options?.message,
|
117
|
+
allow: false
|
118
|
+
});
|
119
|
+
}
|
120
|
+
/**
|
121
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value is
|
122
|
+
* NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
93
123
|
*
|
94
124
|
* @example
|
95
125
|
* ```typescript
|
@@ -115,14 +145,32 @@ export default class BooleanSchema extends Schema {
|
|
115
145
|
return super.nullable(options);
|
116
146
|
}
|
117
147
|
/**
|
118
|
-
*
|
119
|
-
*
|
148
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
149
|
+
*
|
150
|
+
* ```typescript
|
151
|
+
* p.string().nullable({ message: 'This value cannot be null', allow: false })
|
152
|
+
* ```
|
153
|
+
*
|
154
|
+
* @param options - The options of nonNullable function
|
155
|
+
* @param options.message - A custom message if the value is null.
|
156
|
+
*
|
157
|
+
* @returns - The schema.
|
158
|
+
*/ nonNullable(options) {
|
159
|
+
return super.nullable({
|
160
|
+
message: options?.message || '',
|
161
|
+
allow: false
|
162
|
+
});
|
163
|
+
}
|
164
|
+
/**
|
165
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
166
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
120
167
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
121
168
|
* function. This will remove the value from the representation of the schema.
|
122
169
|
*
|
123
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
124
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
125
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
170
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
171
|
+
* function.But if you want to remove the value from the internal representation, you can pass the argument
|
172
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
173
|
+
* the argument `toRepresentation` as true as well.
|
126
174
|
*
|
127
175
|
* @example
|
128
176
|
* ```typescript
|
@@ -144,18 +192,20 @@ export default class BooleanSchema extends Schema {
|
|
144
192
|
* ```
|
145
193
|
*
|
146
194
|
*
|
147
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
148
|
-
* But if you want to remove the value from the internal representation, you can pass the
|
149
|
-
*
|
195
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
196
|
+
* the {@link data} function. But if you want to remove the value from the internal representation, you can pass the
|
197
|
+
* argument `toInternal` as true.
|
198
|
+
* Then if you still want to remove the value from the representation, you will need to pass the argument
|
199
|
+
* `toRepresentation` as true as well.
|
150
200
|
*
|
151
201
|
* @returns The schema.
|
152
202
|
*/ omit(args) {
|
153
203
|
return super.omit(args);
|
154
204
|
}
|
155
205
|
/**
|
156
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
157
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
158
|
-
* of the schema after the save operation.
|
206
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
207
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
208
|
+
* the correct type of the schema after the save operation.
|
159
209
|
*
|
160
210
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
161
211
|
*
|
@@ -197,7 +247,8 @@ export default class BooleanSchema extends Schema {
|
|
197
247
|
return super.onSave(callback);
|
198
248
|
}
|
199
249
|
/**
|
200
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
250
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
251
|
+
* value will be used.
|
201
252
|
*
|
202
253
|
* @example
|
203
254
|
* ```typescript
|
@@ -213,8 +264,9 @@ export default class BooleanSchema extends Schema {
|
|
213
264
|
return super.default(defaultValueOrFunction);
|
214
265
|
}
|
215
266
|
/**
|
216
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
217
|
-
* the custom schema your own way. Our API does not support passthrough?
|
267
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
268
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
269
|
+
* No problem, you can use this function to customize the zod schema.
|
218
270
|
*
|
219
271
|
* @example
|
220
272
|
* ```typescript
|
@@ -225,22 +277,22 @@ export default class BooleanSchema extends Schema {
|
|
225
277
|
* });
|
226
278
|
*
|
227
279
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
228
|
-
*
|
229
|
-
* console.log(errors);
|
280
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
281
|
+
* console.log(errors);
|
230
282
|
* ```
|
231
283
|
*
|
232
284
|
* @param callback - The callback that will be called to customize the schema.
|
233
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
234
|
-
* to a string so you can save it for future runs.
|
285
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
286
|
+
* compile the underlying schema to a string so you can save it for future runs.
|
235
287
|
*
|
236
288
|
* @returns The schema.
|
237
289
|
*/ extends(callback, toStringCallback) {
|
238
290
|
return super.extends(callback, toStringCallback);
|
239
291
|
}
|
240
292
|
/**
|
241
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
242
|
-
* control to add data cleaning for example, transforming the data and
|
243
|
-
* The schema maps to itself.
|
293
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
294
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
295
|
+
* whatever. Another use case is when you want to return deeply nested recursive data. The schema maps to itself.
|
244
296
|
*
|
245
297
|
* @example
|
246
298
|
* ```typescript
|
@@ -283,8 +335,9 @@ export default class BooleanSchema extends Schema {
|
|
283
335
|
return super.toRepresentation(toRepresentationCallback);
|
284
336
|
}
|
285
337
|
/**
|
286
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
287
|
-
* to a type that the schema adapter can understand. For example, you might want
|
338
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
339
|
+
* you want to transform the value to a type that the schema adapter can understand. For example, you might want
|
340
|
+
* to transform a string to a date. This is the function you use.
|
288
341
|
*
|
289
342
|
* @example
|
290
343
|
* ```typescript
|
@@ -317,8 +370,9 @@ export default class BooleanSchema extends Schema {
|
|
317
370
|
return super.toInternal(toInternalCallback);
|
318
371
|
}
|
319
372
|
/**
|
320
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
321
|
-
* here BEFORE the validation. This pretty much transforms the value to a type
|
373
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
374
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
|
375
|
+
* that the schema adapter can understand.
|
322
376
|
*
|
323
377
|
* @example
|
324
378
|
* ```
|
@@ -339,7 +393,8 @@ export default class BooleanSchema extends Schema {
|
|
339
393
|
return super.toValidate(toValidateCallback);
|
340
394
|
}
|
341
395
|
/**
|
342
|
-
* This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
|
396
|
+
* This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
|
397
|
+
* result to be a string as well.
|
343
398
|
*
|
344
399
|
* @example
|
345
400
|
* ```ts
|
@@ -358,7 +413,8 @@ export default class BooleanSchema extends Schema {
|
|
358
413
|
return this;
|
359
414
|
}
|
360
415
|
/**
|
361
|
-
* Allows you to set the values that will be considered as true. This is useful when you have a string that can be
|
416
|
+
* Allows you to set the values that will be considered as true. This is useful when you have a string that can be
|
417
|
+
* 'T' or 'F' for example.
|
362
418
|
*
|
363
419
|
* @example
|
364
420
|
* ```ts
|
@@ -373,15 +429,17 @@ export default class BooleanSchema extends Schema {
|
|
373
429
|
this.__trueValues = values;
|
374
430
|
this.__parsers.medium.set('trueValues', (value)=>{
|
375
431
|
const valueExistsInList = values.includes(value);
|
432
|
+
console.log('trueValues', valueExistsInList);
|
376
433
|
return {
|
377
434
|
preventNextParsers: valueExistsInList,
|
378
|
-
value: valueExistsInList
|
435
|
+
value: valueExistsInList ? true : value
|
379
436
|
};
|
380
437
|
});
|
381
438
|
return this;
|
382
439
|
}
|
383
440
|
/**
|
384
|
-
* Allows you to set the values that will be considered as false. This is useful when you have a string that can
|
441
|
+
* Allows you to set the values that will be considered as false. This is useful when you have a string that can
|
442
|
+
* be 'N' or 0 for example.
|
385
443
|
*
|
386
444
|
* @example
|
387
445
|
* ```ts
|
@@ -396,15 +454,17 @@ export default class BooleanSchema extends Schema {
|
|
396
454
|
this.__falseValues = values;
|
397
455
|
this.__parsers.medium.set('falseValues', (value)=>{
|
398
456
|
const valueExistsInList = values.includes(value);
|
457
|
+
console.log('falseValues', valueExistsInList);
|
399
458
|
return {
|
400
459
|
preventNextParsers: valueExistsInList,
|
401
|
-
value:
|
460
|
+
value: valueExistsInList ? false : value
|
402
461
|
};
|
403
462
|
});
|
404
463
|
return this;
|
405
464
|
}
|
406
465
|
/**
|
407
|
-
* This will allow the value to be a number, it does not validate, it just parses inputs as number and allows the
|
466
|
+
* This will allow the value to be a number, it does not validate, it just parses inputs as number and allows the
|
467
|
+
* result to be a string as well.
|
408
468
|
*
|
409
469
|
* @example
|
410
470
|
* ```ts
|
@@ -453,12 +513,6 @@ export default class BooleanSchema extends Schema {
|
|
453
513
|
}
|
454
514
|
static new() {
|
455
515
|
const returnValue = new BooleanSchema();
|
456
|
-
const adapterInstance = getDefaultAdapter();
|
457
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
458
|
-
transformed: false,
|
459
|
-
adapter: adapterInstance,
|
460
|
-
schemas: []
|
461
|
-
};
|
462
516
|
return returnValue;
|
463
517
|
}
|
464
518
|
}
|