@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,7 +1,7 @@
|
|
1
1
|
import Schema from './schema';
|
2
|
-
import { getDefaultAdapter } from '../conf';
|
3
2
|
import { defaultTransform, defaultTransformToAdapter } from '../utils';
|
4
|
-
import {
|
3
|
+
import { is, nullable, optional } from '../validators/schema';
|
4
|
+
import { email, endsWith, includes, maxLength, minLength, regex, startsWith, stringValidation, uuid } from '../validators/string';
|
5
5
|
export default class StringSchema extends Schema {
|
6
6
|
__is;
|
7
7
|
__email;
|
@@ -12,9 +12,19 @@ export default class StringSchema extends Schema {
|
|
12
12
|
__endsWith;
|
13
13
|
__startsWith;
|
14
14
|
__includes;
|
15
|
+
__type = {
|
16
|
+
message: 'Invalid type',
|
17
|
+
check: (value)=>{
|
18
|
+
return typeof value === 'string';
|
19
|
+
}
|
20
|
+
};
|
15
21
|
async __transformToAdapter(options) {
|
16
22
|
return defaultTransformToAdapter(async (adapter)=>{
|
17
23
|
return defaultTransform('string', this, adapter, adapter.string, ()=>({
|
24
|
+
type: this.__type,
|
25
|
+
is: this.__is,
|
26
|
+
email: this.__email,
|
27
|
+
uuid: this.__uuid,
|
18
28
|
minLength: this.__minLength,
|
19
29
|
maxLength: this.__maxLength,
|
20
30
|
regex: this.__regex,
|
@@ -32,8 +42,13 @@ export default class StringSchema extends Schema {
|
|
32
42
|
minLength,
|
33
43
|
endsWith,
|
34
44
|
startsWith,
|
45
|
+
email,
|
46
|
+
uuid,
|
47
|
+
is,
|
35
48
|
regex,
|
36
|
-
includes
|
49
|
+
includes,
|
50
|
+
nullable,
|
51
|
+
optional
|
37
52
|
}, {
|
38
53
|
validatorsIfFallbackOrNotSupported: stringValidation(),
|
39
54
|
shouldAddStringVersion: options.shouldAddStringVersion,
|
@@ -42,10 +57,11 @@ export default class StringSchema extends Schema {
|
|
42
57
|
return [];
|
43
58
|
}
|
44
59
|
});
|
45
|
-
}, this.__transformedSchemas, options, 'number');
|
60
|
+
}, this, this.__transformedSchemas, options, 'number');
|
46
61
|
}
|
47
62
|
/**
|
48
|
-
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
63
|
+
* This let's you refine the schema with custom validations. This is useful when you want to validate something that
|
64
|
+
* is not supported by default by the schema adapter.
|
49
65
|
*
|
50
66
|
* @example
|
51
67
|
* ```typescript
|
@@ -57,7 +73,8 @@ export default class StringSchema extends Schema {
|
|
57
73
|
*
|
58
74
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
59
75
|
*
|
60
|
-
*
|
76
|
+
* // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
|
77
|
+
* console.log(errors);
|
61
78
|
* ```
|
62
79
|
*
|
63
80
|
* @param refinementCallback - The callback that will be called to validate the value.
|
@@ -89,12 +106,29 @@ export default class StringSchema extends Schema {
|
|
89
106
|
* ```
|
90
107
|
*
|
91
108
|
* @returns - The schema we are working with.
|
92
|
-
*/ optional(
|
93
|
-
return super.optional(
|
109
|
+
*/ optional() {
|
110
|
+
return super.optional();
|
94
111
|
}
|
95
112
|
/**
|
96
|
-
*
|
97
|
-
*
|
113
|
+
* Just adds a message when the value is undefined. It's just a syntax sugar for
|
114
|
+
*
|
115
|
+
* ```typescript
|
116
|
+
* p.string().optional({ message: 'This value cannot be null', allow: false })
|
117
|
+
* ```
|
118
|
+
*
|
119
|
+
* @param options - The options of nonOptional function
|
120
|
+
* @param options.message - A custom message if the value is undefined.
|
121
|
+
*
|
122
|
+
* @returns - The schema.
|
123
|
+
*/ nonOptional(options) {
|
124
|
+
return super.optional({
|
125
|
+
message: options?.message,
|
126
|
+
allow: false
|
127
|
+
});
|
128
|
+
}
|
129
|
+
/**
|
130
|
+
* Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
|
131
|
+
* is NULL by setting the { message: 'Your custom message', allow: false } on the options.
|
98
132
|
*
|
99
133
|
* @example
|
100
134
|
* ```typescript
|
@@ -120,14 +154,32 @@ export default class StringSchema extends Schema {
|
|
120
154
|
return super.nullable(options);
|
121
155
|
}
|
122
156
|
/**
|
123
|
-
*
|
124
|
-
*
|
157
|
+
* Just adds a message when the value is null. It's just a syntax sugar for
|
158
|
+
*
|
159
|
+
* ```typescript
|
160
|
+
* p.string().nullable({ message: 'This value cannot be null', allow: false })
|
161
|
+
* ```
|
162
|
+
*
|
163
|
+
* @param options - The options of nonNullable function
|
164
|
+
* @param options.message - A custom message if the value is null.
|
165
|
+
*
|
166
|
+
* @returns - The schema.
|
167
|
+
*/ nonNullable(options) {
|
168
|
+
return super.nullable({
|
169
|
+
message: options?.message || '',
|
170
|
+
allow: false
|
171
|
+
});
|
172
|
+
}
|
173
|
+
/**
|
174
|
+
* This method will remove the value from the representation of the schema. If the value is undefined it will keep
|
175
|
+
* that way otherwise it will set the value to undefined after it's validated.
|
125
176
|
* This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
|
126
177
|
* function. This will remove the value from the representation of the schema.
|
127
178
|
*
|
128
|
-
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
129
|
-
* But if you want to remove the value from the internal representation, you can pass the argument
|
130
|
-
* Then if you still want to remove the value from the representation, you will need to pass
|
179
|
+
* By default, the value will be removed just from the representation, in other words, when you call the {@link data}
|
180
|
+
* function. But if you want to remove the value from the internal representation, you can pass the argument
|
181
|
+
* `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
|
182
|
+
* the argument `toRepresentation` as true as well.
|
131
183
|
*
|
132
184
|
* @example
|
133
185
|
* ```typescript
|
@@ -149,18 +201,19 @@ export default class StringSchema extends Schema {
|
|
149
201
|
* ```
|
150
202
|
*
|
151
203
|
*
|
152
|
-
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
153
|
-
* But if you want to remove the value from the internal representation, you can pass the
|
154
|
-
* Then if you still want to remove the value from the representation, you will need
|
204
|
+
* @param args - By default, the value will be removed just from the representation, in other words, when you call
|
205
|
+
* the {@link data} function. But if you want to remove the value from the internal representation, you can pass the
|
206
|
+
* argument `toInternal` as true. Then if you still want to remove the value from the representation, you will need
|
207
|
+
* to pass the argument `toRepresentation` as true as well.
|
155
208
|
*
|
156
209
|
* @returns The schema.
|
157
210
|
*/ omit(args) {
|
158
211
|
return super.omit(args);
|
159
212
|
}
|
160
213
|
/**
|
161
|
-
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
162
|
-
* like a database. You should always return the schema after you save the value, that way we will always have
|
163
|
-
* of the schema after the save operation.
|
214
|
+
* This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
|
215
|
+
* source like a database. You should always return the schema after you save the value, that way we will always have
|
216
|
+
* the correct type of the schema after the save operation.
|
164
217
|
*
|
165
218
|
* You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
|
166
219
|
*
|
@@ -202,7 +255,8 @@ export default class StringSchema extends Schema {
|
|
202
255
|
return super.onSave(callback);
|
203
256
|
}
|
204
257
|
/**
|
205
|
-
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
258
|
+
* This function is used to add a default value to the schema. If the value is either undefined or null, the default
|
259
|
+
* value will be used.
|
206
260
|
*
|
207
261
|
* @example
|
208
262
|
* ```typescript
|
@@ -218,8 +272,9 @@ export default class StringSchema extends Schema {
|
|
218
272
|
return super.default(defaultValueOrFunction);
|
219
273
|
}
|
220
274
|
/**
|
221
|
-
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
222
|
-
* the custom schema your own way. Our API does not support passthrough?
|
275
|
+
* This function let's you customize the schema your own way. After we translate the schema on the adapter we call
|
276
|
+
* this function to let you customize the custom schema your own way. Our API does not support passthrough?
|
277
|
+
* No problem, you can use this function to customize the zod schema.
|
223
278
|
*
|
224
279
|
* @example
|
225
280
|
* ```typescript
|
@@ -230,21 +285,22 @@ export default class StringSchema extends Schema {
|
|
230
285
|
* });
|
231
286
|
*
|
232
287
|
* const { errors, parsed } = await numberSchema.parse(-1);
|
233
|
-
*
|
234
|
-
* console.log(errors);
|
288
|
+
* // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
|
289
|
+
* console.log(errors);
|
235
290
|
* ```
|
236
291
|
*
|
237
292
|
* @param callback - The callback that will be called to customize the schema.
|
238
|
-
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
239
|
-
* to a string so you can save it for future runs.
|
293
|
+
* @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
|
294
|
+
* compile the underlying schema to a string so you can save it for future runs.
|
240
295
|
*
|
241
296
|
* @returns The schema.
|
242
297
|
*/ extends(callback, toStringCallback) {
|
243
298
|
return super.extends(callback, toStringCallback);
|
244
299
|
}
|
245
300
|
/**
|
246
|
-
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
247
|
-
* control to add data cleaning for example, transforming the data and
|
301
|
+
* This function is used to transform the value to the representation of the schema. When using the {@link data}
|
302
|
+
* function. With this function you have full control to add data cleaning for example, transforming the data and
|
303
|
+
* whatever. Another use case is when you want to return deeply nested recursive data.
|
248
304
|
* The schema maps to itself.
|
249
305
|
*
|
250
306
|
* @example
|
@@ -288,8 +344,9 @@ export default class StringSchema extends Schema {
|
|
288
344
|
return super.toRepresentation(toRepresentationCallback);
|
289
345
|
}
|
290
346
|
/**
|
291
|
-
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
292
|
-
* to a type that the schema adapter can understand. For example, you might want
|
347
|
+
* This function is used to transform the value to the internal representation of the schema. This is useful when
|
348
|
+
* you want to transform the value to a type that the schema adapter can understand. For example, you might want
|
349
|
+
* to transform a string to a date. This is the function you use.
|
293
350
|
*
|
294
351
|
* @example
|
295
352
|
* ```typescript
|
@@ -322,8 +379,9 @@ export default class StringSchema extends Schema {
|
|
322
379
|
return super.toInternal(toInternalCallback);
|
323
380
|
}
|
324
381
|
/**
|
325
|
-
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
326
|
-
* here BEFORE the validation. This pretty much transforms the value to a
|
382
|
+
* Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
|
383
|
+
* you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a
|
384
|
+
* type that the schema adapter can understand.
|
327
385
|
*
|
328
386
|
* @example
|
329
387
|
* ```typescript
|
@@ -344,7 +402,8 @@ export default class StringSchema extends Schema {
|
|
344
402
|
return super.toValidate(toValidateCallback);
|
345
403
|
}
|
346
404
|
/**
|
347
|
-
* Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed.
|
405
|
+
* Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed.
|
406
|
+
* Like a selector or a Choice field.
|
348
407
|
*
|
349
408
|
* @example
|
350
409
|
* ```typescript
|
@@ -353,16 +412,20 @@ export default class StringSchema extends Schema {
|
|
353
412
|
* const schema = p.string().is(['Argentina', 'Brazil', 'Chile']);
|
354
413
|
*
|
355
414
|
* schema.parse('Argentina'); // { errors: [], parsed: 'Argentina' }
|
356
|
-
*
|
415
|
+
* // { errors: [{
|
416
|
+
* // code: 'invalid_value',
|
417
|
+
* // message: 'The value should be equal to Argentina, Brazil, Chile',
|
418
|
+
* // path: [] }], parsed: 'Uruguay' }
|
419
|
+
* schema.parse('Uruguay');
|
357
420
|
* ```
|
358
421
|
*
|
359
422
|
* @param value - The list of numbers that are allowed
|
360
423
|
*
|
361
424
|
* @returns - The schema instance
|
362
|
-
*/ is(value) {
|
425
|
+
*/ is(value, options) {
|
363
426
|
this.__is = {
|
364
427
|
value,
|
365
|
-
message: `The value should be equal to ${value.join(', ')}`
|
428
|
+
message: typeof options?.message === 'string' ? options.message : `The value should be equal to ${value.join(', ')}`
|
366
429
|
};
|
367
430
|
return this;
|
368
431
|
}
|
@@ -376,7 +439,9 @@ export default class StringSchema extends Schema {
|
|
376
439
|
* const schema = p.string().endsWith('.com');
|
377
440
|
*
|
378
441
|
* schema.parse('example.com'); // { errors: [], parsed: 'example.com' }
|
379
|
-
*
|
442
|
+
*
|
443
|
+
* // { errors: [{ code: 'endsWith', message: 'The value should end with .com', path: [] }], parsed: 'example.org' }
|
444
|
+
* schema.parse('example.org');
|
380
445
|
* ```
|
381
446
|
*
|
382
447
|
* @param value - The value that the string should end with.
|
@@ -401,7 +466,11 @@ export default class StringSchema extends Schema {
|
|
401
466
|
* const schema = p.string().startsWith('https://');
|
402
467
|
*
|
403
468
|
* schema.parse('https://example.com'); // { errors: [], parsed: 'https://example.com' }
|
404
|
-
*
|
469
|
+
* // {
|
470
|
+
* // errors: [{ code: 'startsWith', message: 'The value should start with https://', path: [] }],
|
471
|
+
* // parsed: 'http://example.com'
|
472
|
+
* // }
|
473
|
+
* schema.parse('http://example.com');
|
405
474
|
* ```
|
406
475
|
*
|
407
476
|
* @param value - The value that the string should start with.
|
@@ -426,7 +495,13 @@ export default class StringSchema extends Schema {
|
|
426
495
|
* const schema = p.string().includes('for babies');
|
427
496
|
*
|
428
497
|
* schema.parse('Computer graphics for babies'); // { errors: [], parsed: 'Computer graphics for babies' }
|
429
|
-
*
|
498
|
+
* // {
|
499
|
+
* // errors: [{
|
500
|
+
* // code: 'includes',
|
501
|
+
* // message: 'The string value should include the following substring 'for babies',
|
502
|
+
* // path: []
|
503
|
+
* // }], parsed: 'example.org' }
|
504
|
+
* schema.parse('Learn javascript as you were 5');
|
430
505
|
* ```
|
431
506
|
*
|
432
507
|
* @param value - The value that the string should include.
|
@@ -451,7 +526,15 @@ export default class StringSchema extends Schema {
|
|
451
526
|
* const schema = p.string().regex(/^[a-z]+$/);
|
452
527
|
*
|
453
528
|
* schema.parse('abc'); // { errors: [], parsed: 'abc' }
|
454
|
-
*
|
529
|
+
* // {
|
530
|
+
* // errors: [{
|
531
|
+
* // code: 'regex',
|
532
|
+
* // message: 'The value should match the following regex /^[a-z]+$/',
|
533
|
+
* // path: []
|
534
|
+
* // }],
|
535
|
+
* // parsed: '123'
|
536
|
+
* // }
|
537
|
+
* schema.parse('123');
|
455
538
|
* ```
|
456
539
|
*
|
457
540
|
* @param value - The regex that the string should match.
|
@@ -467,7 +550,8 @@ export default class StringSchema extends Schema {
|
|
467
550
|
return this;
|
468
551
|
}
|
469
552
|
/**
|
470
|
-
* Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length
|
553
|
+
* Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length
|
554
|
+
* as the maximum length.
|
471
555
|
*
|
472
556
|
* @example
|
473
557
|
* ```typescript
|
@@ -476,7 +560,11 @@ export default class StringSchema extends Schema {
|
|
476
560
|
* const schema = p.string().maxLength(5);
|
477
561
|
*
|
478
562
|
* schema.parse('12345'); // { errors: [], parsed: '12345' }
|
479
|
-
*
|
563
|
+
* // {
|
564
|
+
* // errors: [{ code: 'maxLength', message: 'The value should have a maximum length of 5', path: [] }],
|
565
|
+
* // parsed: '123
|
566
|
+
* // }
|
567
|
+
* schema.parse('123456');
|
480
568
|
* ```
|
481
569
|
*
|
482
570
|
* @param value - The maximum length that the string should have.
|
@@ -488,13 +576,13 @@ export default class StringSchema extends Schema {
|
|
488
576
|
*/ maxLength(value, options) {
|
489
577
|
this.__maxLength = {
|
490
578
|
value,
|
491
|
-
message: options?.message || `The value should have a maximum length of ${value}
|
492
|
-
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false
|
579
|
+
message: options?.message || `The value should have a maximum length of ${value}`
|
493
580
|
};
|
494
581
|
return this;
|
495
582
|
}
|
496
583
|
/**
|
497
|
-
* Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length
|
584
|
+
* Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length
|
585
|
+
* as the minimum length.
|
498
586
|
*
|
499
587
|
* @example
|
500
588
|
* ```typescript
|
@@ -503,7 +591,11 @@ export default class StringSchema extends Schema {
|
|
503
591
|
* const schema = p.string().minLength(5);
|
504
592
|
*
|
505
593
|
* schema.parse('12345'); // { errors: [], parsed: '12345' }
|
506
|
-
*
|
594
|
+
* // {
|
595
|
+
* // errors: [{ code: 'minLength', message: 'The value should have a minimum length of 5', path: [] }],
|
596
|
+
* // parsed: '1234'
|
597
|
+
* // }
|
598
|
+
* schema.parse('1234');
|
507
599
|
* ```
|
508
600
|
*
|
509
601
|
* @param value - The minimum length that the string should have.
|
@@ -515,8 +607,7 @@ export default class StringSchema extends Schema {
|
|
515
607
|
*/ minLength(value, options) {
|
516
608
|
this.__minLength = {
|
517
609
|
value,
|
518
|
-
message: options?.message || `The value should have a minimum length of ${value}
|
519
|
-
inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false
|
610
|
+
message: options?.message || `The value should have a minimum length of ${value}`
|
520
611
|
};
|
521
612
|
return this;
|
522
613
|
}
|
@@ -529,11 +620,13 @@ export default class StringSchema extends Schema {
|
|
529
620
|
*
|
530
621
|
* const schema = p.string().uuid();
|
531
622
|
*
|
532
|
-
*
|
623
|
+
* // { errors: [], parsed: '550e8400-e29b-41d4-a716-446655440000' }
|
624
|
+
* schema.parse('550e8400-e29b-41d4-a716-446655440000');
|
533
625
|
* ```
|
534
626
|
*
|
535
627
|
* @param options - The options for the uuid function.
|
536
|
-
* @param options.message - The message to be shown when the value is not a valid UUID. Defaults to
|
628
|
+
* @param options.message - The message to be shown when the value is not a valid UUID. Defaults to
|
629
|
+
* 'The value should be a valid UUID'.
|
537
630
|
*
|
538
631
|
* @returns - The schema instance.
|
539
632
|
*/ uuid(options) {
|
@@ -556,7 +649,8 @@ export default class StringSchema extends Schema {
|
|
556
649
|
* ```
|
557
650
|
*
|
558
651
|
* @param options - The options for the email function.
|
559
|
-
* @param options.message - The message to be shown when the value is not a valid email.
|
652
|
+
* @param options.message - The message to be shown when the value is not a valid email.
|
653
|
+
* Defaults to 'The value should be a valid email'.
|
560
654
|
*
|
561
655
|
* @returns - The schema instance.
|
562
656
|
*/ email(options) {
|
@@ -567,12 +661,6 @@ export default class StringSchema extends Schema {
|
|
567
661
|
}
|
568
662
|
static new() {
|
569
663
|
const returnValue = new StringSchema();
|
570
|
-
const adapterInstance = getDefaultAdapter();
|
571
|
-
returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
|
572
|
-
transformed: false,
|
573
|
-
adapter: adapterInstance,
|
574
|
-
schemas: []
|
575
|
-
};
|
576
664
|
return returnValue;
|
577
665
|
}
|
578
666
|
}
|