@palmares/schemas 0.0.1

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 (219) hide show
  1. package/.turbo/turbo-build$colon$watch.log +424 -0
  2. package/.turbo/turbo-build.log +13 -0
  3. package/.turbo/turbo-build:watch.log +26 -0
  4. package/CHANGELOG.md +11 -0
  5. package/LICENSE +21 -0
  6. package/dist/cjs/src/adapter/fields/array.js +157 -0
  7. package/dist/cjs/src/adapter/fields/boolean.js +167 -0
  8. package/dist/cjs/src/adapter/fields/datetime.js +167 -0
  9. package/dist/cjs/src/adapter/fields/index.js +311 -0
  10. package/dist/cjs/src/adapter/fields/number.js +167 -0
  11. package/dist/cjs/src/adapter/fields/object.js +167 -0
  12. package/dist/cjs/src/adapter/fields/string.js +167 -0
  13. package/dist/cjs/src/adapter/fields/union.js +167 -0
  14. package/dist/cjs/src/adapter/index.js +198 -0
  15. package/dist/cjs/src/adapter/types.js +4 -0
  16. package/dist/cjs/src/compile.js +262 -0
  17. package/dist/cjs/src/conf.js +27 -0
  18. package/dist/cjs/src/constants.js +42 -0
  19. package/dist/cjs/src/domain.js +12 -0
  20. package/dist/cjs/src/exceptions.js +168 -0
  21. package/dist/cjs/src/index.js +365 -0
  22. package/dist/cjs/src/model.js +628 -0
  23. package/dist/cjs/src/parsers/convert-from-number.js +20 -0
  24. package/dist/cjs/src/parsers/convert-from-string.js +24 -0
  25. package/dist/cjs/src/parsers/index.js +25 -0
  26. package/dist/cjs/src/schema/array.js +890 -0
  27. package/dist/cjs/src/schema/boolean.js +826 -0
  28. package/dist/cjs/src/schema/datetime.js +778 -0
  29. package/dist/cjs/src/schema/index.js +17 -0
  30. package/dist/cjs/src/schema/number.js +960 -0
  31. package/dist/cjs/src/schema/object.js +999 -0
  32. package/dist/cjs/src/schema/schema.js +1788 -0
  33. package/dist/cjs/src/schema/string.js +948 -0
  34. package/dist/cjs/src/schema/types.js +4 -0
  35. package/dist/cjs/src/schema/union.js +952 -0
  36. package/dist/cjs/src/types.js +4 -0
  37. package/dist/cjs/src/utils.js +627 -0
  38. package/dist/cjs/src/validators/array.js +457 -0
  39. package/dist/cjs/src/validators/boolean.js +199 -0
  40. package/dist/cjs/src/validators/datetime.js +287 -0
  41. package/dist/cjs/src/validators/number.js +403 -0
  42. package/dist/cjs/src/validators/object.js +290 -0
  43. package/dist/cjs/src/validators/schema.js +318 -0
  44. package/dist/cjs/src/validators/string.js +439 -0
  45. package/dist/cjs/src/validators/types.js +4 -0
  46. package/dist/cjs/src/validators/union.js +232 -0
  47. package/dist/cjs/src/validators/utils.js +426 -0
  48. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -0
  49. package/dist/cjs/types/adapter/fields/array.d.ts +20 -0
  50. package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -0
  51. package/dist/cjs/types/adapter/fields/boolean.d.ts +25 -0
  52. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -0
  53. package/dist/cjs/types/adapter/fields/datetime.d.ts +25 -0
  54. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -0
  55. package/dist/cjs/types/adapter/fields/index.d.ts +31 -0
  56. package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -0
  57. package/dist/cjs/types/adapter/fields/number.d.ts +25 -0
  58. package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -0
  59. package/dist/cjs/types/adapter/fields/object.d.ts +25 -0
  60. package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -0
  61. package/dist/cjs/types/adapter/fields/string.d.ts +25 -0
  62. package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -0
  63. package/dist/cjs/types/adapter/fields/union.d.ts +25 -0
  64. package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -0
  65. package/dist/cjs/types/adapter/index.d.ts +25 -0
  66. package/dist/cjs/types/adapter/index.d.ts.map +1 -0
  67. package/dist/cjs/types/adapter/types.d.ts +144 -0
  68. package/dist/cjs/types/adapter/types.d.ts.map +1 -0
  69. package/dist/cjs/types/compile.d.ts +3 -0
  70. package/dist/cjs/types/compile.d.ts.map +1 -0
  71. package/dist/cjs/types/conf.d.ts +16 -0
  72. package/dist/cjs/types/conf.d.ts.map +1 -0
  73. package/dist/cjs/types/constants.d.ts +6 -0
  74. package/dist/cjs/types/constants.d.ts.map +1 -0
  75. package/dist/cjs/types/domain.d.ts +21 -0
  76. package/dist/cjs/types/domain.d.ts.map +1 -0
  77. package/dist/cjs/types/exceptions.d.ts +13 -0
  78. package/dist/cjs/types/exceptions.d.ts.map +1 -0
  79. package/dist/cjs/types/index.d.ts +240 -0
  80. package/dist/cjs/types/index.d.ts.map +1 -0
  81. package/dist/cjs/types/model.d.ts +136 -0
  82. package/dist/cjs/types/model.d.ts.map +1 -0
  83. package/dist/cjs/types/parsers/convert-from-number.d.ts +15 -0
  84. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +1 -0
  85. package/dist/cjs/types/parsers/convert-from-string.d.ts +9 -0
  86. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +1 -0
  87. package/dist/cjs/types/parsers/index.d.ts +3 -0
  88. package/dist/cjs/types/parsers/index.d.ts.map +1 -0
  89. package/dist/cjs/types/schema/array.d.ts +429 -0
  90. package/dist/cjs/types/schema/array.d.ts.map +1 -0
  91. package/dist/cjs/types/schema/boolean.d.ts +501 -0
  92. package/dist/cjs/types/schema/boolean.d.ts.map +1 -0
  93. package/dist/cjs/types/schema/datetime.d.ts +474 -0
  94. package/dist/cjs/types/schema/datetime.d.ts.map +1 -0
  95. package/dist/cjs/types/schema/index.d.ts +4 -0
  96. package/dist/cjs/types/schema/index.d.ts.map +1 -0
  97. package/dist/cjs/types/schema/number.d.ts +667 -0
  98. package/dist/cjs/types/schema/number.d.ts.map +1 -0
  99. package/dist/cjs/types/schema/object.d.ts +450 -0
  100. package/dist/cjs/types/schema/object.d.ts.map +1 -0
  101. package/dist/cjs/types/schema/schema.d.ts +646 -0
  102. package/dist/cjs/types/schema/schema.d.ts.map +1 -0
  103. package/dist/cjs/types/schema/string.d.ts +606 -0
  104. package/dist/cjs/types/schema/string.d.ts.map +1 -0
  105. package/dist/cjs/types/schema/types.d.ts +70 -0
  106. package/dist/cjs/types/schema/types.d.ts.map +1 -0
  107. package/dist/cjs/types/schema/union.d.ts +388 -0
  108. package/dist/cjs/types/schema/union.d.ts.map +1 -0
  109. package/dist/cjs/types/types.d.ts +11 -0
  110. package/dist/cjs/types/types.d.ts.map +1 -0
  111. package/dist/cjs/types/utils.d.ts +79 -0
  112. package/dist/cjs/types/utils.d.ts.map +1 -0
  113. package/dist/cjs/types/validators/array.d.ts +8 -0
  114. package/dist/cjs/types/validators/array.d.ts.map +1 -0
  115. package/dist/cjs/types/validators/boolean.d.ts +4 -0
  116. package/dist/cjs/types/validators/boolean.d.ts.map +1 -0
  117. package/dist/cjs/types/validators/datetime.d.ts +7 -0
  118. package/dist/cjs/types/validators/datetime.d.ts.map +1 -0
  119. package/dist/cjs/types/validators/number.d.ts +10 -0
  120. package/dist/cjs/types/validators/number.d.ts.map +1 -0
  121. package/dist/cjs/types/validators/object.d.ts +6 -0
  122. package/dist/cjs/types/validators/object.d.ts.map +1 -0
  123. package/dist/cjs/types/validators/schema.d.ts +10 -0
  124. package/dist/cjs/types/validators/schema.d.ts.map +1 -0
  125. package/dist/cjs/types/validators/string.d.ts +12 -0
  126. package/dist/cjs/types/validators/string.d.ts.map +1 -0
  127. package/dist/cjs/types/validators/types.d.ts +2 -0
  128. package/dist/cjs/types/validators/types.d.ts.map +1 -0
  129. package/dist/cjs/types/validators/union.d.ts +4 -0
  130. package/dist/cjs/types/validators/union.d.ts.map +1 -0
  131. package/dist/cjs/types/validators/utils.d.ts +83 -0
  132. package/dist/cjs/types/validators/utils.d.ts.map +1 -0
  133. package/dist/esm/src/adapter/fields/array.js +13 -0
  134. package/dist/esm/src/adapter/fields/boolean.js +20 -0
  135. package/dist/esm/src/adapter/fields/datetime.js +20 -0
  136. package/dist/esm/src/adapter/fields/index.js +37 -0
  137. package/dist/esm/src/adapter/fields/number.js +20 -0
  138. package/dist/esm/src/adapter/fields/object.js +20 -0
  139. package/dist/esm/src/adapter/fields/string.js +20 -0
  140. package/dist/esm/src/adapter/fields/union.js +20 -0
  141. package/dist/esm/src/adapter/index.js +18 -0
  142. package/dist/esm/src/adapter/types.js +1 -0
  143. package/dist/esm/src/compile.js +10 -0
  144. package/dist/esm/src/conf.js +19 -0
  145. package/dist/esm/src/constants.js +5 -0
  146. package/dist/esm/src/domain.js +2 -0
  147. package/dist/esm/src/exceptions.js +15 -0
  148. package/dist/esm/src/index.js +160 -0
  149. package/dist/esm/src/model.js +255 -0
  150. package/dist/esm/src/parsers/convert-from-number.js +8 -0
  151. package/dist/esm/src/parsers/convert-from-string.js +14 -0
  152. package/dist/esm/src/parsers/index.js +2 -0
  153. package/dist/esm/src/schema/array.js +403 -0
  154. package/dist/esm/src/schema/boolean.js +465 -0
  155. package/dist/esm/src/schema/datetime.js +423 -0
  156. package/dist/esm/src/schema/index.js +3 -0
  157. package/dist/esm/src/schema/number.js +592 -0
  158. package/dist/esm/src/schema/object.js +464 -0
  159. package/dist/esm/src/schema/schema.js +728 -0
  160. package/dist/esm/src/schema/string.js +579 -0
  161. package/dist/esm/src/schema/types.js +1 -0
  162. package/dist/esm/src/schema/union.js +388 -0
  163. package/dist/esm/src/types.js +1 -0
  164. package/dist/esm/src/utils.js +175 -0
  165. package/dist/esm/src/validators/array.js +135 -0
  166. package/dist/esm/src/validators/boolean.js +35 -0
  167. package/dist/esm/src/validators/datetime.js +85 -0
  168. package/dist/esm/src/validators/number.js +162 -0
  169. package/dist/esm/src/validators/object.js +38 -0
  170. package/dist/esm/src/validators/schema.js +114 -0
  171. package/dist/esm/src/validators/string.js +174 -0
  172. package/dist/esm/src/validators/types.js +1 -0
  173. package/dist/esm/src/validators/union.js +38 -0
  174. package/dist/esm/src/validators/utils.js +120 -0
  175. package/package.json +48 -0
  176. package/src/adapter/fields/array.ts +31 -0
  177. package/src/adapter/fields/boolean.ts +48 -0
  178. package/src/adapter/fields/datetime.ts +49 -0
  179. package/src/adapter/fields/index.ts +72 -0
  180. package/src/adapter/fields/number.ts +49 -0
  181. package/src/adapter/fields/object.ts +49 -0
  182. package/src/adapter/fields/string.ts +49 -0
  183. package/src/adapter/fields/union.ts +49 -0
  184. package/src/adapter/index.ts +34 -0
  185. package/src/adapter/types.ts +261 -0
  186. package/src/compile.ts +14 -0
  187. package/src/conf.ts +27 -0
  188. package/src/constants.ts +9 -0
  189. package/src/domain.ts +3 -0
  190. package/src/exceptions.ts +17 -0
  191. package/src/index.ts +338 -0
  192. package/src/model.ts +501 -0
  193. package/src/parsers/convert-from-number.ts +13 -0
  194. package/src/parsers/convert-from-string.ts +19 -0
  195. package/src/parsers/index.ts +2 -0
  196. package/src/schema/array.ts +633 -0
  197. package/src/schema/boolean.ts +700 -0
  198. package/src/schema/datetime.ts +613 -0
  199. package/src/schema/index.ts +5 -0
  200. package/src/schema/number.ts +885 -0
  201. package/src/schema/object.ts +699 -0
  202. package/src/schema/schema.ts +1093 -0
  203. package/src/schema/string.ts +807 -0
  204. package/src/schema/types.ts +126 -0
  205. package/src/schema/union.ts +596 -0
  206. package/src/types.ts +13 -0
  207. package/src/utils.ts +322 -0
  208. package/src/validators/array.ts +164 -0
  209. package/src/validators/boolean.ts +46 -0
  210. package/src/validators/datetime.ts +113 -0
  211. package/src/validators/number.ts +188 -0
  212. package/src/validators/object.ts +55 -0
  213. package/src/validators/schema.ts +134 -0
  214. package/src/validators/string.ts +215 -0
  215. package/src/validators/types.ts +1 -0
  216. package/src/validators/union.ts +52 -0
  217. package/src/validators/utils.ts +200 -0
  218. package/tsconfig.json +9 -0
  219. package/tsconfig.types.json +10 -0
@@ -0,0 +1,423 @@
1
+ import Schema from './schema';
2
+ import { getDefaultAdapter } from '../conf';
3
+ import { defaultTransform, defaultTransformToAdapter } from '../utils';
4
+ import { above, allowStringParser, below, datetimeValidation } from '../validators/datetime';
5
+ import { nullable, optional } from '../validators/schema';
6
+ export default class DatetimeSchema extends Schema {
7
+ __allowString;
8
+ __above;
9
+ __below;
10
+ async __transformToAdapter(options) {
11
+ return defaultTransformToAdapter(async (adapter)=>{
12
+ return defaultTransform('datetime', this, adapter, adapter.datetime, ()=>({
13
+ allowString: this.__allowString,
14
+ below: this.__below,
15
+ above: this.__above,
16
+ nullable: this.__nullable,
17
+ optional: this.__optional,
18
+ parsers: {
19
+ nullable: this.__nullable.allow,
20
+ optional: this.__optional.allow
21
+ }
22
+ }), {
23
+ optional,
24
+ nullable,
25
+ allowString: allowStringParser,
26
+ above,
27
+ below
28
+ }, {
29
+ validatorsIfFallbackOrNotSupported: datetimeValidation(),
30
+ shouldAddStringVersion: options.shouldAddStringVersion,
31
+ // eslint-disable-next-line ts/require-await
32
+ fallbackIfNotSupported: async ()=>[]
33
+ });
34
+ }, this.__transformedSchemas, options, 'datetime');
35
+ }
36
+ /**
37
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something that is not supported by default by the schema adapter.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * import * as p from '@palmares/schemas';
42
+ *
43
+ * const numberSchema = p.number().refine((value) => {
44
+ * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
45
+ * });
46
+ *
47
+ * const { errors, parsed } = await numberSchema.parse(-1);
48
+ *
49
+ * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
50
+ * ```
51
+ *
52
+ * @param refinementCallback - The callback that will be called to validate the value.
53
+ * @param options - Options for the refinement.
54
+ * @param options.isAsync - Whether the callback is async or not. Defaults to true.
55
+ *
56
+ * @returns The schema.
57
+ */ refine(refinementCallback) {
58
+ return super.refine(refinementCallback);
59
+ }
60
+ /**
61
+ * Allows the value to be either undefined or null.
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * import * as p from '@palmares/schemas';
66
+ *
67
+ * const numberSchema = p.number().optional();
68
+ *
69
+ * const { errors, parsed } = await numberSchema.parse(undefined);
70
+ *
71
+ * console.log(parsed); // undefined
72
+ *
73
+ * const { errors, parsed } = await numberSchema.parse(null);
74
+ *
75
+ * console.log(parsed); // null
76
+ *
77
+ * const { errors, parsed } = await numberSchema.parse(1);
78
+ *
79
+ * console.log(parsed); // 1
80
+ * ```
81
+ *
82
+ * @returns - The schema we are working with.
83
+ */ optional(options) {
84
+ return super.optional(options);
85
+ }
86
+ /**
87
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value is NULL by setting
88
+ * the { message: 'Your custom message', allow: false } on the options.
89
+ *
90
+ * @example
91
+ * ```typescript
92
+ * import * as p from '@palmares/schemas';
93
+ *
94
+ * const numberSchema = p.number().nullable();
95
+ *
96
+ * const { errors, parsed } = await numberSchema.parse(null);
97
+ *
98
+ * console.log(parsed); // null
99
+ *
100
+ * const { errors, parsed } = await numberSchema.parse(undefined);
101
+ *
102
+ * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
103
+ * ```
104
+ *
105
+ * @param options - The options for the nullable function.
106
+ * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
107
+ * @param options.allow - Whether the value can be null or not. Defaults to true.
108
+ *
109
+ * @returns The schema.
110
+ */ nullable(options) {
111
+ return super.nullable(options);
112
+ }
113
+ /**
114
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
115
+ * otherwise it will set the value to undefined after it's validated.
116
+ * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
117
+ * function. This will remove the value from the representation of the schema.
118
+ *
119
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
120
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
121
+ * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
122
+ *
123
+ * @example
124
+ * ```typescript
125
+ * import * as p from '@palmares/schemas';
126
+ *
127
+ * const userSchema = p.object({
128
+ * id: p.number().optional(),
129
+ * name: p.string(),
130
+ * password: p.string().omit()
131
+ * });
132
+ *
133
+ * const user = await userSchema.data({
134
+ * id: 1,
135
+ * name: 'John Doe',
136
+ * password: '123456'
137
+ * });
138
+ *
139
+ * console.log(user); // { id: 1, name: 'John Doe' }
140
+ * ```
141
+ *
142
+ *
143
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
144
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
145
+ * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
146
+ *
147
+ * @returns The schema.
148
+ */ omit(args) {
149
+ return super.omit(args);
150
+ }
151
+ /**
152
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
153
+ * like a database. You should always return the schema after you save the value, that way we will always have the correct type
154
+ * of the schema after the save operation.
155
+ *
156
+ * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * import * as p from '@palmares/schemas';
161
+ *
162
+ * import { User } from './models';
163
+ *
164
+ * const userSchema = p.object({
165
+ * id: p.number().optional(),
166
+ * name: p.string(),
167
+ * email: p.string().email(),
168
+ * }).onSave(async (value) => {
169
+ * // Create or update the user on the database using palmares models or any other library of your choice.
170
+ * if (value.id)
171
+ * await User.default.set(value, { search: { id: value.id } });
172
+ * else
173
+ * await User.default.set(value);
174
+ *
175
+ * return value;
176
+ * });
177
+ *
178
+ *
179
+ * // Then, on your controller, do something like this:
180
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
181
+ * if (isValid) {
182
+ * const savedValue = await save();
183
+ * return Response.json(savedValue, { status: 201 });
184
+ * }
185
+ *
186
+ * return Response.json({ errors }, { status: 400 });
187
+ * ```
188
+ *
189
+ * @param callback - The callback that will be called to save the value on an external source.
190
+ *
191
+ * @returns The schema.
192
+ */ onSave(callback) {
193
+ return super.onSave(callback);
194
+ }
195
+ /**
196
+ * This function is used to add a default value to the schema. If the value is either undefined or null, the default value will be used.
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * import * as p from '@palmares/schemas';
201
+ *
202
+ * const numberSchema = p.number().default(0);
203
+ *
204
+ * const { errors, parsed } = await numberSchema.parse(undefined);
205
+ *
206
+ * console.log(parsed); // 0
207
+ * ```
208
+ */ default(defaultValueOrFunction) {
209
+ return super.default(defaultValueOrFunction);
210
+ }
211
+ /**
212
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call this function to let you customize
213
+ * the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * import * as p from '@palmares/schemas';
218
+ *
219
+ * const numberSchema = p.number().extends((schema) => {
220
+ * return schema.nonnegative();
221
+ * });
222
+ *
223
+ * const { errors, parsed } = await numberSchema.parse(-1);
224
+ *
225
+ * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
226
+ * ```
227
+ *
228
+ * @param callback - The callback that will be called to customize the schema.
229
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to compile the underlying schema
230
+ * to a string so you can save it for future runs.
231
+ *
232
+ * @returns The schema.
233
+ */ extends(callback, toStringCallback) {
234
+ return super.extends(callback, toStringCallback);
235
+ }
236
+ /**
237
+ * This function is used to transform the value to the representation of the schema. When using the {@link data} function. With this function you have full
238
+ * control to add data cleaning for example, transforming the data and whatever. Another use case is when you want to return deeply nested recursive data.
239
+ * The schema maps to itself.
240
+ *
241
+ * @example
242
+ * ```typescript
243
+ * import * as p from '@palmares/schemas';
244
+ *
245
+ * const recursiveSchema = p.object({
246
+ * id: p.number().optional(),
247
+ * name: p.string(),
248
+ * }).toRepresentation(async (value) => {
249
+ * return {
250
+ * id: value.id,
251
+ * name: value.name,
252
+ * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
253
+ * }
254
+ * });
255
+ *
256
+ * const data = await recursiveSchema.data({
257
+ * id: 1,
258
+ * name: 'John Doe',
259
+ * });
260
+ * ```
261
+ *
262
+ * @example
263
+ * ```
264
+ * import * as p from '@palmares/schemas';
265
+ *
266
+ * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
267
+ * switch (value) {
268
+ * case 'red': return { r: 255, g: 0, b: 0 };
269
+ * case 'green': return { r: 0, g: 255, b: 0 };
270
+ * case 'blue': return { r: 0, g: 0, b: 255 };
271
+ * default: return { r: 0, g: 0, b: 0 };
272
+ * }
273
+ * });
274
+ * ```
275
+ * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
276
+ *
277
+ * @returns The schema with a new return type
278
+ */ toRepresentation(toRepresentationCallback) {
279
+ return super.toRepresentation(toRepresentationCallback);
280
+ }
281
+ /**
282
+ * This function is used to transform the value to the internal representation of the schema. This is useful when you want to transform the value
283
+ * to a type that the schema adapter can understand. For example, you might want to transform a string to a date. This is the function you use.
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * import * as p from '@palmares/schemas';
288
+ *
289
+ * const dateSchema = p.string().toInternal((value) => {
290
+ * return new Date(value);
291
+ * });
292
+ *
293
+ * const date = await dateSchema.parse('2021-01-01');
294
+ *
295
+ * console.log(date); // Date object
296
+ *
297
+ * const rgbToColorSchema = p.object({
298
+ * r: p.number().min(0).max(255),
299
+ * g: p.number().min(0).max(255),
300
+ * b: p.number().min(0).max(255),
301
+ * }).toInternal(async (value) => {
302
+ * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
303
+ * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
304
+ * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
305
+ * return `rgb(${value.r}, ${value.g}, ${value.b})`;
306
+ * });
307
+ * ```
308
+ *
309
+ * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
310
+ *
311
+ * @returns The schema with a new return type.
312
+ */ toInternal(toInternalCallback) {
313
+ return super.toInternal(toInternalCallback);
314
+ }
315
+ /**
316
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string, you can convert that string to a date
317
+ * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
318
+ *
319
+ * @example
320
+ * ```
321
+ * import * as p from '@palmares/schemas';
322
+ * import * as z from 'zod';
323
+ *
324
+ * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
325
+ * return new Map(value); // Before validating we transform the value to a map.
326
+ * });
327
+ *
328
+ * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
329
+ * ```
330
+ *
331
+ * @param toValidateCallback - The callback that will be called to validate the value.
332
+ *
333
+ * @returns The schema with a new return type.
334
+ */ toValidate(toValidateCallback) {
335
+ return super.toValidate(toValidateCallback);
336
+ }
337
+ /**
338
+ * This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the result to be a string as well.
339
+ *
340
+ * @example
341
+ * ```typescript
342
+ * const schema = datetime().allowString();
343
+ *
344
+ * schema.parse('2021-01-01T00:00:00.000Z') // works
345
+ * ```
346
+ *
347
+ * @returns - The schema instance
348
+ */ allowString() {
349
+ this.__allowString = true;
350
+ return this;
351
+ }
352
+ /**
353
+ * This will validate if the value is above the specified date.
354
+ *
355
+ * @example
356
+ * ```ts
357
+ * const today = new Date();
358
+ * const yesterday = new Date(today);
359
+ * yesterday.setDate(today.getDate() - 1);
360
+ *
361
+ * datetime().above(today).parse(today) // throws error
362
+ * datetime().above(today, { inclusive: true }).parse(today) // works
363
+ * datetime().above(yesterday).parse(today) // works
364
+ * ```
365
+ *
366
+ * @param value - The value that we are comparing against.
367
+ * @param options - The options that we are passing to the validator.
368
+ * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to the specified date.
369
+ * @param options.message - The message that we are returning if the value is not above the specified date.
370
+ *
371
+ * @returns - The schema instance
372
+ */ above(value, options) {
373
+ const inclusive = typeof options?.inclusive === 'boolean' ? options.inclusive : false;
374
+ const message = options?.message || 'Value is not above the specified date';
375
+ this.__above = {
376
+ value,
377
+ inclusive,
378
+ message
379
+ };
380
+ return this;
381
+ }
382
+ /**
383
+ * This will validate if the value is below the specified date.
384
+ *
385
+ * @example
386
+ * ```ts
387
+ * const today = new Date();
388
+ * const yesterday = new Date(today);
389
+ * yesterday.setDate(today.getDate() - 1);
390
+ *
391
+ * datetime().below(today).parse(today) // throws error
392
+ * datetime().below(today, { inclusive: true }).parse(today) // works
393
+ * datetime().below(today).parse(yesterday) // works
394
+ * ```
395
+ *
396
+ * @param value - The value that we are comparing against.
397
+ * @param options - The options that we are passing to the validator.
398
+ * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to the specified date.
399
+ * @param options.message - The message that we are returning if the value is not above the specified date.
400
+ *
401
+ * @returns - The schema instance
402
+ */ below(value, options) {
403
+ const inclusive = typeof options?.inclusive === 'boolean' ? options.inclusive : false;
404
+ const message = options?.message || 'Value is not below the specified date';
405
+ this.__below = {
406
+ value,
407
+ inclusive,
408
+ message
409
+ };
410
+ return this;
411
+ }
412
+ static new() {
413
+ const returnValue = new DatetimeSchema();
414
+ const adapterInstance = getDefaultAdapter();
415
+ returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
416
+ transformed: false,
417
+ adapter: adapterInstance,
418
+ schemas: []
419
+ };
420
+ return returnValue;
421
+ }
422
+ }
423
+ export const datetime = ()=>DatetimeSchema.new();
@@ -0,0 +1,3 @@
1
+ import NumberSchema from './number';
2
+ const number = NumberSchema.new; // teste
3
+ export { number };