@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/string.ts
CHANGED
@@ -1,11 +1,19 @@
|
|
1
|
-
|
2
1
|
import Schema from './schema';
|
3
|
-
import { getDefaultAdapter } from '../conf';
|
4
2
|
import { defaultTransform, defaultTransformToAdapter } from '../utils';
|
5
|
-
import {
|
3
|
+
import { is, nullable, optional } from '../validators/schema';
|
4
|
+
import {
|
5
|
+
email,
|
6
|
+
endsWith,
|
7
|
+
includes,
|
8
|
+
maxLength,
|
9
|
+
minLength,
|
10
|
+
regex,
|
11
|
+
startsWith,
|
12
|
+
stringValidation,
|
13
|
+
uuid
|
14
|
+
} from '../validators/string';
|
6
15
|
|
7
16
|
import type { DefinitionsOfSchemaType } from './types';
|
8
|
-
import type { Narrow } from '@palmares/core';
|
9
17
|
|
10
18
|
export default class StringSchema<
|
11
19
|
TType extends {
|
@@ -21,53 +29,59 @@ export default class StringSchema<
|
|
21
29
|
representation: string;
|
22
30
|
validate: string;
|
23
31
|
},
|
24
|
-
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
32
|
+
TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
|
25
33
|
> extends Schema<TType, TDefinitions> {
|
26
|
-
protected __is
|
27
|
-
value:
|
34
|
+
protected __is?: {
|
35
|
+
value: TType['input'] | TType['input'][];
|
28
36
|
message: string;
|
29
37
|
};
|
30
38
|
|
31
|
-
protected __email
|
39
|
+
protected __email?: {
|
32
40
|
message: string;
|
33
41
|
};
|
34
42
|
|
35
|
-
|
36
|
-
protected __uuid!: {
|
43
|
+
protected __uuid?: {
|
37
44
|
message: string;
|
38
45
|
};
|
39
46
|
|
40
|
-
protected __minLength
|
47
|
+
protected __minLength?: {
|
41
48
|
value: number;
|
42
|
-
inclusive: boolean;
|
43
49
|
message: string;
|
44
50
|
};
|
45
51
|
|
46
|
-
protected __maxLength
|
52
|
+
protected __maxLength?: {
|
47
53
|
value: number;
|
48
|
-
inclusive: boolean;
|
49
54
|
message: string;
|
50
55
|
};
|
51
56
|
|
52
|
-
protected __regex
|
57
|
+
protected __regex?: {
|
53
58
|
value: RegExp;
|
54
59
|
message: string;
|
55
60
|
};
|
56
|
-
protected __endsWith
|
61
|
+
protected __endsWith?: {
|
57
62
|
value: string;
|
58
63
|
message: string;
|
59
64
|
};
|
60
65
|
|
61
|
-
protected __startsWith
|
66
|
+
protected __startsWith?: {
|
62
67
|
value: string;
|
63
68
|
message: string;
|
64
69
|
};
|
65
70
|
|
66
|
-
protected __includes
|
71
|
+
protected __includes?: {
|
67
72
|
value: string;
|
68
73
|
message: string;
|
69
74
|
};
|
70
75
|
|
76
|
+
protected __type: {
|
77
|
+
message: string;
|
78
|
+
check: (value: TType['input']) => boolean;
|
79
|
+
} = {
|
80
|
+
message: 'Invalid type',
|
81
|
+
check: (value: any) => {
|
82
|
+
return typeof value === 'string';
|
83
|
+
}
|
84
|
+
};
|
71
85
|
protected async __transformToAdapter(options: Parameters<Schema['__transformToAdapter']>[0]): Promise<any> {
|
72
86
|
return defaultTransformToAdapter(
|
73
87
|
async (adapter) => {
|
@@ -77,6 +91,10 @@ export default class StringSchema<
|
|
77
91
|
adapter,
|
78
92
|
adapter.string,
|
79
93
|
() => ({
|
94
|
+
type: this.__type,
|
95
|
+
is: this.__is,
|
96
|
+
email: this.__email,
|
97
|
+
uuid: this.__uuid,
|
80
98
|
minLength: this.__minLength,
|
81
99
|
maxLength: this.__maxLength,
|
82
100
|
regex: this.__regex,
|
@@ -87,7 +105,7 @@ export default class StringSchema<
|
|
87
105
|
optional: this.__optional,
|
88
106
|
parsers: {
|
89
107
|
nullable: this.__nullable.allow,
|
90
|
-
optional: this.__optional.allow
|
108
|
+
optional: this.__optional.allow
|
91
109
|
}
|
92
110
|
}),
|
93
111
|
{
|
@@ -95,8 +113,13 @@ export default class StringSchema<
|
|
95
113
|
minLength,
|
96
114
|
endsWith,
|
97
115
|
startsWith,
|
116
|
+
email,
|
117
|
+
uuid,
|
118
|
+
is,
|
98
119
|
regex,
|
99
120
|
includes,
|
121
|
+
nullable,
|
122
|
+
optional
|
100
123
|
},
|
101
124
|
{
|
102
125
|
validatorsIfFallbackOrNotSupported: stringValidation(),
|
@@ -104,10 +127,11 @@ export default class StringSchema<
|
|
104
127
|
// eslint-disable-next-line ts/require-await
|
105
128
|
fallbackIfNotSupported: async () => {
|
106
129
|
return [];
|
107
|
-
}
|
130
|
+
}
|
108
131
|
}
|
109
132
|
);
|
110
133
|
},
|
134
|
+
this,
|
111
135
|
this.__transformedSchemas,
|
112
136
|
options,
|
113
137
|
'number'
|
@@ -115,7 +139,8 @@ export default class StringSchema<
|
|
115
139
|
}
|
116
140
|
|
117
141
|
/**
|
118
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
142
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
143
|
+
* is not supported by default by the schema adapter.
|
119
144
|
*
|
120
145
|
* @example
|
121
146
|
* ```typescript
|
@@ -127,7 +152,8 @@ export default class StringSchema<
|
|
127
152
|
*
|
128
153
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
129
154
|
*
|
130
|
-
*
|
155
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
156
|
+
* console.log(errors);
|
131
157
|
* ```
|
132
158
|
*
|
133
159
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -135,7 +161,13 @@ export default class StringSchema<
|
|
135
161
|
* @param options.isAsync - Whether the callback is async or not. Defaults to true.
|
136
162
|
*/
|
137
163
|
refine(
|
138
|
-
refinementCallback: (
|
164
|
+
refinementCallback: (
|
165
|
+
value: TType['input']
|
166
|
+
) =>
|
167
|
+
| Promise<void | undefined | { code: string; message: string }>
|
168
|
+
| void
|
169
|
+
| undefined
|
170
|
+
| { code: string; message: string }
|
139
171
|
) {
|
140
172
|
return super.refine(refinementCallback) as unknown as StringSchema<
|
141
173
|
{
|
@@ -144,7 +176,8 @@ export default class StringSchema<
|
|
144
176
|
internal: TType['internal'];
|
145
177
|
output: TType['output'];
|
146
178
|
representation: TType['representation'];
|
147
|
-
},
|
179
|
+
},
|
180
|
+
TDefinitions
|
148
181
|
>;
|
149
182
|
}
|
150
183
|
|
@@ -172,8 +205,8 @@ export default class StringSchema<
|
|
172
205
|
*
|
173
206
|
* @returns - The schema we are working with.
|
174
207
|
*/
|
175
|
-
optional(
|
176
|
-
return super.optional(
|
208
|
+
optional() {
|
209
|
+
return super.optional() as unknown as StringSchema<
|
177
210
|
{
|
178
211
|
input: TType['input'] | undefined | null;
|
179
212
|
validate: TType['validate'] | undefined | null;
|
@@ -186,8 +219,36 @@ export default class StringSchema<
|
|
186
219
|
}
|
187
220
|
|
188
221
|
/**
|
189
|
-
*
|
190
|
-
*
|
222
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
223
|
+
*
|
224
|
+
* ```typescript
|
225
|
+
* p.string().optional({ message: 'This value cannot be null', allow: false })
|
226
|
+
* ```
|
227
|
+
*
|
228
|
+
* @param options - The options of nonOptional function
|
229
|
+
* @param options.message - A custom message if the value is undefined.
|
230
|
+
*
|
231
|
+
* @returns - The schema.
|
232
|
+
*/
|
233
|
+
nonOptional(options?: { message: string }) {
|
234
|
+
return super.optional({
|
235
|
+
message: options?.message,
|
236
|
+
allow: false
|
237
|
+
}) as unknown as StringSchema<
|
238
|
+
{
|
239
|
+
input: TType['input'];
|
240
|
+
validate: TType['validate'];
|
241
|
+
internal: TType['internal'];
|
242
|
+
output: TType['output'];
|
243
|
+
representation: TType['representation'];
|
244
|
+
},
|
245
|
+
TDefinitions
|
246
|
+
>;
|
247
|
+
}
|
248
|
+
|
249
|
+
/**
|
250
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
251
|
+
* is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
191
252
|
*
|
192
253
|
* @example
|
193
254
|
* ```typescript
|
@@ -224,14 +285,43 @@ export default class StringSchema<
|
|
224
285
|
}
|
225
286
|
|
226
287
|
/**
|
227
|
-
*
|
228
|
-
*
|
288
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
289
|
+
*
|
290
|
+
* ```typescript
|
291
|
+
* p.string().nullable({ message: 'This value cannot be null', allow: false })
|
292
|
+
* ```
|
293
|
+
*
|
294
|
+
* @param options - The options of nonNullable function
|
295
|
+
* @param options.message - A custom message if the value is null.
|
296
|
+
*
|
297
|
+
* @returns - The schema.
|
298
|
+
*/
|
299
|
+
nonNullable(options?: { message: string }) {
|
300
|
+
return super.nullable({
|
301
|
+
message: options?.message || '',
|
302
|
+
allow: false
|
303
|
+
}) as unknown as StringSchema<
|
304
|
+
{
|
305
|
+
input: TType['input'];
|
306
|
+
validate: TType['validate'];
|
307
|
+
internal: TType['internal'];
|
308
|
+
output: TType['output'];
|
309
|
+
representation: TType['representation'];
|
310
|
+
},
|
311
|
+
TDefinitions
|
312
|
+
>;
|
313
|
+
}
|
314
|
+
|
315
|
+
/**
|
316
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
317
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
229
318
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
230
319
|
* function. This will remove the value from the representation of the schema.
|
231
320
|
*
|
232
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
233
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
234
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
321
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
322
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
323
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
324
|
+
* the argument `toRepresentation` as true as well.
|
235
325
|
*
|
236
326
|
* @example
|
237
327
|
* ```typescript
|
@@ -253,16 +343,17 @@ export default class StringSchema<
|
|
253
343
|
* ```
|
254
344
|
*
|
255
345
|
*
|
256
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
257
|
-
* But if you want to remove the value from the internal representation, you can pass the
|
258
|
-
* Then if you still want to remove the value from the representation, you will need
|
346
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
347
|
+
* the {@link data} function. But if you want to remove the value from the internal representation, you can pass the
|
348
|
+
* argument `toInternal` as true. Then if you still want to remove the value from the representation, you will need
|
349
|
+
* to pass the argument `toRepresentation` as true as well.
|
259
350
|
*
|
260
351
|
* @returns The schema.
|
261
352
|
*/
|
262
353
|
omit<
|
263
354
|
TToInternal extends boolean,
|
264
355
|
TToRepresentation extends boolean = boolean extends TToInternal ? true : false
|
265
|
-
>(args?: { toInternal?: TToInternal
|
356
|
+
>(args?: { toInternal?: TToInternal; toRepresentation?: TToRepresentation }) {
|
266
357
|
return super.omit(args) as unknown as StringSchema<
|
267
358
|
{
|
268
359
|
input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
|
@@ -276,9 +367,9 @@ export default class StringSchema<
|
|
276
367
|
}
|
277
368
|
|
278
369
|
/**
|
279
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
280
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
281
|
-
* of the schema after the save operation.
|
370
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
371
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
372
|
+
* the correct type of the schema after the save operation.
|
282
373
|
*
|
283
374
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
284
375
|
*
|
@@ -332,9 +423,9 @@ export default class StringSchema<
|
|
332
423
|
>;
|
333
424
|
}
|
334
425
|
|
335
|
-
|
336
426
|
/**
|
337
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
427
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
428
|
+
* value will be used.
|
338
429
|
*
|
339
430
|
* @example
|
340
431
|
* ```typescript
|
@@ -363,8 +454,9 @@ export default class StringSchema<
|
|
363
454
|
}
|
364
455
|
|
365
456
|
/**
|
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?
|
457
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
458
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
459
|
+
* No problem, you can use this function to customize the zod schema.
|
368
460
|
*
|
369
461
|
* @example
|
370
462
|
* ```typescript
|
@@ -375,13 +467,13 @@ export default class StringSchema<
|
|
375
467
|
* });
|
376
468
|
*
|
377
469
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
378
|
-
*
|
379
|
-
* console.log(errors);
|
470
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
471
|
+
* console.log(errors);
|
380
472
|
* ```
|
381
473
|
*
|
382
474
|
* @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 want to
|
384
|
-
* to a string so you can save it for future runs.
|
475
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
476
|
+
* compile the underlying schema to a string so you can save it for future runs.
|
385
477
|
*
|
386
478
|
* @returns The schema.
|
387
479
|
*/
|
@@ -395,8 +487,9 @@ export default class StringSchema<
|
|
395
487
|
}
|
396
488
|
|
397
489
|
/**
|
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
|
490
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
491
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
492
|
+
* whatever. Another use case is when you want to return deeply nested recursive data.
|
400
493
|
* The schema maps to itself.
|
401
494
|
*
|
402
495
|
* @example
|
@@ -453,8 +546,9 @@ export default class StringSchema<
|
|
453
546
|
}
|
454
547
|
|
455
548
|
/**
|
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
|
549
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
550
|
+
* you want to transform the value to a type that the schema adapter can understand. For example, you might want
|
551
|
+
* to transform a string to a date. This is the function you use.
|
458
552
|
*
|
459
553
|
* @example
|
460
554
|
* ```typescript
|
@@ -498,8 +592,9 @@ export default class StringSchema<
|
|
498
592
|
}
|
499
593
|
|
500
594
|
/**
|
501
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
502
|
-
* here BEFORE the validation. This pretty much transforms the value to a
|
595
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
596
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a
|
597
|
+
* type that the schema adapter can understand.
|
503
598
|
*
|
504
599
|
* @example
|
505
600
|
* ```typescript
|
@@ -531,7 +626,8 @@ export default class StringSchema<
|
|
531
626
|
}
|
532
627
|
|
533
628
|
/**
|
534
|
-
* Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed.
|
629
|
+
* Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed.
|
630
|
+
* Like a selector or a Choice field.
|
535
631
|
*
|
536
632
|
* @example
|
537
633
|
* ```typescript
|
@@ -540,17 +636,25 @@ export default class StringSchema<
|
|
540
636
|
* const schema = p.string().is(['Argentina', 'Brazil', 'Chile']);
|
541
637
|
*
|
542
638
|
* schema.parse('Argentina'); // { errors: [], parsed: 'Argentina' }
|
543
|
-
*
|
639
|
+
* // { errors: [{
|
640
|
+
* // code: 'invalid_value',
|
641
|
+
* // message: 'The value should be equal to Argentina, Brazil, Chile',
|
642
|
+
* // path: [] }], parsed: 'Uruguay' }
|
643
|
+
* schema.parse('Uruguay');
|
544
644
|
* ```
|
545
645
|
*
|
546
646
|
* @param value - The list of numbers that are allowed
|
547
647
|
*
|
548
648
|
* @returns - The schema instance
|
549
649
|
*/
|
550
|
-
is<const TValue extends TType['input'][]>(
|
650
|
+
is<const TValue extends TType['input'][]>(
|
651
|
+
value: TValue,
|
652
|
+
options?: Partial<Omit<NonNullable<StringSchema['__is']>, 'value'>>
|
653
|
+
) {
|
551
654
|
this.__is = {
|
552
655
|
value,
|
553
|
-
message:
|
656
|
+
message:
|
657
|
+
typeof options?.message === 'string' ? options.message : `The value should be equal to ${value.join(', ')}`
|
554
658
|
};
|
555
659
|
|
556
660
|
return this as any as Schema<
|
@@ -575,7 +679,9 @@ export default class StringSchema<
|
|
575
679
|
* const schema = p.string().endsWith('.com');
|
576
680
|
*
|
577
681
|
* schema.parse('example.com'); // { errors: [], parsed: 'example.com' }
|
578
|
-
*
|
682
|
+
*
|
683
|
+
* // { errors: [{ code: 'endsWith', message: 'The value should end with .com', path: [] }], parsed: 'example.org' }
|
684
|
+
* schema.parse('example.org');
|
579
685
|
* ```
|
580
686
|
*
|
581
687
|
* @param value - The value that the string should end with.
|
@@ -584,10 +690,10 @@ export default class StringSchema<
|
|
584
690
|
*
|
585
691
|
* @returns - The schema instance.
|
586
692
|
*/
|
587
|
-
endsWith(value: string, options?: Partial<Omit<StringSchema['__endsWith']
|
693
|
+
endsWith(value: string, options?: Partial<Omit<NonNullable<StringSchema['__endsWith']>, 'value'>>) {
|
588
694
|
this.__endsWith = {
|
589
695
|
value,
|
590
|
-
message: options?.message || `The value should end with ${value}
|
696
|
+
message: options?.message || `The value should end with ${value}`
|
591
697
|
};
|
592
698
|
return this;
|
593
699
|
}
|
@@ -602,7 +708,11 @@ export default class StringSchema<
|
|
602
708
|
* const schema = p.string().startsWith('https://');
|
603
709
|
*
|
604
710
|
* schema.parse('https://example.com'); // { errors: [], parsed: 'https://example.com' }
|
605
|
-
*
|
711
|
+
* // {
|
712
|
+
* // errors: [{ code: 'startsWith', message: 'The value should start with https://', path: [] }],
|
713
|
+
* // parsed: 'http://example.com'
|
714
|
+
* // }
|
715
|
+
* schema.parse('http://example.com');
|
606
716
|
* ```
|
607
717
|
*
|
608
718
|
* @param value - The value that the string should start with.
|
@@ -611,10 +721,10 @@ export default class StringSchema<
|
|
611
721
|
*
|
612
722
|
* @returns - The schema instance.
|
613
723
|
*/
|
614
|
-
startsWith(value: string
|
724
|
+
startsWith(value: string, options?: Partial<Omit<NonNullable<StringSchema['__startsWith']>, 'value'>>) {
|
615
725
|
this.__startsWith = {
|
616
726
|
value,
|
617
|
-
message: options?.message || `The value should start with ${value}
|
727
|
+
message: options?.message || `The value should start with ${value}`
|
618
728
|
};
|
619
729
|
return this;
|
620
730
|
}
|
@@ -629,7 +739,13 @@ export default class StringSchema<
|
|
629
739
|
* const schema = p.string().includes('for babies');
|
630
740
|
*
|
631
741
|
* schema.parse('Computer graphics for babies'); // { errors: [], parsed: 'Computer graphics for babies' }
|
632
|
-
*
|
742
|
+
* // {
|
743
|
+
* // errors: [{
|
744
|
+
* // code: 'includes',
|
745
|
+
* // message: 'The string value should include the following substring 'for babies',
|
746
|
+
* // path: []
|
747
|
+
* // }], parsed: 'example.org' }
|
748
|
+
* schema.parse('Learn javascript as you were 5');
|
633
749
|
* ```
|
634
750
|
*
|
635
751
|
* @param value - The value that the string should include.
|
@@ -638,10 +754,10 @@ export default class StringSchema<
|
|
638
754
|
*
|
639
755
|
* @returns - The schema instance.
|
640
756
|
*/
|
641
|
-
includes(value: string, options?: Partial<Omit<StringSchema['__includes']
|
757
|
+
includes(value: string, options?: Partial<Omit<NonNullable<StringSchema['__includes']>, 'value'>>) {
|
642
758
|
this.__includes = {
|
643
759
|
value,
|
644
|
-
message: options?.message || `The string value should include the following substring '${value}'
|
760
|
+
message: options?.message || `The string value should include the following substring '${value}'`
|
645
761
|
};
|
646
762
|
return this;
|
647
763
|
}
|
@@ -656,7 +772,15 @@ export default class StringSchema<
|
|
656
772
|
* const schema = p.string().regex(/^[a-z]+$/);
|
657
773
|
*
|
658
774
|
* schema.parse('abc'); // { errors: [], parsed: 'abc' }
|
659
|
-
*
|
775
|
+
* // {
|
776
|
+
* // errors: [{
|
777
|
+
* // code: 'regex',
|
778
|
+
* // message: 'The value should match the following regex /^[a-z]+$/',
|
779
|
+
* // path: []
|
780
|
+
* // }],
|
781
|
+
* // parsed: '123'
|
782
|
+
* // }
|
783
|
+
* schema.parse('123');
|
660
784
|
* ```
|
661
785
|
*
|
662
786
|
* @param value - The regex that the string should match.
|
@@ -665,16 +789,17 @@ export default class StringSchema<
|
|
665
789
|
*
|
666
790
|
* @returns - The schema instance.
|
667
791
|
*/
|
668
|
-
regex(value: RegExp, options?: Partial<Omit<StringSchema['__regex']
|
792
|
+
regex(value: RegExp, options?: Partial<Omit<NonNullable<StringSchema['__regex']>, 'value'>>) {
|
669
793
|
this.__regex = {
|
670
794
|
value,
|
671
|
-
message: options?.message || `The value should match the following regex '${value.toString()}'
|
795
|
+
message: options?.message || `The value should match the following regex '${value.toString()}'`
|
672
796
|
};
|
673
797
|
return this;
|
674
798
|
}
|
675
799
|
|
676
800
|
/**
|
677
|
-
* Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length
|
801
|
+
* Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length
|
802
|
+
* as the maximum length.
|
678
803
|
*
|
679
804
|
* @example
|
680
805
|
* ```typescript
|
@@ -683,7 +808,11 @@ export default class StringSchema<
|
|
683
808
|
* const schema = p.string().maxLength(5);
|
684
809
|
*
|
685
810
|
* schema.parse('12345'); // { errors: [], parsed: '12345' }
|
686
|
-
*
|
811
|
+
* // {
|
812
|
+
* // errors: [{ code: 'maxLength', message: 'The value should have a maximum length of 5', path: [] }],
|
813
|
+
* // parsed: '123
|
814
|
+
* // }
|
815
|
+
* schema.parse('123456');
|
687
816
|
* ```
|
688
817
|
*
|
689
818
|
* @param value - The maximum length that the string should have.
|
@@ -693,17 +822,17 @@ export default class StringSchema<
|
|
693
822
|
*
|
694
823
|
* @returns - The schema instance.
|
695
824
|
*/
|
696
|
-
maxLength(value: number, options?: Partial<Omit<StringSchema['__maxLength']
|
825
|
+
maxLength(value: number, options?: Partial<Omit<NonNullable<StringSchema['__maxLength']>, 'value'>>) {
|
697
826
|
this.__maxLength = {
|
698
827
|
value,
|
699
|
-
message: options?.message || `The value should have a maximum length of ${value}
|
700
|
-
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false,
|
828
|
+
message: options?.message || `The value should have a maximum length of ${value}`
|
701
829
|
};
|
702
830
|
return this;
|
703
831
|
}
|
704
832
|
|
705
833
|
/**
|
706
|
-
* Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length
|
834
|
+
* Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length
|
835
|
+
* as the minimum length.
|
707
836
|
*
|
708
837
|
* @example
|
709
838
|
* ```typescript
|
@@ -712,7 +841,11 @@ export default class StringSchema<
|
|
712
841
|
* const schema = p.string().minLength(5);
|
713
842
|
*
|
714
843
|
* schema.parse('12345'); // { errors: [], parsed: '12345' }
|
715
|
-
*
|
844
|
+
* // {
|
845
|
+
* // errors: [{ code: 'minLength', message: 'The value should have a minimum length of 5', path: [] }],
|
846
|
+
* // parsed: '1234'
|
847
|
+
* // }
|
848
|
+
* schema.parse('1234');
|
716
849
|
* ```
|
717
850
|
*
|
718
851
|
* @param value - The minimum length that the string should have.
|
@@ -722,11 +855,10 @@ export default class StringSchema<
|
|
722
855
|
*
|
723
856
|
* @returns - The schema instance.
|
724
857
|
*/
|
725
|
-
minLength(value: number, options?: Partial<Omit<StringSchema['__minLength']
|
858
|
+
minLength(value: number, options?: Partial<Omit<NonNullable<StringSchema['__minLength']>, 'value'>>) {
|
726
859
|
this.__minLength = {
|
727
860
|
value,
|
728
|
-
message: options?.message || `The value should have a minimum length of ${value}
|
729
|
-
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false,
|
861
|
+
message: options?.message || `The value should have a minimum length of ${value}`
|
730
862
|
};
|
731
863
|
return this;
|
732
864
|
}
|
@@ -740,22 +872,23 @@ export default class StringSchema<
|
|
740
872
|
*
|
741
873
|
* const schema = p.string().uuid();
|
742
874
|
*
|
743
|
-
*
|
875
|
+
* // { errors: [], parsed: '550e8400-e29b-41d4-a716-446655440000' }
|
876
|
+
* schema.parse('550e8400-e29b-41d4-a716-446655440000');
|
744
877
|
* ```
|
745
878
|
*
|
746
879
|
* @param options - The options for the uuid function.
|
747
|
-
* @param options.message - The message to be shown when the value is not a valid UUID. Defaults to
|
880
|
+
* @param options.message - The message to be shown when the value is not a valid UUID. Defaults to
|
881
|
+
* 'The value should be a valid UUID'.
|
748
882
|
*
|
749
883
|
* @returns - The schema instance.
|
750
884
|
*/
|
751
885
|
uuid(options?: StringSchema['__uuid']) {
|
752
886
|
this.__uuid = {
|
753
|
-
message: options?.message || 'The value should be a valid UUID'
|
887
|
+
message: options?.message || 'The value should be a valid UUID'
|
754
888
|
};
|
755
889
|
return this;
|
756
890
|
}
|
757
891
|
|
758
|
-
|
759
892
|
/**
|
760
893
|
* Validates if the string is a valid email or not
|
761
894
|
*
|
@@ -770,14 +903,15 @@ export default class StringSchema<
|
|
770
903
|
* ```
|
771
904
|
*
|
772
905
|
* @param options - The options for the email function.
|
773
|
-
* @param options.message - The message to be shown when the value is not a valid email.
|
906
|
+
* @param options.message - The message to be shown when the value is not a valid email.
|
907
|
+
* Defaults to 'The value should be a valid email'.
|
774
908
|
*
|
775
909
|
* @returns - The schema instance.
|
776
910
|
*/
|
777
911
|
email(options?: StringSchema['__email']) {
|
778
912
|
this.__email = {
|
779
|
-
message: options?.message || 'The value should be a valid email'
|
780
|
-
}
|
913
|
+
message: options?.message || 'The value should be a valid email'
|
914
|
+
};
|
781
915
|
return this;
|
782
916
|
}
|
783
917
|
|
@@ -792,13 +926,6 @@ export default class StringSchema<
|
|
792
926
|
},
|
793
927
|
TDefinitions
|
794
928
|
>();
|
795
|
-
const adapterInstance = getDefaultAdapter();
|
796
|
-
|
797
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
798
|
-
transformed: false,
|
799
|
-
adapter: adapterInstance,
|
800
|
-
schemas: [],
|
801
|
-
};
|
802
929
|
|
803
930
|
return returnValue;
|
804
931
|
}
|