@palmares/schemas 0.1.20 → 0.1.22

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.
Files changed (111) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/src/adapter/index.js +2 -0
  3. package/dist/cjs/src/conf.js +3 -4
  4. package/dist/cjs/src/model.js +34 -38
  5. package/dist/cjs/src/schema/array.js +1 -0
  6. package/dist/cjs/src/schema/object.js +2 -8
  7. package/dist/cjs/src/schema/schema.js +1 -0
  8. package/dist/cjs/src/utils.js +12 -14
  9. package/dist/cjs/src/validators/utils.js +1 -0
  10. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
  11. package/dist/cjs/types/adapter/index.d.ts +2 -0
  12. package/dist/cjs/types/adapter/index.d.ts.map +1 -1
  13. package/dist/cjs/types/conf.d.ts +3 -0
  14. package/dist/cjs/types/conf.d.ts.map +1 -1
  15. package/dist/cjs/types/domain.d.ts +3 -0
  16. package/dist/cjs/types/domain.d.ts.map +1 -1
  17. package/dist/cjs/types/model.d.ts +2 -2
  18. package/dist/cjs/types/model.d.ts.map +1 -1
  19. package/dist/cjs/types/schema/array.d.ts +1 -0
  20. package/dist/cjs/types/schema/array.d.ts.map +1 -1
  21. package/dist/cjs/types/schema/object.d.ts.map +1 -1
  22. package/dist/cjs/types/schema/schema.d.ts +1 -0
  23. package/dist/cjs/types/schema/schema.d.ts.map +1 -1
  24. package/dist/cjs/types/utils.d.ts +2 -1
  25. package/dist/cjs/types/utils.d.ts.map +1 -1
  26. package/dist/cjs/types/validators/utils.d.ts +1 -0
  27. package/dist/cjs/types/validators/utils.d.ts.map +1 -1
  28. package/dist/esm/src/adapter/index.js +2 -0
  29. package/dist/esm/src/conf.js +3 -4
  30. package/dist/esm/src/model.js +32 -30
  31. package/dist/esm/src/schema/array.js +1 -0
  32. package/dist/esm/src/schema/object.js +2 -1
  33. package/dist/esm/src/schema/schema.js +1 -0
  34. package/dist/esm/src/utils.js +9 -6
  35. package/dist/esm/src/validators/utils.js +1 -0
  36. package/package.json +11 -5
  37. package/.turbo/turbo-build$colon$watch.log +0 -24
  38. package/.turbo/turbo-build.log +0 -4
  39. package/.turbo/turbo-build:watch.log +0 -26
  40. package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +0 -22
  41. package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +0 -156
  42. package/__tests__/.drizzle/migrations/meta/_journal.json +0 -13
  43. package/__tests__/.drizzle/schema.ts +0 -35
  44. package/__tests__/drizzle.config.ts +0 -11
  45. package/__tests__/eslint.config.js +0 -10
  46. package/__tests__/manage.ts +0 -5
  47. package/__tests__/node_modules/.bin/drizzle-kit +0 -17
  48. package/__tests__/node_modules/.bin/tsc +0 -17
  49. package/__tests__/node_modules/.bin/tsserver +0 -17
  50. package/__tests__/node_modules/.bin/tsx +0 -17
  51. package/__tests__/package.json +0 -34
  52. package/__tests__/sqlite.db +0 -0
  53. package/__tests__/src/core/array.test.ts +0 -131
  54. package/__tests__/src/core/boolean.test.ts +0 -66
  55. package/__tests__/src/core/datetime.test.ts +0 -102
  56. package/__tests__/src/core/index.ts +0 -35
  57. package/__tests__/src/core/model.test.ts +0 -260
  58. package/__tests__/src/core/models.ts +0 -50
  59. package/__tests__/src/core/numbers.test.ts +0 -177
  60. package/__tests__/src/core/object.test.ts +0 -218
  61. package/__tests__/src/core/string.test.ts +0 -222
  62. package/__tests__/src/core/test.test.ts +0 -59
  63. package/__tests__/src/core/types.test.ts +0 -97
  64. package/__tests__/src/core/union.test.ts +0 -99
  65. package/__tests__/src/settings.ts +0 -69
  66. package/__tests__/tsconfig.json +0 -11
  67. package/src/adapter/fields/array.ts +0 -31
  68. package/src/adapter/fields/boolean.ts +0 -43
  69. package/src/adapter/fields/datetime.ts +0 -43
  70. package/src/adapter/fields/index.ts +0 -72
  71. package/src/adapter/fields/number.ts +0 -43
  72. package/src/adapter/fields/object.ts +0 -52
  73. package/src/adapter/fields/string.ts +0 -43
  74. package/src/adapter/fields/union.ts +0 -43
  75. package/src/adapter/index.ts +0 -35
  76. package/src/adapter/types.ts +0 -276
  77. package/src/compile.ts +0 -14
  78. package/src/conf.ts +0 -27
  79. package/src/constants.ts +0 -7
  80. package/src/domain.ts +0 -15
  81. package/src/exceptions.ts +0 -17
  82. package/src/index.ts +0 -318
  83. package/src/middleware.ts +0 -52
  84. package/src/model.ts +0 -507
  85. package/src/parsers/convert-from-number.ts +0 -13
  86. package/src/parsers/convert-from-string.ts +0 -19
  87. package/src/parsers/index.ts +0 -2
  88. package/src/schema/array.ts +0 -824
  89. package/src/schema/boolean.ts +0 -792
  90. package/src/schema/datetime.ts +0 -704
  91. package/src/schema/index.ts +0 -5
  92. package/src/schema/number.ts +0 -929
  93. package/src/schema/object.ts +0 -798
  94. package/src/schema/schema.ts +0 -1178
  95. package/src/schema/string.ts +0 -941
  96. package/src/schema/types.ts +0 -154
  97. package/src/schema/union.ts +0 -724
  98. package/src/types.ts +0 -66
  99. package/src/utils.ts +0 -383
  100. package/src/validators/array.ts +0 -183
  101. package/src/validators/boolean.ts +0 -52
  102. package/src/validators/datetime.ts +0 -121
  103. package/src/validators/number.ts +0 -178
  104. package/src/validators/object.ts +0 -56
  105. package/src/validators/schema.ts +0 -142
  106. package/src/validators/string.ts +0 -278
  107. package/src/validators/types.ts +0 -1
  108. package/src/validators/union.ts +0 -52
  109. package/src/validators/utils.ts +0 -225
  110. package/tsconfig.json +0 -9
  111. package/tsconfig.types.json +0 -10
@@ -1,704 +0,0 @@
1
- import { Schema } from './schema';
2
- import { defaultTransform, defaultTransformToAdapter } from '../utils';
3
- import { above, allowStringParser, below, datetimeValidation } from '../validators/datetime';
4
- import { nullable, optional } from '../validators/schema';
5
-
6
- import type { DefinitionsOfSchemaType } from './types';
7
-
8
- export class DatetimeSchema<
9
- TType extends {
10
- input: any;
11
- validate: any;
12
- internal: any;
13
- output: any;
14
- representation: any;
15
- } = {
16
- input: Date;
17
- output: string;
18
- internal: Date;
19
- representation: string;
20
- validate: Date;
21
- },
22
- TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType
23
- > extends Schema<TType, TDefinitions> {
24
- protected fieldType = 'datetime';
25
-
26
- protected __allowString!: boolean;
27
-
28
- protected __above!: {
29
- value: Date;
30
- inclusive: boolean;
31
- message: string;
32
- };
33
- protected __below!: {
34
- value: Date;
35
- inclusive: boolean;
36
- message: string;
37
- };
38
-
39
- protected __type: {
40
- message: string;
41
- check: (value: TType['input']) => boolean;
42
- } = {
43
- message: 'Invalid type',
44
- check: (value: any) => {
45
- if (typeof value === 'string') {
46
- const valueAsDate = new Date(value);
47
- if (isNaN(valueAsDate.getTime())) return false;
48
- return true;
49
- }
50
- return value instanceof Date;
51
- }
52
- };
53
-
54
- protected async __transformToAdapter(options: Parameters<Schema['__transformToAdapter']>[0]): Promise<any> {
55
- return defaultTransformToAdapter(
56
- async (adapter) => {
57
- return defaultTransform(
58
- 'datetime',
59
- this,
60
- adapter,
61
- adapter.datetime,
62
- () => ({
63
- allowString: this.__allowString,
64
- below: this.__below,
65
- above: this.__above,
66
- nullable: this.__nullable,
67
- optional: this.__optional,
68
- type: this.__type,
69
- parsers: {
70
- nullable: this.__nullable.allow,
71
- optional: this.__optional.allow
72
- }
73
- }),
74
- {
75
- optional,
76
- nullable,
77
- allowString: allowStringParser,
78
- above,
79
- below
80
- },
81
- {
82
- validatorsIfFallbackOrNotSupported: datetimeValidation(),
83
- shouldAddStringVersion: options.shouldAddStringVersion,
84
- // eslint-disable-next-line ts/require-await
85
- fallbackIfNotSupported: async () => []
86
- }
87
- );
88
- },
89
- this,
90
- this.__transformedSchemas,
91
- options,
92
- 'datetime'
93
- );
94
- }
95
-
96
- /**
97
- * This let's you refine the schema with custom validations. This is useful when you want to validate something
98
- * that is not supported by default by the schema adapter.
99
- *
100
- * @example
101
- * ```typescript
102
- * import * as p from '@palmares/schemas';
103
- *
104
- * const numberSchema = p.number().refine((value) => {
105
- * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
106
- * });
107
- *
108
- * const { errors, parsed } = await numberSchema.parse(-1);
109
- *
110
- * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
111
- * console.log(errors);
112
- * ```
113
- *
114
- * @param refinementCallback - The callback that will be called to validate the value.
115
- * @param options - Options for the refinement.
116
- * @param options.isAsync - Whether the callback is async or not. Defaults to true.
117
- *
118
- * @returns The schema.
119
- */
120
- refine(
121
- refinementCallback: (
122
- value: TType['input']
123
- ) =>
124
- | Promise<void | undefined | { code: string; message: string }>
125
- | void
126
- | undefined
127
- | { code: string; message: string }
128
- ) {
129
- return super.refine(refinementCallback) as unknown as DatetimeSchema<
130
- {
131
- input: TType['input'];
132
- validate: TType['validate'];
133
- internal: TType['internal'];
134
- output: TType['output'];
135
- representation: TType['representation'];
136
- },
137
- TDefinitions
138
- >;
139
- }
140
-
141
- /**
142
- * Allows the value to be either undefined or null.
143
- *
144
- * @example
145
- * ```typescript
146
- * import * as p from '@palmares/schemas';
147
- *
148
- * const numberSchema = p.number().optional();
149
- *
150
- * const { errors, parsed } = await numberSchema.parse(undefined);
151
- *
152
- * console.log(parsed); // undefined
153
- *
154
- * const { errors, parsed } = await numberSchema.parse(null);
155
- *
156
- * console.log(parsed); // null
157
- *
158
- * const { errors, parsed } = await numberSchema.parse(1);
159
- *
160
- * console.log(parsed); // 1
161
- * ```
162
- *
163
- * @returns - The schema we are working with.
164
- */
165
- optional(options?: { message: string; allow: false }) {
166
- return super.optional(options) as unknown as DatetimeSchema<
167
- {
168
- input: TType['input'] | undefined | null;
169
- validate: TType['validate'] | undefined | null;
170
- internal: TType['internal'] | undefined | null;
171
- output: TType['output'] | undefined | null;
172
- representation: TType['representation'] | undefined | null;
173
- },
174
- TDefinitions
175
- >;
176
- }
177
-
178
- /**
179
- * Just adds a message when the value is undefined. It's just a syntax sugar for
180
- *
181
- * ```typescript
182
- * p.datetime().optional({ message: 'This value should be defined', allow: false })
183
- * ```
184
- *
185
- * @param options - The options of nonOptional function
186
- * @param options.message - A custom message if the value is undefined.
187
- *
188
- * @returns - The schema.
189
- */
190
- nonOptional(options?: { message: string }) {
191
- return super.optional({
192
- message: options?.message,
193
- allow: false
194
- }) as unknown as DatetimeSchema<
195
- {
196
- input: TType['input'];
197
- validate: TType['validate'];
198
- internal: TType['internal'];
199
- output: TType['output'];
200
- representation: TType['representation'];
201
- },
202
- TDefinitions
203
- >;
204
- }
205
-
206
- /**
207
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
208
- * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
209
- *
210
- * @example
211
- * ```typescript
212
- * import * as p from '@palmares/schemas';
213
- *
214
- * const numberSchema = p.number().nullable();
215
- *
216
- * const { errors, parsed } = await numberSchema.parse(null);
217
- *
218
- * console.log(parsed); // null
219
- *
220
- * const { errors, parsed } = await numberSchema.parse(undefined);
221
- *
222
- * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
223
- * ```
224
- *
225
- * @param options - The options for the nullable function.
226
- * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
227
- * @param options.allow - Whether the value can be null or not. Defaults to true.
228
- *
229
- * @returns The schema.
230
- */
231
- nullable(options?: { message: string; allow: false }) {
232
- return super.nullable(options) as unknown as DatetimeSchema<
233
- {
234
- input: TType['input'] | null;
235
- validate: TType['validate'] | null;
236
- internal: TType['internal'] | null;
237
- output: TType['output'] | null;
238
- representation: TType['representation'] | null;
239
- },
240
- TDefinitions
241
- >;
242
- }
243
-
244
- /**
245
- * Just adds a message when the value is null. It's just a syntax sugar for
246
- *
247
- * ```typescript
248
- * p.datetime().nullable({ message: 'This value cannot be null', allow: false })
249
- * ```
250
- *
251
- * @param options - The options of nonNullable function
252
- * @param options.message - A custom message if the value is null.
253
- *
254
- * @returns - The schema.
255
- */
256
- nonNullable(options?: { message: string }) {
257
- return super.nullable({
258
- message: options?.message || '',
259
- allow: false
260
- }) as unknown as DatetimeSchema<
261
- {
262
- input: TType['input'];
263
- validate: TType['validate'];
264
- internal: TType['internal'];
265
- output: TType['output'];
266
- representation: TType['representation'];
267
- },
268
- TDefinitions
269
- >;
270
- }
271
-
272
- /**
273
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep
274
- * that way otherwise it will set the value to undefined after it's validated.
275
- * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
276
- * function. This will remove the value from the representation of the schema.
277
- *
278
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
279
- * function.But if you want to remove the value from the internal representation, you can pass the argument
280
- * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
281
- * the argument `toRepresentation` as true as well.
282
- *
283
- * @example
284
- * ```typescript
285
- * import * as p from '@palmares/schemas';
286
- *
287
- * const userSchema = p.object({
288
- * id: p.number().optional(),
289
- * name: p.string(),
290
- * password: p.string().omit()
291
- * });
292
- *
293
- * const user = await userSchema.data({
294
- * id: 1,
295
- * name: 'John Doe',
296
- * password: '123456'
297
- * });
298
- *
299
- * console.log(user); // { id: 1, name: 'John Doe' }
300
- * ```
301
- *
302
- *
303
- * @param args - By default, the value will be removed just from the representation, in other words, when you call
304
- * the {@link data} function.
305
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as
306
- * true. Then if you still want to remove the value from the representation, you will need to pass the argument
307
- * `toRepresentation` as true as well.
308
- *
309
- * @returns The schema.
310
- */
311
- omit<
312
- TToInternal extends boolean,
313
- TToRepresentation extends boolean = boolean extends TToInternal ? true : false
314
- >(args?: { toInternal?: TToInternal; toRepresentation?: TToRepresentation }) {
315
- return super.omit(args) as unknown as DatetimeSchema<
316
- {
317
- input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
318
- validate: TToInternal extends true ? TType['validate'] | undefined : TType['validate'];
319
- internal: TToInternal extends true ? undefined : TType['internal'];
320
- output: TToRepresentation extends true ? TType['output'] | undefined : TType['output'];
321
- representation: TToRepresentation extends true ? undefined : TType['representation'];
322
- },
323
- TDefinitions
324
- >;
325
- }
326
-
327
- /**
328
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
329
- * source like a database. You should always return the schema after you save the value, that way we will always have
330
- * the correct type of the schema after the save operation.
331
- *
332
- * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
333
- *
334
- * @example
335
- * ```typescript
336
- * import * as p from '@palmares/schemas';
337
- *
338
- * import { User } from './models';
339
- *
340
- * const userSchema = p.object({
341
- * id: p.number().optional(),
342
- * name: p.string(),
343
- * email: p.string().email(),
344
- * }).onSave(async (value) => {
345
- * // Create or update the user on the database using palmares models or any other library of your choice.
346
- * if (value.id)
347
- * await User.default.set(value, { search: { id: value.id } });
348
- * else
349
- * await User.default.set(value);
350
- *
351
- * return value;
352
- * });
353
- *
354
- *
355
- * // Then, on your controller, do something like this:
356
- * const { isValid, save, errors } = await userSchema.validate(req.body);
357
- * if (isValid) {
358
- * const savedValue = await save();
359
- * return Response.json(savedValue, { status: 201 });
360
- * }
361
- *
362
- * return Response.json({ errors }, { status: 400 });
363
- * ```
364
- *
365
- * @param callback - The callback that will be called to save the value on an external source.
366
- *
367
- * @returns The schema.
368
- */
369
- onSave(
370
- callback: <TContext = any>(
371
- value: TType['internal'],
372
- context: TContext
373
- ) => Promise<TType['output']> | TType['output']
374
- ) {
375
- return super.onSave(callback) as unknown as DatetimeSchema<
376
- {
377
- input: TType['input'];
378
- validate: TType['validate'];
379
- internal: TType['internal'];
380
- output: TType['output'];
381
- representation: TType['representation'];
382
- },
383
- TDefinitions & {
384
- hasSave: true;
385
- }
386
- >;
387
- }
388
-
389
- /**
390
- * This function is used to add a default value to the schema. If the value is either undefined or null, the default
391
- * value will be used.
392
- *
393
- * @example
394
- * ```typescript
395
- * import * as p from '@palmares/schemas';
396
- *
397
- * const numberSchema = p.number().default(0);
398
- *
399
- * const { errors, parsed } = await numberSchema.parse(undefined);
400
- *
401
- * console.log(parsed); // 0
402
- * ```
403
- */
404
- default<TDefaultValue extends TType['input'] | (() => Promise<TType['input']>)>(
405
- defaultValueOrFunction: TDefaultValue
406
- ) {
407
- return super.default(defaultValueOrFunction) as unknown as DatetimeSchema<
408
- {
409
- input: TType['input'] | undefined | null;
410
- validate: TType['validate'];
411
- internal: TType['internal'];
412
- output: TType['output'] | undefined | null;
413
- representation: TType['representation'];
414
- },
415
- TDefinitions
416
- >;
417
- }
418
-
419
- /**
420
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
421
- * this function to let you customize the custom schema your own way. Our API does not support passthrough?
422
- * No problem, you can use this function to customize the zod schema.
423
- *
424
- * @example
425
- * ```typescript
426
- * import * as p from '@palmares/schemas';
427
- *
428
- * const numberSchema = p.number().extends((schema) => {
429
- * return schema.nonnegative();
430
- * });
431
- *
432
- * const { errors, parsed } = await numberSchema.parse(-1);
433
- *
434
- * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
435
- * console.log(errors);
436
- * ```
437
- *
438
- * @param callback - The callback that will be called to customize the schema.
439
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
440
- * compile the underlying schema to a string so you can save it for future runs.
441
- *
442
- * @returns The schema.
443
- */
444
- extends(
445
- callback: (
446
- schema: Awaited<ReturnType<NonNullable<TDefinitions['schemaAdapter']['datetime']>['translate']>>
447
- ) => Awaited<ReturnType<NonNullable<TDefinitions['schemaAdapter']['field']>['translate']>> | any,
448
- toStringCallback?: (schemaAsString: string) => string
449
- ) {
450
- return super.extends(callback, toStringCallback);
451
- }
452
-
453
- /**
454
- * This function is used to transform the value to the representation of the schema. When using the {@link data}
455
- * function. With this function you have full control to add data cleaning for example, transforming the data and
456
- * whatever. Another use case is when you want to return deeply nested recursive data.
457
- * The schema maps to itself.
458
- *
459
- * @example
460
- * ```typescript
461
- * import * as p from '@palmares/schemas';
462
- *
463
- * const recursiveSchema = p.object({
464
- * id: p.number().optional(),
465
- * name: p.string(),
466
- * }).toRepresentation(async (value) => {
467
- * return {
468
- * id: value.id,
469
- * name: value.name,
470
- * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
471
- * }
472
- * });
473
- *
474
- * const data = await recursiveSchema.data({
475
- * id: 1,
476
- * name: 'John Doe',
477
- * });
478
- * ```
479
- *
480
- * @example
481
- * ```
482
- * import * as p from '@palmares/schemas';
483
- *
484
- * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
485
- * switch (value) {
486
- * case 'red': return { r: 255, g: 0, b: 0 };
487
- * case 'green': return { r: 0, g: 255, b: 0 };
488
- * case 'blue': return { r: 0, g: 0, b: 255 };
489
- * default: return { r: 0, g: 0, b: 0 };
490
- * }
491
- * });
492
- * ```
493
- * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
494
- *
495
- * @returns The schema with a new return type
496
- */
497
- toRepresentation<TRepresentation>(
498
- toRepresentationCallback: (value: TType['representation']) => Promise<TRepresentation>
499
- ) {
500
- return super.toRepresentation(toRepresentationCallback) as unknown as DatetimeSchema<
501
- {
502
- input: TType['input'];
503
- validate: TType['validate'];
504
- internal: TType['internal'];
505
- output: TType['output'];
506
- representation: TRepresentation;
507
- },
508
- TDefinitions
509
- >;
510
- }
511
-
512
- /**
513
- * This function is used to transform the value to the internal representation of the schema. This is useful when you
514
- * want to transform the value to a type that the schema adapter can understand. For example, you might want to
515
- * transform a string to a date. This is the function you use.
516
- *
517
- * @example
518
- * ```typescript
519
- * import * as p from '@palmares/schemas';
520
- *
521
- * const dateSchema = p.string().toInternal((value) => {
522
- * return new Date(value);
523
- * });
524
- *
525
- * const date = await dateSchema.parse('2021-01-01');
526
- *
527
- * console.log(date); // Date object
528
- *
529
- * const rgbToColorSchema = p.object({
530
- * r: p.number().min(0).max(255),
531
- * g: p.number().min(0).max(255),
532
- * b: p.number().min(0).max(255),
533
- * }).toInternal(async (value) => {
534
- * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
535
- * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
536
- * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
537
- * return `rgb(${value.r}, ${value.g}, ${value.b})`;
538
- * });
539
- * ```
540
- *
541
- * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
542
- *
543
- * @returns The schema with a new return type.
544
- */
545
- toInternal<TInternal>(toInternalCallback: (value: TType['validate']) => Promise<TInternal>) {
546
- return super.toInternal(toInternalCallback) as unknown as DatetimeSchema<
547
- {
548
- input: TType['input'];
549
- validate: TType['validate'];
550
- internal: TInternal;
551
- output: TType['output'];
552
- representation: TType['representation'];
553
- },
554
- TDefinitions
555
- >;
556
- }
557
-
558
- /**
559
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
560
- * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
561
- * that the schema adapter can understand.
562
- *
563
- * @example
564
- * ```
565
- * import * as p from '@palmares/schemas';
566
- * import * as z from 'zod';
567
- *
568
- * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
569
- * return new Map(value); // Before validating we transform the value to a map.
570
- * });
571
- *
572
- * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
573
- * ```
574
- *
575
- * @param toValidateCallback - The callback that will be called to validate the value.
576
- *
577
- * @returns The schema with a new return type.
578
- */
579
- toValidate<TValidate>(toValidateCallback: (value: TType['input']) => Promise<TValidate> | TValidate) {
580
- return super.toValidate(toValidateCallback) as unknown as DatetimeSchema<
581
- {
582
- input: TType['input'];
583
- validate: TValidate;
584
- internal: TType['internal'];
585
- output: TType['output'];
586
- representation: TType['representation'];
587
- },
588
- TDefinitions
589
- >;
590
- }
591
-
592
- /**
593
- * This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
594
- * result to be a string as well.
595
- *
596
- * @example
597
- * ```typescript
598
- * const schema = datetime().allowString();
599
- *
600
- * schema.parse('2021-01-01T00:00:00.000Z') // works
601
- * ```
602
- *
603
- * @returns - The schema instance
604
- */
605
- allowString() {
606
- this.__allowString = true;
607
-
608
- return this as any as DatetimeSchema<
609
- {
610
- input: string | TType['input'];
611
- output: string | TType['output'];
612
- internal: string | TType['output'];
613
- representation: string | TType['output'];
614
- validate: string | TType['output'];
615
- },
616
- TDefinitions
617
- >;
618
- }
619
-
620
- /**
621
- * This will validate if the value is above the specified date.
622
- *
623
- * @example
624
- * ```ts
625
- * const today = new Date();
626
- * const yesterday = new Date(today);
627
- * yesterday.setDate(today.getDate() - 1);
628
- *
629
- * datetime().above(today).parse(today) // throws error
630
- * datetime().above(today, { inclusive: true }).parse(today) // works
631
- * datetime().above(yesterday).parse(today) // works
632
- * ```
633
- *
634
- * @param value - The value that we are comparing against.
635
- * @param options - The options that we are passing to the validator.
636
- * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to
637
- * the specified date.
638
- * @param options.message - The message that we are returning if the value is not above the specified date.
639
- *
640
- * @returns - The schema instance
641
- */
642
- above(value: Date, options?: { inclusive?: boolean; message: string }) {
643
- const inclusive = typeof options?.inclusive === 'boolean' ? options.inclusive : false;
644
- const message = options?.message || 'Value is not above the specified date';
645
-
646
- this.__above = {
647
- value,
648
- inclusive,
649
- message
650
- };
651
- return this;
652
- }
653
-
654
- /**
655
- * This will validate if the value is below the specified date.
656
- *
657
- * @example
658
- * ```ts
659
- * const today = new Date();
660
- * const yesterday = new Date(today);
661
- * yesterday.setDate(today.getDate() - 1);
662
- *
663
- * datetime().below(today).parse(today) // throws error
664
- * datetime().below(today, { inclusive: true }).parse(today) // works
665
- * datetime().below(today).parse(yesterday) // works
666
- * ```
667
- *
668
- * @param value - The value that we are comparing against.
669
- * @param options - The options that we are passing to the validator.
670
- * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to the
671
- * specified date.
672
- * @param options.message - The message that we are returning if the value is not above the specified date.
673
- *
674
- * @returns - The schema instance
675
- */
676
- below(value: Date, options?: { inclusive?: boolean; message: string }) {
677
- const inclusive = typeof options?.inclusive === 'boolean' ? options.inclusive : false;
678
- const message = options?.message || 'Value is not below the specified date';
679
-
680
- this.__below = {
681
- value,
682
- inclusive,
683
- message
684
- };
685
- return this;
686
- }
687
-
688
- static new<TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType>() {
689
- const returnValue = new DatetimeSchema<
690
- {
691
- input: Date;
692
- validate: Date;
693
- internal: Date;
694
- output: Date;
695
- representation: Date;
696
- },
697
- TDefinitions
698
- >();
699
-
700
- return returnValue;
701
- }
702
- }
703
-
704
- export const datetime = <TDefinitions extends DefinitionsOfSchemaType>() => DatetimeSchema.new<TDefinitions>();
@@ -1,5 +0,0 @@
1
- import { NumberSchema } from './number';
2
-
3
- const number = NumberSchema.new; // teste
4
-
5
- export { number };