@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,807 @@
1
+
2
+ import Schema from './schema';
3
+ import { getDefaultAdapter } from '../conf';
4
+ import { defaultTransform, defaultTransformToAdapter } from '../utils';
5
+ import { endsWith, includes, maxLength, minLength, regex, startsWith, stringValidation } from '../validators/string';
6
+
7
+ import type { DefinitionsOfSchemaType } from './types';
8
+ import type { Narrow } from '@palmares/core';
9
+
10
+ export default class StringSchema<
11
+ TType extends {
12
+ input: any;
13
+ validate: any;
14
+ internal: any;
15
+ output: any;
16
+ representation: any;
17
+ } = {
18
+ input: string;
19
+ output: string;
20
+ internal: string;
21
+ representation: string;
22
+ validate: string;
23
+ },
24
+ TDefinitions extends DefinitionsOfSchemaType = DefinitionsOfSchemaType,
25
+ > extends Schema<TType, TDefinitions> {
26
+ protected __is!: {
27
+ value: Narrow<TType['input'] | TType['input'][]>;
28
+ message: string;
29
+ };
30
+
31
+ protected __email!: {
32
+ message: string;
33
+ };
34
+
35
+
36
+ protected __uuid!: {
37
+ message: string;
38
+ };
39
+
40
+ protected __minLength!: {
41
+ value: number;
42
+ inclusive: boolean;
43
+ message: string;
44
+ };
45
+
46
+ protected __maxLength!: {
47
+ value: number;
48
+ inclusive: boolean;
49
+ message: string;
50
+ };
51
+
52
+ protected __regex!: {
53
+ value: RegExp;
54
+ message: string;
55
+ };
56
+ protected __endsWith!: {
57
+ value: string;
58
+ message: string;
59
+ };
60
+
61
+ protected __startsWith!: {
62
+ value: string;
63
+ message: string;
64
+ };
65
+
66
+ protected __includes!: {
67
+ value: string;
68
+ message: string;
69
+ };
70
+
71
+ protected async __transformToAdapter(options: Parameters<Schema['__transformToAdapter']>[0]): Promise<any> {
72
+ return defaultTransformToAdapter(
73
+ async (adapter) => {
74
+ return defaultTransform(
75
+ 'string',
76
+ this,
77
+ adapter,
78
+ adapter.string,
79
+ () => ({
80
+ minLength: this.__minLength,
81
+ maxLength: this.__maxLength,
82
+ regex: this.__regex,
83
+ endsWith: this.__endsWith,
84
+ startsWith: this.__startsWith,
85
+ includes: this.__includes,
86
+ nullable: this.__nullable,
87
+ optional: this.__optional,
88
+ parsers: {
89
+ nullable: this.__nullable.allow,
90
+ optional: this.__optional.allow,
91
+ }
92
+ }),
93
+ {
94
+ maxLength,
95
+ minLength,
96
+ endsWith,
97
+ startsWith,
98
+ regex,
99
+ includes,
100
+ },
101
+ {
102
+ validatorsIfFallbackOrNotSupported: stringValidation(),
103
+ shouldAddStringVersion: options.shouldAddStringVersion,
104
+ // eslint-disable-next-line ts/require-await
105
+ fallbackIfNotSupported: async () => {
106
+ return [];
107
+ },
108
+ }
109
+ );
110
+ },
111
+ this.__transformedSchemas,
112
+ options,
113
+ 'number'
114
+ );
115
+ }
116
+
117
+ /**
118
+ * 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.
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * import * as p from '@palmares/schemas';
123
+ *
124
+ * const numberSchema = p.number().refine((value) => {
125
+ * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
126
+ * });
127
+ *
128
+ * const { errors, parsed } = await numberSchema.parse(-1);
129
+ *
130
+ * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
131
+ * ```
132
+ *
133
+ * @param refinementCallback - The callback that will be called to validate the value.
134
+ * @param options - Options for the refinement.
135
+ * @param options.isAsync - Whether the callback is async or not. Defaults to true.
136
+ */
137
+ refine(
138
+ refinementCallback: (value: TType['input']) => Promise<void | undefined | { code: string; message: string }> | void | undefined | { code: string; message: string }
139
+ ) {
140
+ return super.refine(refinementCallback) as unknown as StringSchema<
141
+ {
142
+ input: TType['input'];
143
+ validate: TType['validate'];
144
+ internal: TType['internal'];
145
+ output: TType['output'];
146
+ representation: TType['representation'];
147
+ }, TDefinitions
148
+ >;
149
+ }
150
+
151
+ /**
152
+ * Allows the value to be either undefined or null.
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * import * as p from '@palmares/schemas';
157
+ *
158
+ * const numberSchema = p.number().optional();
159
+ *
160
+ * const { errors, parsed } = await numberSchema.parse(undefined);
161
+ *
162
+ * console.log(parsed); // undefined
163
+ *
164
+ * const { errors, parsed } = await numberSchema.parse(null);
165
+ *
166
+ * console.log(parsed); // null
167
+ *
168
+ * const { errors, parsed } = await numberSchema.parse(1);
169
+ *
170
+ * console.log(parsed); // 1
171
+ * ```
172
+ *
173
+ * @returns - The schema we are working with.
174
+ */
175
+ optional(options?: { message: string; allow: false }) {
176
+ return super.optional(options) as unknown as StringSchema<
177
+ {
178
+ input: TType['input'] | undefined | null;
179
+ validate: TType['validate'] | undefined | null;
180
+ internal: TType['internal'] | undefined | null;
181
+ output: TType['output'] | undefined | null;
182
+ representation: TType['representation'] | undefined | null;
183
+ },
184
+ TDefinitions
185
+ >;
186
+ }
187
+
188
+ /**
189
+ * 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
190
+ * the { message: 'Your custom message', allow: false } on the options.
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * import * as p from '@palmares/schemas';
195
+ *
196
+ * const numberSchema = p.number().nullable();
197
+ *
198
+ * const { errors, parsed } = await numberSchema.parse(null);
199
+ *
200
+ * console.log(parsed); // null
201
+ *
202
+ * const { errors, parsed } = await numberSchema.parse(undefined);
203
+ *
204
+ * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
205
+ * ```
206
+ *
207
+ * @param options - The options for the nullable function.
208
+ * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
209
+ * @param options.allow - Whether the value can be null or not. Defaults to true.
210
+ *
211
+ * @returns The schema.
212
+ */
213
+ nullable(options?: { message: string; allow: false }) {
214
+ return super.nullable(options) as unknown as StringSchema<
215
+ {
216
+ input: TType['input'] | null;
217
+ validate: TType['validate'] | null;
218
+ internal: TType['internal'] | null;
219
+ output: TType['output'] | null;
220
+ representation: TType['representation'] | null;
221
+ },
222
+ TDefinitions
223
+ >;
224
+ }
225
+
226
+ /**
227
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
228
+ * otherwise it will set the value to undefined after it's validated.
229
+ * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
230
+ * function. This will remove the value from the representation of the schema.
231
+ *
232
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
233
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
234
+ * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * import * as p from '@palmares/schemas';
239
+ *
240
+ * const userSchema = p.object({
241
+ * id: p.number().optional(),
242
+ * name: p.string(),
243
+ * password: p.string().omit()
244
+ * });
245
+ *
246
+ * const user = await userSchema.data({
247
+ * id: 1,
248
+ * name: 'John Doe',
249
+ * password: '123456'
250
+ * });
251
+ *
252
+ * console.log(user); // { id: 1, name: 'John Doe' }
253
+ * ```
254
+ *
255
+ *
256
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
257
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
258
+ * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
259
+ *
260
+ * @returns The schema.
261
+ */
262
+ omit<
263
+ TToInternal extends boolean,
264
+ TToRepresentation extends boolean = boolean extends TToInternal ? true : false
265
+ >(args?: { toInternal?: TToInternal, toRepresentation?: TToRepresentation }) {
266
+ return super.omit(args) as unknown as StringSchema<
267
+ {
268
+ input: TToInternal extends true ? TType['input'] | undefined : TType['input'];
269
+ validate: TToInternal extends true ? TType['validate'] | undefined : TType['validate'];
270
+ internal: TToInternal extends true ? undefined : TType['internal'];
271
+ output: TToRepresentation extends true ? TType['output'] | undefined : TType['output'];
272
+ representation: TToRepresentation extends true ? undefined : TType['representation'];
273
+ },
274
+ TDefinitions
275
+ >;
276
+ }
277
+
278
+ /**
279
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
280
+ * like a database. You should always return the schema after you save the value, that way we will always have the correct type
281
+ * of the schema after the save operation.
282
+ *
283
+ * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * import * as p from '@palmares/schemas';
288
+ *
289
+ * import { User } from './models';
290
+ *
291
+ * const userSchema = p.object({
292
+ * id: p.number().optional(),
293
+ * name: p.string(),
294
+ * email: p.string().email(),
295
+ * }).onSave(async (value) => {
296
+ * // Create or update the user on the database using palmares models or any other library of your choice.
297
+ * if (value.id)
298
+ * await User.default.set(value, { search: { id: value.id } });
299
+ * else
300
+ * await User.default.set(value);
301
+ *
302
+ * return value;
303
+ * });
304
+ *
305
+ *
306
+ * // Then, on your controller, do something like this:
307
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
308
+ * if (isValid) {
309
+ * const savedValue = await save();
310
+ * return Response.json(savedValue, { status: 201 });
311
+ * }
312
+ *
313
+ * return Response.json({ errors }, { status: 400 });
314
+ * ```
315
+ *
316
+ * @param callback - The callback that will be called to save the value on an external source.
317
+ *
318
+ * @returns The schema.
319
+ */
320
+ onSave(callback: (value: TType['internal']) => Promise<TType['output']> | TType['output']) {
321
+ return super.onSave(callback) as unknown as StringSchema<
322
+ {
323
+ input: TType['input'];
324
+ validate: TType['validate'];
325
+ internal: TType['internal'];
326
+ output: TType['output'];
327
+ representation: TType['representation'];
328
+ },
329
+ TDefinitions & {
330
+ hasSave: true;
331
+ }
332
+ >;
333
+ }
334
+
335
+
336
+ /**
337
+ * 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.
338
+ *
339
+ * @example
340
+ * ```typescript
341
+ * import * as p from '@palmares/schemas';
342
+ *
343
+ * const numberSchema = p.number().default(0);
344
+ *
345
+ * const { errors, parsed } = await numberSchema.parse(undefined);
346
+ *
347
+ * console.log(parsed); // 0
348
+ * ```
349
+ */
350
+ default<TDefaultValue extends TType['input'] | (() => Promise<TType['input']>)>(
351
+ defaultValueOrFunction: TDefaultValue
352
+ ) {
353
+ return super.default(defaultValueOrFunction) as unknown as StringSchema<
354
+ {
355
+ input: TType['input'] | undefined | null;
356
+ validate: TType['validate'];
357
+ internal: TType['internal'];
358
+ output: TType['output'] | undefined | null;
359
+ representation: TType['representation'];
360
+ },
361
+ TDefinitions
362
+ >;
363
+ }
364
+
365
+ /**
366
+ * 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
367
+ * the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
368
+ *
369
+ * @example
370
+ * ```typescript
371
+ * import * as p from '@palmares/schemas';
372
+ *
373
+ * const numberSchema = p.number().extends((schema) => {
374
+ * return schema.nonnegative();
375
+ * });
376
+ *
377
+ * const { errors, parsed } = await numberSchema.parse(-1);
378
+ *
379
+ * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
380
+ * ```
381
+ *
382
+ * @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 compile the underlying schema
384
+ * to a string so you can save it for future runs.
385
+ *
386
+ * @returns The schema.
387
+ */
388
+ extends(
389
+ callback: (
390
+ schema: Awaited<ReturnType<NonNullable<TDefinitions['schemaAdapter']['string']>['translate']>>
391
+ ) => Awaited<ReturnType<NonNullable<TDefinitions['schemaAdapter']['field']>['translate']>> | any,
392
+ toStringCallback?: (schemaAsString: string) => string
393
+ ) {
394
+ return super.extends(callback, toStringCallback);
395
+ }
396
+
397
+ /**
398
+ * 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
399
+ * 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.
400
+ * The schema maps to itself.
401
+ *
402
+ * @example
403
+ * ```typescript
404
+ * import * as p from '@palmares/schemas';
405
+ *
406
+ * const recursiveSchema = p.object({
407
+ * id: p.number().optional(),
408
+ * name: p.string(),
409
+ * }).toRepresentation(async (value) => {
410
+ * return {
411
+ * id: value.id,
412
+ * name: value.name,
413
+ * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
414
+ * }
415
+ * });
416
+ *
417
+ * const data = await recursiveSchema.data({
418
+ * id: 1,
419
+ * name: 'John Doe',
420
+ * });
421
+ * ```
422
+ *
423
+ * @example
424
+ * ```
425
+ * import * as p from '@palmares/schemas';
426
+ *
427
+ * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
428
+ * switch (value) {
429
+ * case 'red': return { r: 255, g: 0, b: 0 };
430
+ * case 'green': return { r: 0, g: 255, b: 0 };
431
+ * case 'blue': return { r: 0, g: 0, b: 255 };
432
+ * default: return { r: 0, g: 0, b: 0 };
433
+ * }
434
+ * });
435
+ * ```
436
+ * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
437
+ *
438
+ * @returns The schema with a new return type
439
+ */
440
+ toRepresentation<TRepresentation>(
441
+ toRepresentationCallback: (value: TType['representation']) => Promise<TRepresentation>
442
+ ) {
443
+ return super.toRepresentation(toRepresentationCallback) as unknown as StringSchema<
444
+ {
445
+ input: TType['input'];
446
+ validate: TType['validate'];
447
+ internal: TType['internal'];
448
+ output: TType['output'];
449
+ representation: TRepresentation;
450
+ },
451
+ TDefinitions
452
+ >;
453
+ }
454
+
455
+ /**
456
+ * 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
457
+ * 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.
458
+ *
459
+ * @example
460
+ * ```typescript
461
+ * import * as p from '@palmares/schemas';
462
+ *
463
+ * const dateSchema = p.string().toInternal((value) => {
464
+ * return new Date(value);
465
+ * });
466
+ *
467
+ * const date = await dateSchema.parse('2021-01-01');
468
+ *
469
+ * console.log(date); // Date object
470
+ *
471
+ * const rgbToColorSchema = p.object({
472
+ * r: p.number().min(0).max(255),
473
+ * g: p.number().min(0).max(255),
474
+ * b: p.number().min(0).max(255),
475
+ * }).toInternal(async (value) => {
476
+ * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
477
+ * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
478
+ * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
479
+ * return `rgb(${value.r}, ${value.g}, ${value.b})`;
480
+ * });
481
+ * ```
482
+ *
483
+ * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
484
+ *
485
+ * @returns The schema with a new return type.
486
+ */
487
+ toInternal<TInternal>(toInternalCallback: (value: TType['validate']) => Promise<TInternal>) {
488
+ return super.toInternal(toInternalCallback) as unknown as StringSchema<
489
+ {
490
+ input: TType['input'];
491
+ validate: TType['validate'];
492
+ internal: TInternal;
493
+ output: TType['output'];
494
+ representation: TType['representation'];
495
+ },
496
+ TDefinitions
497
+ >;
498
+ }
499
+
500
+ /**
501
+ * 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
502
+ * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
503
+ *
504
+ * @example
505
+ * ```typescript
506
+ * import * as p from '@palmares/schemas';
507
+ * import * as z from 'zod';
508
+ *
509
+ * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
510
+ * return new Map(value); // Before validating we transform the value to a map.
511
+ * });
512
+ *
513
+ * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
514
+ * ```
515
+ *
516
+ * @param toValidateCallback - The callback that will be called to validate the value.
517
+ *
518
+ * @returns The schema with a new return type.
519
+ */
520
+ toValidate<TValidate>(toValidateCallback: (value: TType['input']) => Promise<TValidate> | TValidate) {
521
+ return super.toValidate(toValidateCallback) as unknown as StringSchema<
522
+ {
523
+ input: TType['input'];
524
+ validate: TValidate;
525
+ internal: TType['internal'];
526
+ output: TType['output'];
527
+ representation: TType['representation'];
528
+ },
529
+ TDefinitions
530
+ >;
531
+ }
532
+
533
+ /**
534
+ * Defines a list of strings that are allowed, it's useful when you want to restrict the values that are allowed. Like a selector or a Choice field.
535
+ *
536
+ * @example
537
+ * ```typescript
538
+ * import * as p from '@palmares/schema';
539
+ *
540
+ * const schema = p.string().is(['Argentina', 'Brazil', 'Chile']);
541
+ *
542
+ * schema.parse('Argentina'); // { errors: [], parsed: 'Argentina' }
543
+ * schema.parse('Uruguay'); // { errors: [{ code: 'invalid_value', message: 'The value should be equal to Argentina, Brazil, Chile', path: [] }], parsed: 'Uruguay' }
544
+ * ```
545
+ *
546
+ * @param value - The list of numbers that are allowed
547
+ *
548
+ * @returns - The schema instance
549
+ */
550
+ is<const TValue extends TType['input'][]>(value: TValue) {
551
+ this.__is = {
552
+ value,
553
+ message: `The value should be equal to ${value.join(', ')}`,
554
+ };
555
+
556
+ return this as any as Schema<
557
+ {
558
+ input: TValue[number];
559
+ output: TValue[number];
560
+ internal: TValue[number];
561
+ representation: TValue[number];
562
+ validate: TValue[number];
563
+ },
564
+ TDefinitions
565
+ >;
566
+ }
567
+
568
+ /**
569
+ * Validates if the string ends with a specific value.
570
+ *
571
+ * @example
572
+ * ```typescript
573
+ * import * as p from '@palmares/schema';
574
+ *
575
+ * const schema = p.string().endsWith('.com');
576
+ *
577
+ * schema.parse('example.com'); // { errors: [], parsed: 'example.com' }
578
+ * schema.parse('example.org'); // { errors: [{ code: 'endsWith', message: 'The value should end with .com', path: [] }], parsed: 'example.org' }
579
+ * ```
580
+ *
581
+ * @param value - The value that the string should end with.
582
+ * @param options - The options for the endsWith function.
583
+ * @param options.message - The message to be shown when the value does not end with the value.
584
+ *
585
+ * @returns - The schema instance.
586
+ */
587
+ endsWith(value: string, options?: Partial<Omit<StringSchema['__endsWith'], 'value'>>) {
588
+ this.__endsWith = {
589
+ value,
590
+ message: options?.message || `The value should end with ${value}`,
591
+ };
592
+ return this;
593
+ }
594
+
595
+ /**
596
+ * Validates if the string starts with a specific value.
597
+ *
598
+ * @example
599
+ * ```typescript
600
+ * import * as p from '@palmares/schema';
601
+ *
602
+ * const schema = p.string().startsWith('https://');
603
+ *
604
+ * schema.parse('https://example.com'); // { errors: [], parsed: 'https://example.com' }
605
+ * schema.parse('http://example.com'); // { errors: [{ code: 'startsWith', message: 'The value should start with https://', path: [] }], parsed: 'http://example.com' }
606
+ * ```
607
+ *
608
+ * @param value - The value that the string should start with.
609
+ * @param options - The options for the startsWith function.
610
+ * @param options.message - The message to be shown when the value does not start with the value.
611
+ *
612
+ * @returns - The schema instance.
613
+ */
614
+ startsWith(value: string , options?: Partial<Omit<StringSchema['__startsWith'], 'value'>>) {
615
+ this.__startsWith = {
616
+ value,
617
+ message: options?.message || `The value should start with ${value}`,
618
+ };
619
+ return this;
620
+ }
621
+
622
+ /**
623
+ * Checks if the string includes a specific substring.
624
+ *
625
+ * @example
626
+ * ```typescript
627
+ * import * as p from '@palmares/schema';
628
+ *
629
+ * const schema = p.string().includes('for babies');
630
+ *
631
+ * schema.parse('Computer graphics for babies'); // { errors: [], parsed: 'Computer graphics for babies' }
632
+ * schema.parse('Learn javascript as you were 5'); // { errors: [{ code: 'includes', message: 'The string value should include the following substring 'for babies', path: [] }], parsed: 'example.org' }
633
+ * ```
634
+ *
635
+ * @param value - The value that the string should include.
636
+ * @param options - The options for the includes function.
637
+ * @param options.message - The message to be shown when the value does not include the value.
638
+ *
639
+ * @returns - The schema instance.
640
+ */
641
+ includes(value: string, options?: Partial<Omit<StringSchema['__includes'], 'value'>>) {
642
+ this.__includes = {
643
+ value,
644
+ message: options?.message || `The string value should include the following substring '${value}'`,
645
+ };
646
+ return this;
647
+ }
648
+
649
+ /**
650
+ * Validates if the string matches a specific regex.
651
+ *
652
+ * @example
653
+ * ```typescript
654
+ * import * as p from '@palmares/schema';
655
+ *
656
+ * const schema = p.string().regex(/^[a-z]+$/);
657
+ *
658
+ * schema.parse('abc'); // { errors: [], parsed: 'abc' }
659
+ * schema.parse('123'); // { errors: [{ code: 'regex', message: 'The value should match the following regex /^[a-z]+$/', path: [] }], parsed: '123' }
660
+ * ```
661
+ *
662
+ * @param value - The regex that the string should match.
663
+ * @param options - The options for the regex function.
664
+ * @param options.message - The message to be shown when the value does not match the regex.
665
+ *
666
+ * @returns - The schema instance.
667
+ */
668
+ regex(value: RegExp, options?: Partial<Omit<StringSchema['__regex'], 'value'>>) {
669
+ this.__regex = {
670
+ value,
671
+ message: options?.message || `The value should match the following regex '${value.toString()}'`,
672
+ };
673
+ return this;
674
+ }
675
+
676
+ /**
677
+ * Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length as the maximum length.
678
+ *
679
+ * @example
680
+ * ```typescript
681
+ * import * as p from '@palmares/schema';
682
+ *
683
+ * const schema = p.string().maxLength(5);
684
+ *
685
+ * schema.parse('12345'); // { errors: [], parsed: '12345' }
686
+ * schema.parse('123456'); // { errors: [{ code: 'maxLength', message: 'The value should have a maximum length of 5', path: [] }], parsed: '123
687
+ * ```
688
+ *
689
+ * @param value - The maximum length that the string should have.
690
+ * @param options - The options for the maxLength function.
691
+ * @param options.message - The message to be shown when the value has a length greater than the maximum length.
692
+ * @param options.inclusive - Whether the value can have the same length as the maximum length. Defaults to false.
693
+ *
694
+ * @returns - The schema instance.
695
+ */
696
+ maxLength(value: number, options?: Partial<Omit<StringSchema['__maxLength'], 'value'>>) {
697
+ this.__maxLength = {
698
+ value,
699
+ message: options?.message || `The value should have a maximum length of ${value}`,
700
+ inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false,
701
+ };
702
+ return this;
703
+ }
704
+
705
+ /**
706
+ * Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length as the minimum length.
707
+ *
708
+ * @example
709
+ * ```typescript
710
+ * import * as p from '@palmares/schema';
711
+ *
712
+ * const schema = p.string().minLength(5);
713
+ *
714
+ * schema.parse('12345'); // { errors: [], parsed: '12345' }
715
+ * schema.parse('1234'); // { errors: [{ code: 'minLength', message: 'The value should have a minimum length of 5', path: [] }], parsed: '1234' }
716
+ * ```
717
+ *
718
+ * @param value - The minimum length that the string should have.
719
+ * @param options - The options for the minLength function.
720
+ * @param options.message - The message to be shown when the value has a length less than the minimum length.
721
+ * @param options.inclusive - Whether the value can have the same length as the minimum length. Defaults to false.
722
+ *
723
+ * @returns - The schema instance.
724
+ */
725
+ minLength(value: number, options?: Partial<Omit<StringSchema['__minLength'], 'value'>>) {
726
+ this.__minLength = {
727
+ value,
728
+ message: options?.message || `The value should have a minimum length of ${value}`,
729
+ inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false,
730
+ };
731
+ return this;
732
+ }
733
+
734
+ /**
735
+ * Validates if the string is a valid UUID.
736
+ *
737
+ * @example
738
+ * ```typescript
739
+ * import * as p from '@palmares/schema';
740
+ *
741
+ * const schema = p.string().uuid();
742
+ *
743
+ * schema.parse('550e8400-e29b-41d4-a716-446655440000'); // { errors: [], parsed: '550e8400-e29b-41d4-a716-446655440000' }
744
+ * ```
745
+ *
746
+ * @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 'The value should be a valid UUID'.
748
+ *
749
+ * @returns - The schema instance.
750
+ */
751
+ uuid(options?: StringSchema['__uuid']) {
752
+ this.__uuid = {
753
+ message: options?.message || 'The value should be a valid UUID',
754
+ };
755
+ return this;
756
+ }
757
+
758
+
759
+ /**
760
+ * Validates if the string is a valid email or not
761
+ *
762
+ * @example
763
+ * ```typescript
764
+ *
765
+ * import * as p from '@palmares/schema';
766
+ *
767
+ * const schema = p.string().email();
768
+ *
769
+ * schema.parse('john.doe@example.com'); // { errors: [], parsed: 'john.doe@example.com' }
770
+ * ```
771
+ *
772
+ * @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. Defaults to 'The value should be a valid email'.
774
+ *
775
+ * @returns - The schema instance.
776
+ */
777
+ email(options?: StringSchema['__email']) {
778
+ this.__email = {
779
+ message: options?.message || 'The value should be a valid email',
780
+ }
781
+ return this;
782
+ }
783
+
784
+ static new<TDefinitions extends DefinitionsOfSchemaType>() {
785
+ const returnValue = new StringSchema<
786
+ {
787
+ input: string;
788
+ output: string;
789
+ internal: string;
790
+ representation: string;
791
+ validate: string;
792
+ },
793
+ TDefinitions
794
+ >();
795
+ const adapterInstance = getDefaultAdapter();
796
+
797
+ returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
798
+ transformed: false,
799
+ adapter: adapterInstance,
800
+ schemas: [],
801
+ };
802
+
803
+ return returnValue;
804
+ }
805
+ }
806
+
807
+ export const string = StringSchema.new;