@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/number.ts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
import Schema from './schema';
|
2
|
-
import { getDefaultAdapter } from '../conf';
|
3
2
|
import {
|
4
3
|
DEFAULT_NUMBER_INTEGER_EXCEPTION,
|
5
4
|
DEFAULT_NUMBER_MAX_EXCEPTION,
|
6
|
-
DEFAULT_NUMBER_MIN_EXCEPTION
|
7
|
-
DEFAULT_NUMBER_NEGATIVE_EXCEPTION,
|
5
|
+
DEFAULT_NUMBER_MIN_EXCEPTION
|
8
6
|
} from '../constants';
|
7
|
+
import { convertFromStringBuilder } from '../parsers';
|
9
8
|
import { defaultTransform, defaultTransformToAdapter } from '../utils';
|
10
|
-
import { max, min, numberValidation } from '../validators/number';
|
9
|
+
import { decimalPlaces, integer, max, maxDigits, min, numberValidation } from '../validators/number';
|
10
|
+
import { is, nullable, optional } from '../validators/schema';
|
11
11
|
|
12
12
|
import type { DefinitionsOfSchemaType } from './types';
|
13
13
|
|
@@ -19,53 +19,57 @@ export default class NumberSchema<
|
|
19
19
|
output: any;
|
20
20
|
representation: any;
|
21
21
|
} = {
|
22
|
-
input: number
|
23
|
-
output: number
|
24
|
-
validate: number
|
25
|
-
internal: number
|
26
|
-
representation: number
|
22
|
+
input: number;
|
23
|
+
output: number;
|
24
|
+
validate: number;
|
25
|
+
internal: number;
|
26
|
+
representation: number;
|
27
27
|
},
|
28
|
-
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
28
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
29
29
|
> extends Schema<TType, TDefinitions> {
|
30
|
-
protected
|
30
|
+
protected __allowString!: boolean;
|
31
|
+
|
32
|
+
protected __is?: {
|
31
33
|
value: TType['input'][];
|
32
34
|
message: string;
|
33
35
|
};
|
34
36
|
|
35
|
-
protected __integer
|
37
|
+
protected __integer?: {
|
36
38
|
message: string;
|
37
39
|
};
|
38
40
|
|
39
|
-
protected __maxDigits
|
41
|
+
protected __maxDigits?: {
|
40
42
|
value: number;
|
41
|
-
message: string
|
43
|
+
message: string;
|
42
44
|
};
|
43
45
|
|
44
|
-
protected __decimalPlaces
|
46
|
+
protected __decimalPlaces?: {
|
45
47
|
value: number;
|
46
|
-
message: string
|
48
|
+
message: string;
|
47
49
|
};
|
48
50
|
|
49
|
-
protected __max
|
51
|
+
protected __max?: {
|
50
52
|
value: number;
|
51
53
|
inclusive: boolean;
|
52
54
|
message: string;
|
53
55
|
};
|
54
56
|
|
55
|
-
protected __min
|
57
|
+
protected __min?: {
|
56
58
|
value: number;
|
57
59
|
inclusive: boolean;
|
58
60
|
message: string;
|
59
61
|
};
|
60
62
|
|
61
|
-
protected
|
62
|
-
allowZero: boolean;
|
63
|
-
message: string;
|
64
|
-
};
|
65
|
-
|
66
|
-
protected __allowPositive!: {
|
67
|
-
allowZero: boolean;
|
63
|
+
protected __type: {
|
68
64
|
message: string;
|
65
|
+
check: (value: TType['input']) => boolean;
|
66
|
+
} = {
|
67
|
+
message: 'Invalid type',
|
68
|
+
check: (value: any) => {
|
69
|
+
const isNumber = new RegExp('^-?\\d*(\\.\\d+)?$').test(value);
|
70
|
+
if (typeof value === 'string' && this.__allowString && isNumber) return true;
|
71
|
+
return typeof value === 'number';
|
72
|
+
}
|
69
73
|
};
|
70
74
|
|
71
75
|
protected async __transformToAdapter(options: Parameters<Schema['__transformToAdapter']>[0]): Promise<any> {
|
@@ -79,22 +83,28 @@ export default class NumberSchema<
|
|
79
83
|
() => ({
|
80
84
|
is: this.__is,
|
81
85
|
min: this.__min,
|
82
|
-
allowNegative: this.__allowNegative,
|
83
|
-
allowPositive: this.__allowPositive,
|
84
86
|
max: this.__max,
|
85
87
|
integer: this.__integer,
|
86
88
|
optional: this.__optional,
|
87
89
|
nullable: this.__nullable,
|
88
90
|
maxDigits: this.__maxDigits,
|
89
91
|
decimalPlaces: this.__decimalPlaces,
|
92
|
+
type: this.__type,
|
90
93
|
parsers: {
|
94
|
+
allowString: this.__allowString,
|
91
95
|
nullable: this.__nullable.allow,
|
92
|
-
optional: this.__optional.allow
|
96
|
+
optional: this.__optional.allow
|
93
97
|
}
|
94
98
|
}),
|
95
99
|
{
|
96
100
|
max,
|
97
101
|
min,
|
102
|
+
maxDigits: maxDigits,
|
103
|
+
is: is,
|
104
|
+
optional: optional,
|
105
|
+
nullable: nullable,
|
106
|
+
decimalPlaces: decimalPlaces,
|
107
|
+
integer: integer
|
98
108
|
},
|
99
109
|
{
|
100
110
|
validatorsIfFallbackOrNotSupported: numberValidation(),
|
@@ -102,10 +112,11 @@ export default class NumberSchema<
|
|
102
112
|
// eslint-disable-next-line ts/require-await
|
103
113
|
fallbackIfNotSupported: async () => {
|
104
114
|
return [];
|
105
|
-
}
|
115
|
+
}
|
106
116
|
}
|
107
117
|
);
|
108
118
|
},
|
119
|
+
this,
|
109
120
|
this.__transformedSchemas,
|
110
121
|
options,
|
111
122
|
'number'
|
@@ -113,7 +124,8 @@ export default class NumberSchema<
|
|
113
124
|
}
|
114
125
|
|
115
126
|
/**
|
116
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
127
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something
|
128
|
+
* that is not supported by default by the schema adapter.
|
117
129
|
*
|
118
130
|
* @example
|
119
131
|
* ```typescript
|
@@ -125,7 +137,8 @@ export default class NumberSchema<
|
|
125
137
|
*
|
126
138
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
127
139
|
*
|
128
|
-
* console.log(errors);
|
140
|
+
* console.log(errors);
|
141
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
129
142
|
* ```
|
130
143
|
*
|
131
144
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -135,7 +148,13 @@ export default class NumberSchema<
|
|
135
148
|
* @returns The schema.
|
136
149
|
*/
|
137
150
|
refine(
|
138
|
-
refinementCallback: (
|
151
|
+
refinementCallback: (
|
152
|
+
value: TType['input']
|
153
|
+
) =>
|
154
|
+
| Promise<void | undefined | { code: string; message: string }>
|
155
|
+
| void
|
156
|
+
| undefined
|
157
|
+
| { code: string; message: string }
|
139
158
|
) {
|
140
159
|
return super.refine(refinementCallback) as unknown as NumberSchema<
|
141
160
|
{
|
@@ -144,7 +163,8 @@ export default class NumberSchema<
|
|
144
163
|
internal: TType['internal'];
|
145
164
|
output: TType['output'];
|
146
165
|
representation: TType['representation'];
|
147
|
-
},
|
166
|
+
},
|
167
|
+
TDefinitions
|
148
168
|
>;
|
149
169
|
}
|
150
170
|
|
@@ -186,8 +206,36 @@ export default class NumberSchema<
|
|
186
206
|
}
|
187
207
|
|
188
208
|
/**
|
189
|
-
*
|
190
|
-
*
|
209
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
210
|
+
*
|
211
|
+
* ```typescript
|
212
|
+
* p.string().optional({ message: 'This value cannot be null', allow: false })
|
213
|
+
* ```
|
214
|
+
*
|
215
|
+
* @param options - The options of nonOptional function
|
216
|
+
* @param options.message - A custom message if the value is undefined.
|
217
|
+
*
|
218
|
+
* @returns - The schema.
|
219
|
+
*/
|
220
|
+
nonOptional(options?: { message: string }) {
|
221
|
+
return super.optional({
|
222
|
+
message: options?.message,
|
223
|
+
allow: false
|
224
|
+
}) as unknown as NumberSchema<
|
225
|
+
{
|
226
|
+
input: TType['input'];
|
227
|
+
validate: TType['validate'];
|
228
|
+
internal: TType['internal'];
|
229
|
+
output: TType['output'];
|
230
|
+
representation: TType['representation'];
|
231
|
+
},
|
232
|
+
TDefinitions
|
233
|
+
>;
|
234
|
+
}
|
235
|
+
|
236
|
+
/**
|
237
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
238
|
+
* is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
191
239
|
*
|
192
240
|
* @example
|
193
241
|
* ```typescript
|
@@ -224,14 +272,44 @@ export default class NumberSchema<
|
|
224
272
|
}
|
225
273
|
|
226
274
|
/**
|
227
|
-
*
|
228
|
-
*
|
275
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
276
|
+
*
|
277
|
+
* ```typescript
|
278
|
+
* p.string().nullable({ message: 'This value cannot be null', allow: false })
|
279
|
+
* ```
|
280
|
+
*
|
281
|
+
* @param options - The options of nonNullable function
|
282
|
+
* @param options.message - A custom message if the value is null.
|
283
|
+
*
|
284
|
+
* @returns - The schema.
|
285
|
+
*/
|
286
|
+
nonNullable(options?: { message: string }) {
|
287
|
+
return super.nullable({
|
288
|
+
message: options?.message || '',
|
289
|
+
allow: false
|
290
|
+
}) as unknown as NumberSchema<
|
291
|
+
{
|
292
|
+
input: TType['input'];
|
293
|
+
validate: TType['validate'];
|
294
|
+
internal: TType['internal'];
|
295
|
+
output: TType['output'];
|
296
|
+
representation: TType['representation'];
|
297
|
+
},
|
298
|
+
TDefinitions
|
299
|
+
>;
|
300
|
+
}
|
301
|
+
|
302
|
+
/**
|
303
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
304
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
229
305
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
230
306
|
* function. This will remove the value from the representation of the schema.
|
231
307
|
*
|
232
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
233
|
-
*
|
234
|
-
*
|
308
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
309
|
+
* function.
|
310
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
311
|
+
* as true. Then if you still want to remove the value from the representation, you will need to pass the argument
|
312
|
+
* `toRepresentation` as true as well.
|
235
313
|
*
|
236
314
|
* @example
|
237
315
|
* ```typescript
|
@@ -253,16 +331,18 @@ export default class NumberSchema<
|
|
253
331
|
* ```
|
254
332
|
*
|
255
333
|
*
|
256
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
257
|
-
*
|
258
|
-
*
|
334
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
335
|
+
* the {@link data} function.
|
336
|
+
* But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
|
337
|
+
* as true. Then if you still want to remove the value from the representation, you will need to pass the argument
|
338
|
+
* `toRepresentation` as true as well.
|
259
339
|
*
|
260
340
|
* @returns The schema.
|
261
341
|
*/
|
262
342
|
omit<
|
263
343
|
TToInternal extends boolean,
|
264
344
|
TToRepresentation extends boolean = boolean extends TToInternal ? true : false
|
265
|
-
>(args?: { toInternal?: TToInternal
|
345
|
+
>(args?: { toInternal?: TToInternal; toRepresentation?: TToRepresentation; parent?: boolean }) {
|
266
346
|
return super.omit(args) as unknown as NumberSchema<
|
267
347
|
{
|
268
348
|
input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
|
@@ -276,9 +356,45 @@ export default class NumberSchema<
|
|
276
356
|
}
|
277
357
|
|
278
358
|
/**
|
279
|
-
* This
|
280
|
-
*
|
281
|
-
*
|
359
|
+
* This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
|
360
|
+
* result to be a string as well.
|
361
|
+
*
|
362
|
+
* @example
|
363
|
+
* ```ts
|
364
|
+
* number().allowString().parse('true') // true
|
365
|
+
* ```
|
366
|
+
*
|
367
|
+
* @returns - The schema instance
|
368
|
+
*/
|
369
|
+
allowString() {
|
370
|
+
this.__allowString = true;
|
371
|
+
|
372
|
+
this.__parsers.low.set(
|
373
|
+
'allowString',
|
374
|
+
convertFromStringBuilder((value) => {
|
375
|
+
return {
|
376
|
+
value: Number(value),
|
377
|
+
preventNextParsers: false
|
378
|
+
};
|
379
|
+
})
|
380
|
+
);
|
381
|
+
|
382
|
+
return this as any as NumberSchema<
|
383
|
+
{
|
384
|
+
input: string | TType['input'];
|
385
|
+
output: string | TType['output'];
|
386
|
+
internal: string | TType['internal'];
|
387
|
+
representation: string | TType['representation'];
|
388
|
+
validate: string | TType['validate'];
|
389
|
+
},
|
390
|
+
TDefinitions
|
391
|
+
>;
|
392
|
+
}
|
393
|
+
|
394
|
+
/**
|
395
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
396
|
+
* source like a database. You should always return the schema after you save the value, that way we will always
|
397
|
+
* have the correct type of the schema after the save operation.
|
282
398
|
*
|
283
399
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
284
400
|
*
|
@@ -332,9 +448,9 @@ export default class NumberSchema<
|
|
332
448
|
>;
|
333
449
|
}
|
334
450
|
|
335
|
-
|
336
451
|
/**
|
337
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
452
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
453
|
+
* value will be used.
|
338
454
|
*
|
339
455
|
* @example
|
340
456
|
* ```typescript
|
@@ -363,8 +479,9 @@ export default class NumberSchema<
|
|
363
479
|
}
|
364
480
|
|
365
481
|
/**
|
366
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
367
|
-
* the custom schema your own way. Our API does not support passthrough?
|
482
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
483
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
484
|
+
* No problem, you can use this function to customize the zod schema.
|
368
485
|
*
|
369
486
|
* @example
|
370
487
|
* ```typescript
|
@@ -376,12 +493,13 @@ export default class NumberSchema<
|
|
376
493
|
*
|
377
494
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
378
495
|
*
|
379
|
-
* console.log(errors);
|
496
|
+
* console.log(errors);
|
497
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
380
498
|
* ```
|
381
499
|
*
|
382
500
|
* @param callback - The callback that will be called to customize the schema.
|
383
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you
|
384
|
-
* to a string so you can save it for future runs.
|
501
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you
|
502
|
+
* want to compile the underlying schema to a string so you can save it for future runs.
|
385
503
|
*
|
386
504
|
* @returns The schema.
|
387
505
|
*/
|
@@ -395,8 +513,9 @@ export default class NumberSchema<
|
|
395
513
|
}
|
396
514
|
|
397
515
|
/**
|
398
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
399
|
-
* control to add data cleaning for example, transforming the data and
|
516
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
517
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
518
|
+
* whatever. Another use case is when you want to return deeply nested recursive data.
|
400
519
|
* The schema maps to itself.
|
401
520
|
*
|
402
521
|
* @example
|
@@ -453,8 +572,9 @@ export default class NumberSchema<
|
|
453
572
|
}
|
454
573
|
|
455
574
|
/**
|
456
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
457
|
-
* to a type that the schema adapter can understand. For example, you might want
|
575
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
576
|
+
* you want to transform the value to a type that the schema adapter can understand. For example, you might want
|
577
|
+
* to transform a string to a date. This is the function you use.
|
458
578
|
*
|
459
579
|
* @example
|
460
580
|
* ```typescript
|
@@ -498,7 +618,8 @@ export default class NumberSchema<
|
|
498
618
|
}
|
499
619
|
|
500
620
|
/**
|
501
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
621
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
622
|
+
* you can convert that string to a date
|
502
623
|
* here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
|
503
624
|
*
|
504
625
|
* @example
|
@@ -550,10 +671,14 @@ export default class NumberSchema<
|
|
550
671
|
*
|
551
672
|
* @returns - The schema instance
|
552
673
|
*/
|
553
|
-
is<const TValue extends TType['input'][]>(
|
674
|
+
is<const TValue extends TType['input'][]>(
|
675
|
+
value: TValue,
|
676
|
+
options?: Partial<Omit<NonNullable<NumberSchema['__is']>, 'value'>>
|
677
|
+
) {
|
554
678
|
this.__is = {
|
555
679
|
value,
|
556
|
-
message:
|
680
|
+
message:
|
681
|
+
typeof options?.message === 'string' ? options.message : `The value should be equal to ${value.join(',')}`
|
557
682
|
};
|
558
683
|
|
559
684
|
return this as any as Schema<
|
@@ -569,7 +694,8 @@ export default class NumberSchema<
|
|
569
694
|
}
|
570
695
|
|
571
696
|
/**
|
572
|
-
* Allows only numbers that are less than the value passed. If you want to allow the number to be equal to the value,
|
697
|
+
* Allows only numbers that are less than the value passed. If you want to allow the number to be equal to the value,
|
698
|
+
* you can pass the option `inclusive` as `true`.
|
573
699
|
* Otherwise, it will only allow numbers less than the value.
|
574
700
|
*
|
575
701
|
* @example
|
@@ -606,13 +732,14 @@ export default class NumberSchema<
|
|
606
732
|
this.__max = {
|
607
733
|
value,
|
608
734
|
inclusive,
|
609
|
-
message
|
735
|
+
message
|
610
736
|
};
|
611
737
|
return this as unknown as NumberSchema<TType, TDefinitions> & { is: never };
|
612
738
|
}
|
613
739
|
|
614
740
|
/**
|
615
|
-
* This method will validate if the number is greater than the value passed. If you want to allow the number to be
|
741
|
+
* This method will validate if the number is greater than the value passed. If you want to allow the number to be
|
742
|
+
* equal to the value, you can pass the option `inclusive` as `true`.
|
616
743
|
* Otherwise, it will only allow numbers greater than the value.
|
617
744
|
*
|
618
745
|
* @example
|
@@ -650,101 +777,15 @@ export default class NumberSchema<
|
|
650
777
|
this.__min = {
|
651
778
|
value,
|
652
779
|
inclusive,
|
653
|
-
message
|
780
|
+
message
|
654
781
|
};
|
655
782
|
|
656
783
|
return this;
|
657
784
|
}
|
658
785
|
|
659
786
|
/**
|
660
|
-
*
|
661
|
-
*
|
662
|
-
* @example
|
663
|
-
* ```typescript
|
664
|
-
* import * as p from '@palmares/schema';
|
665
|
-
*
|
666
|
-
* const schema = p.number().negative();
|
667
|
-
*
|
668
|
-
* schema.parse(-10); // { errors: [], parsed: -10 }
|
669
|
-
* schema.parse(0); // { errors: [{ code: 'negative', message: 'The number should be negative' }], parsed: 0 }
|
670
|
-
*
|
671
|
-
* const schema = p.number().negative({ allowZero: true });
|
672
|
-
*
|
673
|
-
* schema.parse(0); // { errors: [], parsed: 0 }
|
674
|
-
* ```
|
675
|
-
*
|
676
|
-
* @param options - The options to be passed to the validation
|
677
|
-
* @param options.allowZero - If you want to allow zero, you can pass this option as `true`. Otherwise, it will only allow negative numbers.
|
678
|
-
* @param options.message - The message to be returned if the validation fails
|
679
|
-
*
|
680
|
-
* @returns - The schema instance
|
681
|
-
*/
|
682
|
-
negative(options?: { allowZero?: boolean; message?: string }) {
|
683
|
-
const allowZero = typeof options?.allowZero === 'boolean' ? options.allowZero : true;
|
684
|
-
const message =
|
685
|
-
typeof options?.message === 'string' ? options.message : DEFAULT_NUMBER_NEGATIVE_EXCEPTION(allowZero);
|
686
|
-
|
687
|
-
this.__allowNegative = {
|
688
|
-
allowZero,
|
689
|
-
message,
|
690
|
-
};
|
691
|
-
return this as unknown as NumberSchema<
|
692
|
-
{
|
693
|
-
input: TType['input'];
|
694
|
-
output: TType['output'];
|
695
|
-
representation: TType['representation'];
|
696
|
-
internal: TType['internal'];
|
697
|
-
validate: TType['validate'];
|
698
|
-
},
|
699
|
-
TDefinitions
|
700
|
-
>;
|
701
|
-
}
|
702
|
-
|
703
|
-
/**
|
704
|
-
* Allows only positive numbers. If you want to allow zero, you can pass the option `allowZero` as `true`. Otherwise, it will only allow positive numbers greater than zero.
|
705
|
-
*
|
706
|
-
* @example
|
707
|
-
* ```typescript
|
708
|
-
* import * as p from '@palmares/schema';
|
709
|
-
*
|
710
|
-
* const schema = p.number().positive();
|
711
|
-
*
|
712
|
-
* schema.parse(10); // { errors: [], parsed: 10 }
|
713
|
-
* schema.parse(0); // { errors: [{ code: 'positive', message: 'The number should be positive' }], parsed: 0 }
|
714
|
-
*
|
715
|
-
* const schema = p.number().positive({ allowZero: true });
|
716
|
-
* schema.parse(0); // { errors: [], parsed: 0 }
|
717
|
-
* ```
|
718
|
-
*
|
719
|
-
* @param options - The options to be passed to the validation
|
720
|
-
* @param options.allowZero - If you want to allow zero, you can pass this option as `true`. Otherwise, it will only allow positive numbers greater than zero.
|
721
|
-
* @param options.message - The message to be returned if the validation fails
|
722
|
-
*
|
723
|
-
* @returns - The schema instance
|
724
|
-
*/
|
725
|
-
positive(options?: { allowZero?: boolean; message?: string }) {
|
726
|
-
const allowZero = typeof options?.allowZero === 'boolean' ? options.allowZero : true;
|
727
|
-
const message =
|
728
|
-
typeof options?.message === 'string' ? options.message : DEFAULT_NUMBER_NEGATIVE_EXCEPTION(allowZero);
|
729
|
-
|
730
|
-
this.__allowPositive = {
|
731
|
-
allowZero,
|
732
|
-
message,
|
733
|
-
};
|
734
|
-
return this as unknown as NumberSchema<
|
735
|
-
{
|
736
|
-
input: TType['input'];
|
737
|
-
output: TType['output'];
|
738
|
-
representation: TType['representation'];
|
739
|
-
internal: TType['internal'];
|
740
|
-
validate: TType['validate'];
|
741
|
-
},
|
742
|
-
TDefinitions
|
743
|
-
>;
|
744
|
-
}
|
745
|
-
|
746
|
-
/**
|
747
|
-
* This method will validate the number to have the exact number of decimal places. It's usually useful for decimal numbers like currencies.
|
787
|
+
* This method will validate the number to have the exact number of decimal places. It's usually useful for decimal
|
788
|
+
* numbers like currencies.
|
748
789
|
*
|
749
790
|
* @example
|
750
791
|
* ```
|
@@ -752,7 +793,8 @@ export default class NumberSchema<
|
|
752
793
|
*
|
753
794
|
* schema.parse(10.00); // { errors: [], parsed: 10.00}
|
754
795
|
*
|
755
|
-
* schema.parse(10.000);
|
796
|
+
* schema.parse(10.000);
|
797
|
+
* // { errors: [{ code: 'decimal_places', message: 'The number should have 2 decimal places' }], parsed: 10.000}
|
756
798
|
* ```
|
757
799
|
*
|
758
800
|
* @param value - The number of decimal places.
|
@@ -762,11 +804,12 @@ export default class NumberSchema<
|
|
762
804
|
* @returns The schema so you can chain other methods.
|
763
805
|
*/
|
764
806
|
decimalPlaces(value: number, options?: { message?: string }) {
|
765
|
-
const message =
|
807
|
+
const message =
|
808
|
+
typeof options?.message === 'string' ? options.message : `The number should have ${value} decimal places`;
|
766
809
|
|
767
810
|
this.__decimalPlaces = {
|
768
811
|
value,
|
769
|
-
message
|
812
|
+
message
|
770
813
|
};
|
771
814
|
return this as unknown as NumberSchema<
|
772
815
|
{
|
@@ -781,8 +824,8 @@ export default class NumberSchema<
|
|
781
824
|
}
|
782
825
|
|
783
826
|
/**
|
784
|
-
* This method will validate the number to have at most the number of digits specified. If used in conjunction with
|
785
|
-
* value of the decimal places.
|
827
|
+
* This method will validate the number to have at most the number of digits specified. If used in conjunction with
|
828
|
+
* {@link decimalPlaces}, this number should be bigger than the value of the decimal places.
|
786
829
|
*
|
787
830
|
* Think about that
|
788
831
|
*
|
@@ -805,11 +848,12 @@ export default class NumberSchema<
|
|
805
848
|
* @returns - The schema so you can chain other methods.
|
806
849
|
*/
|
807
850
|
maxDigits(value: number, options?: { message?: string }) {
|
808
|
-
const message =
|
851
|
+
const message =
|
852
|
+
typeof options?.message === 'string' ? options.message : `The number should have at most ${value} digits`;
|
809
853
|
|
810
854
|
this.__maxDigits = {
|
811
855
|
value,
|
812
|
-
message
|
856
|
+
message
|
813
857
|
};
|
814
858
|
return this as unknown as NumberSchema<
|
815
859
|
{
|
@@ -845,7 +889,7 @@ export default class NumberSchema<
|
|
845
889
|
const message = typeof options?.message === 'string' ? options.message : DEFAULT_NUMBER_INTEGER_EXCEPTION();
|
846
890
|
|
847
891
|
this.__integer = {
|
848
|
-
message
|
892
|
+
message
|
849
893
|
};
|
850
894
|
return this as unknown as NumberSchema<
|
851
895
|
{
|
@@ -862,21 +906,14 @@ export default class NumberSchema<
|
|
862
906
|
static new<TDefinitions extends DefinitionsOfSchemaType>() {
|
863
907
|
const returnValue = new NumberSchema<
|
864
908
|
{
|
865
|
-
input: number
|
866
|
-
output: number
|
867
|
-
internal: number
|
868
|
-
representation: number
|
869
|
-
validate: number
|
909
|
+
input: number;
|
910
|
+
output: number;
|
911
|
+
internal: number;
|
912
|
+
representation: number;
|
913
|
+
validate: number;
|
870
914
|
},
|
871
915
|
TDefinitions
|
872
916
|
>();
|
873
|
-
const adapterInstance = getDefaultAdapter();
|
874
|
-
|
875
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
876
|
-
transformed: false,
|
877
|
-
adapter: adapterInstance,
|
878
|
-
schemas: [],
|
879
|
-
};
|
880
917
|
|
881
918
|
return returnValue;
|
882
919
|
}
|