@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,579 @@
1
+ import Schema from './schema';
2
+ import { getDefaultAdapter } from '../conf';
3
+ import { defaultTransform, defaultTransformToAdapter } from '../utils';
4
+ import { endsWith, includes, maxLength, minLength, regex, startsWith, stringValidation } from '../validators/string';
5
+ export default class StringSchema extends Schema {
6
+ __is;
7
+ __email;
8
+ __uuid;
9
+ __minLength;
10
+ __maxLength;
11
+ __regex;
12
+ __endsWith;
13
+ __startsWith;
14
+ __includes;
15
+ async __transformToAdapter(options) {
16
+ return defaultTransformToAdapter(async (adapter)=>{
17
+ return defaultTransform('string', this, adapter, adapter.string, ()=>({
18
+ minLength: this.__minLength,
19
+ maxLength: this.__maxLength,
20
+ regex: this.__regex,
21
+ endsWith: this.__endsWith,
22
+ startsWith: this.__startsWith,
23
+ includes: this.__includes,
24
+ nullable: this.__nullable,
25
+ optional: this.__optional,
26
+ parsers: {
27
+ nullable: this.__nullable.allow,
28
+ optional: this.__optional.allow
29
+ }
30
+ }), {
31
+ maxLength,
32
+ minLength,
33
+ endsWith,
34
+ startsWith,
35
+ regex,
36
+ includes
37
+ }, {
38
+ validatorsIfFallbackOrNotSupported: stringValidation(),
39
+ shouldAddStringVersion: options.shouldAddStringVersion,
40
+ // eslint-disable-next-line ts/require-await
41
+ fallbackIfNotSupported: async ()=>{
42
+ return [];
43
+ }
44
+ });
45
+ }, this.__transformedSchemas, options, 'number');
46
+ }
47
+ /**
48
+ * 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.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * import * as p from '@palmares/schemas';
53
+ *
54
+ * const numberSchema = p.number().refine((value) => {
55
+ * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
56
+ * });
57
+ *
58
+ * const { errors, parsed } = await numberSchema.parse(-1);
59
+ *
60
+ * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
61
+ * ```
62
+ *
63
+ * @param refinementCallback - The callback that will be called to validate the value.
64
+ * @param options - Options for the refinement.
65
+ * @param options.isAsync - Whether the callback is async or not. Defaults to true.
66
+ */ refine(refinementCallback) {
67
+ return super.refine(refinementCallback);
68
+ }
69
+ /**
70
+ * Allows the value to be either undefined or null.
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * import * as p from '@palmares/schemas';
75
+ *
76
+ * const numberSchema = p.number().optional();
77
+ *
78
+ * const { errors, parsed } = await numberSchema.parse(undefined);
79
+ *
80
+ * console.log(parsed); // undefined
81
+ *
82
+ * const { errors, parsed } = await numberSchema.parse(null);
83
+ *
84
+ * console.log(parsed); // null
85
+ *
86
+ * const { errors, parsed } = await numberSchema.parse(1);
87
+ *
88
+ * console.log(parsed); // 1
89
+ * ```
90
+ *
91
+ * @returns - The schema we are working with.
92
+ */ optional(options) {
93
+ return super.optional(options);
94
+ }
95
+ /**
96
+ * 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
97
+ * the { message: 'Your custom message', allow: false } on the options.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * import * as p from '@palmares/schemas';
102
+ *
103
+ * const numberSchema = p.number().nullable();
104
+ *
105
+ * const { errors, parsed } = await numberSchema.parse(null);
106
+ *
107
+ * console.log(parsed); // null
108
+ *
109
+ * const { errors, parsed } = await numberSchema.parse(undefined);
110
+ *
111
+ * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
112
+ * ```
113
+ *
114
+ * @param options - The options for the nullable function.
115
+ * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
116
+ * @param options.allow - Whether the value can be null or not. Defaults to true.
117
+ *
118
+ * @returns The schema.
119
+ */ nullable(options) {
120
+ return super.nullable(options);
121
+ }
122
+ /**
123
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
124
+ * otherwise it will set the value to undefined after it's validated.
125
+ * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
126
+ * function. This will remove the value from the representation of the schema.
127
+ *
128
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
129
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
130
+ * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
131
+ *
132
+ * @example
133
+ * ```typescript
134
+ * import * as p from '@palmares/schemas';
135
+ *
136
+ * const userSchema = p.object({
137
+ * id: p.number().optional(),
138
+ * name: p.string(),
139
+ * password: p.string().omit()
140
+ * });
141
+ *
142
+ * const user = await userSchema.data({
143
+ * id: 1,
144
+ * name: 'John Doe',
145
+ * password: '123456'
146
+ * });
147
+ *
148
+ * console.log(user); // { id: 1, name: 'John Doe' }
149
+ * ```
150
+ *
151
+ *
152
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
153
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
154
+ * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
155
+ *
156
+ * @returns The schema.
157
+ */ omit(args) {
158
+ return super.omit(args);
159
+ }
160
+ /**
161
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
162
+ * like a database. You should always return the schema after you save the value, that way we will always have the correct type
163
+ * of the schema after the save operation.
164
+ *
165
+ * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * import * as p from '@palmares/schemas';
170
+ *
171
+ * import { User } from './models';
172
+ *
173
+ * const userSchema = p.object({
174
+ * id: p.number().optional(),
175
+ * name: p.string(),
176
+ * email: p.string().email(),
177
+ * }).onSave(async (value) => {
178
+ * // Create or update the user on the database using palmares models or any other library of your choice.
179
+ * if (value.id)
180
+ * await User.default.set(value, { search: { id: value.id } });
181
+ * else
182
+ * await User.default.set(value);
183
+ *
184
+ * return value;
185
+ * });
186
+ *
187
+ *
188
+ * // Then, on your controller, do something like this:
189
+ * const { isValid, save, errors } = await userSchema.validate(req.body);
190
+ * if (isValid) {
191
+ * const savedValue = await save();
192
+ * return Response.json(savedValue, { status: 201 });
193
+ * }
194
+ *
195
+ * return Response.json({ errors }, { status: 400 });
196
+ * ```
197
+ *
198
+ * @param callback - The callback that will be called to save the value on an external source.
199
+ *
200
+ * @returns The schema.
201
+ */ onSave(callback) {
202
+ return super.onSave(callback);
203
+ }
204
+ /**
205
+ * 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.
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * import * as p from '@palmares/schemas';
210
+ *
211
+ * const numberSchema = p.number().default(0);
212
+ *
213
+ * const { errors, parsed } = await numberSchema.parse(undefined);
214
+ *
215
+ * console.log(parsed); // 0
216
+ * ```
217
+ */ default(defaultValueOrFunction) {
218
+ return super.default(defaultValueOrFunction);
219
+ }
220
+ /**
221
+ * 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
222
+ * the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * import * as p from '@palmares/schemas';
227
+ *
228
+ * const numberSchema = p.number().extends((schema) => {
229
+ * return schema.nonnegative();
230
+ * });
231
+ *
232
+ * const { errors, parsed } = await numberSchema.parse(-1);
233
+ *
234
+ * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
235
+ * ```
236
+ *
237
+ * @param callback - The callback that will be called to customize the schema.
238
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to compile the underlying schema
239
+ * to a string so you can save it for future runs.
240
+ *
241
+ * @returns The schema.
242
+ */ extends(callback, toStringCallback) {
243
+ return super.extends(callback, toStringCallback);
244
+ }
245
+ /**
246
+ * 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
247
+ * 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.
248
+ * The schema maps to itself.
249
+ *
250
+ * @example
251
+ * ```typescript
252
+ * import * as p from '@palmares/schemas';
253
+ *
254
+ * const recursiveSchema = p.object({
255
+ * id: p.number().optional(),
256
+ * name: p.string(),
257
+ * }).toRepresentation(async (value) => {
258
+ * return {
259
+ * id: value.id,
260
+ * name: value.name,
261
+ * children: await Promise.all(value.children.map(async (child) => await recursiveSchema.data(child)))
262
+ * }
263
+ * });
264
+ *
265
+ * const data = await recursiveSchema.data({
266
+ * id: 1,
267
+ * name: 'John Doe',
268
+ * });
269
+ * ```
270
+ *
271
+ * @example
272
+ * ```
273
+ * import * as p from '@palmares/schemas';
274
+ *
275
+ * const colorToRGBSchema = p.string().toRepresentation(async (value) => {
276
+ * switch (value) {
277
+ * case 'red': return { r: 255, g: 0, b: 0 };
278
+ * case 'green': return { r: 0, g: 255, b: 0 };
279
+ * case 'blue': return { r: 0, g: 0, b: 255 };
280
+ * default: return { r: 0, g: 0, b: 0 };
281
+ * }
282
+ * });
283
+ * ```
284
+ * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
285
+ *
286
+ * @returns The schema with a new return type
287
+ */ toRepresentation(toRepresentationCallback) {
288
+ return super.toRepresentation(toRepresentationCallback);
289
+ }
290
+ /**
291
+ * 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
292
+ * 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.
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * import * as p from '@palmares/schemas';
297
+ *
298
+ * const dateSchema = p.string().toInternal((value) => {
299
+ * return new Date(value);
300
+ * });
301
+ *
302
+ * const date = await dateSchema.parse('2021-01-01');
303
+ *
304
+ * console.log(date); // Date object
305
+ *
306
+ * const rgbToColorSchema = p.object({
307
+ * r: p.number().min(0).max(255),
308
+ * g: p.number().min(0).max(255),
309
+ * b: p.number().min(0).max(255),
310
+ * }).toInternal(async (value) => {
311
+ * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
312
+ * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
313
+ * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
314
+ * return `rgb(${value.r}, ${value.g}, ${value.b})`;
315
+ * });
316
+ * ```
317
+ *
318
+ * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
319
+ *
320
+ * @returns The schema with a new return type.
321
+ */ toInternal(toInternalCallback) {
322
+ return super.toInternal(toInternalCallback);
323
+ }
324
+ /**
325
+ * 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
326
+ * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
327
+ *
328
+ * @example
329
+ * ```typescript
330
+ * import * as p from '@palmares/schemas';
331
+ * import * as z from 'zod';
332
+ *
333
+ * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
334
+ * return new Map(value); // Before validating we transform the value to a map.
335
+ * });
336
+ *
337
+ * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
338
+ * ```
339
+ *
340
+ * @param toValidateCallback - The callback that will be called to validate the value.
341
+ *
342
+ * @returns The schema with a new return type.
343
+ */ toValidate(toValidateCallback) {
344
+ return super.toValidate(toValidateCallback);
345
+ }
346
+ /**
347
+ * 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.
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * import * as p from '@palmares/schema';
352
+ *
353
+ * const schema = p.string().is(['Argentina', 'Brazil', 'Chile']);
354
+ *
355
+ * schema.parse('Argentina'); // { errors: [], parsed: 'Argentina' }
356
+ * schema.parse('Uruguay'); // { errors: [{ code: 'invalid_value', message: 'The value should be equal to Argentina, Brazil, Chile', path: [] }], parsed: 'Uruguay' }
357
+ * ```
358
+ *
359
+ * @param value - The list of numbers that are allowed
360
+ *
361
+ * @returns - The schema instance
362
+ */ is(value) {
363
+ this.__is = {
364
+ value,
365
+ message: `The value should be equal to ${value.join(', ')}`
366
+ };
367
+ return this;
368
+ }
369
+ /**
370
+ * Validates if the string ends with a specific value.
371
+ *
372
+ * @example
373
+ * ```typescript
374
+ * import * as p from '@palmares/schema';
375
+ *
376
+ * const schema = p.string().endsWith('.com');
377
+ *
378
+ * schema.parse('example.com'); // { errors: [], parsed: 'example.com' }
379
+ * schema.parse('example.org'); // { errors: [{ code: 'endsWith', message: 'The value should end with .com', path: [] }], parsed: 'example.org' }
380
+ * ```
381
+ *
382
+ * @param value - The value that the string should end with.
383
+ * @param options - The options for the endsWith function.
384
+ * @param options.message - The message to be shown when the value does not end with the value.
385
+ *
386
+ * @returns - The schema instance.
387
+ */ endsWith(value, options) {
388
+ this.__endsWith = {
389
+ value,
390
+ message: options?.message || `The value should end with ${value}`
391
+ };
392
+ return this;
393
+ }
394
+ /**
395
+ * Validates if the string starts with a specific value.
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * import * as p from '@palmares/schema';
400
+ *
401
+ * const schema = p.string().startsWith('https://');
402
+ *
403
+ * schema.parse('https://example.com'); // { errors: [], parsed: 'https://example.com' }
404
+ * schema.parse('http://example.com'); // { errors: [{ code: 'startsWith', message: 'The value should start with https://', path: [] }], parsed: 'http://example.com' }
405
+ * ```
406
+ *
407
+ * @param value - The value that the string should start with.
408
+ * @param options - The options for the startsWith function.
409
+ * @param options.message - The message to be shown when the value does not start with the value.
410
+ *
411
+ * @returns - The schema instance.
412
+ */ startsWith(value, options) {
413
+ this.__startsWith = {
414
+ value,
415
+ message: options?.message || `The value should start with ${value}`
416
+ };
417
+ return this;
418
+ }
419
+ /**
420
+ * Checks if the string includes a specific substring.
421
+ *
422
+ * @example
423
+ * ```typescript
424
+ * import * as p from '@palmares/schema';
425
+ *
426
+ * const schema = p.string().includes('for babies');
427
+ *
428
+ * schema.parse('Computer graphics for babies'); // { errors: [], parsed: 'Computer graphics for babies' }
429
+ * 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' }
430
+ * ```
431
+ *
432
+ * @param value - The value that the string should include.
433
+ * @param options - The options for the includes function.
434
+ * @param options.message - The message to be shown when the value does not include the value.
435
+ *
436
+ * @returns - The schema instance.
437
+ */ includes(value, options) {
438
+ this.__includes = {
439
+ value,
440
+ message: options?.message || `The string value should include the following substring '${value}'`
441
+ };
442
+ return this;
443
+ }
444
+ /**
445
+ * Validates if the string matches a specific regex.
446
+ *
447
+ * @example
448
+ * ```typescript
449
+ * import * as p from '@palmares/schema';
450
+ *
451
+ * const schema = p.string().regex(/^[a-z]+$/);
452
+ *
453
+ * schema.parse('abc'); // { errors: [], parsed: 'abc' }
454
+ * schema.parse('123'); // { errors: [{ code: 'regex', message: 'The value should match the following regex /^[a-z]+$/', path: [] }], parsed: '123' }
455
+ * ```
456
+ *
457
+ * @param value - The regex that the string should match.
458
+ * @param options - The options for the regex function.
459
+ * @param options.message - The message to be shown when the value does not match the regex.
460
+ *
461
+ * @returns - The schema instance.
462
+ */ regex(value, options) {
463
+ this.__regex = {
464
+ value,
465
+ message: options?.message || `The value should match the following regex '${value.toString()}'`
466
+ };
467
+ return this;
468
+ }
469
+ /**
470
+ * Validates if the string has a maximum length. Use { inclusive: true } to allow the value to have the same length as the maximum length.
471
+ *
472
+ * @example
473
+ * ```typescript
474
+ * import * as p from '@palmares/schema';
475
+ *
476
+ * const schema = p.string().maxLength(5);
477
+ *
478
+ * schema.parse('12345'); // { errors: [], parsed: '12345' }
479
+ * schema.parse('123456'); // { errors: [{ code: 'maxLength', message: 'The value should have a maximum length of 5', path: [] }], parsed: '123
480
+ * ```
481
+ *
482
+ * @param value - The maximum length that the string should have.
483
+ * @param options - The options for the maxLength function.
484
+ * @param options.message - The message to be shown when the value has a length greater than the maximum length.
485
+ * @param options.inclusive - Whether the value can have the same length as the maximum length. Defaults to false.
486
+ *
487
+ * @returns - The schema instance.
488
+ */ maxLength(value, options) {
489
+ this.__maxLength = {
490
+ value,
491
+ message: options?.message || `The value should have a maximum length of ${value}`,
492
+ inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false
493
+ };
494
+ return this;
495
+ }
496
+ /**
497
+ * Validates if the string has a minimum length. Use { inclusive: true } to allow the value to have the same length as the minimum length.
498
+ *
499
+ * @example
500
+ * ```typescript
501
+ * import * as p from '@palmares/schema';
502
+ *
503
+ * const schema = p.string().minLength(5);
504
+ *
505
+ * schema.parse('12345'); // { errors: [], parsed: '12345' }
506
+ * schema.parse('1234'); // { errors: [{ code: 'minLength', message: 'The value should have a minimum length of 5', path: [] }], parsed: '1234' }
507
+ * ```
508
+ *
509
+ * @param value - The minimum length that the string should have.
510
+ * @param options - The options for the minLength function.
511
+ * @param options.message - The message to be shown when the value has a length less than the minimum length.
512
+ * @param options.inclusive - Whether the value can have the same length as the minimum length. Defaults to false.
513
+ *
514
+ * @returns - The schema instance.
515
+ */ minLength(value, options) {
516
+ this.__minLength = {
517
+ value,
518
+ message: options?.message || `The value should have a minimum length of ${value}`,
519
+ inclusive: typeof options?.inclusive === 'boolean' ? options.inclusive : false
520
+ };
521
+ return this;
522
+ }
523
+ /**
524
+ * Validates if the string is a valid UUID.
525
+ *
526
+ * @example
527
+ * ```typescript
528
+ * import * as p from '@palmares/schema';
529
+ *
530
+ * const schema = p.string().uuid();
531
+ *
532
+ * schema.parse('550e8400-e29b-41d4-a716-446655440000'); // { errors: [], parsed: '550e8400-e29b-41d4-a716-446655440000' }
533
+ * ```
534
+ *
535
+ * @param options - The options for the uuid function.
536
+ * @param options.message - The message to be shown when the value is not a valid UUID. Defaults to 'The value should be a valid UUID'.
537
+ *
538
+ * @returns - The schema instance.
539
+ */ uuid(options) {
540
+ this.__uuid = {
541
+ message: options?.message || 'The value should be a valid UUID'
542
+ };
543
+ return this;
544
+ }
545
+ /**
546
+ * Validates if the string is a valid email or not
547
+ *
548
+ * @example
549
+ * ```typescript
550
+ *
551
+ * import * as p from '@palmares/schema';
552
+ *
553
+ * const schema = p.string().email();
554
+ *
555
+ * schema.parse('john.doe@example.com'); // { errors: [], parsed: 'john.doe@example.com' }
556
+ * ```
557
+ *
558
+ * @param options - The options for the email function.
559
+ * @param options.message - The message to be shown when the value is not a valid email. Defaults to 'The value should be a valid email'.
560
+ *
561
+ * @returns - The schema instance.
562
+ */ email(options) {
563
+ this.__email = {
564
+ message: options?.message || 'The value should be a valid email'
565
+ };
566
+ return this;
567
+ }
568
+ static new() {
569
+ const returnValue = new StringSchema();
570
+ const adapterInstance = getDefaultAdapter();
571
+ returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
572
+ transformed: false,
573
+ adapter: adapterInstance,
574
+ schemas: []
575
+ };
576
+ return returnValue;
577
+ }
578
+ }
579
+ export const string = StringSchema.new;
@@ -0,0 +1 @@
1
+ export { };