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