@palmares/schemas 0.0.1 → 0.1.0

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 (174) hide show
  1. package/.turbo/turbo-build$colon$watch.log +12 -410
  2. package/CHANGELOG.md +17 -0
  3. package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +22 -0
  4. package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +156 -0
  5. package/__tests__/.drizzle/migrations/meta/_journal.json +13 -0
  6. package/__tests__/.drizzle/schema.ts +35 -0
  7. package/__tests__/drizzle.config.ts +11 -0
  8. package/__tests__/eslint.config.js +10 -0
  9. package/__tests__/manage.ts +5 -0
  10. package/__tests__/node_modules/.bin/drizzle-kit +17 -0
  11. package/__tests__/node_modules/.bin/esbuild +14 -0
  12. package/__tests__/node_modules/.bin/tsc +17 -0
  13. package/__tests__/node_modules/.bin/tsserver +17 -0
  14. package/__tests__/node_modules/.bin/tsx +17 -0
  15. package/__tests__/package.json +36 -0
  16. package/__tests__/sqlite.db +0 -0
  17. package/__tests__/src/core/array.test.ts +130 -0
  18. package/__tests__/src/core/boolean.test.ts +66 -0
  19. package/__tests__/src/core/datetime.test.ts +102 -0
  20. package/__tests__/src/core/index.ts +35 -0
  21. package/__tests__/src/core/model.test.ts +260 -0
  22. package/__tests__/src/core/models.ts +50 -0
  23. package/__tests__/src/core/numbers.test.ts +177 -0
  24. package/__tests__/src/core/object.test.ts +198 -0
  25. package/__tests__/src/core/string.test.ts +222 -0
  26. package/__tests__/src/core/test.test.ts +59 -0
  27. package/__tests__/src/core/types.test.ts +97 -0
  28. package/__tests__/src/core/union.test.ts +99 -0
  29. package/__tests__/src/settings.ts +71 -0
  30. package/__tests__/tsconfig.json +11 -0
  31. package/dist/cjs/src/adapter/fields/index.js +2 -2
  32. package/dist/cjs/src/adapter/fields/object.js +9 -0
  33. package/dist/cjs/src/adapter/index.js +1 -0
  34. package/dist/cjs/src/constants.js +1 -7
  35. package/dist/cjs/src/domain.js +146 -1
  36. package/dist/cjs/src/index.js +69 -74
  37. package/dist/cjs/src/model.js +206 -206
  38. package/dist/cjs/src/schema/array.js +185 -58
  39. package/dist/cjs/src/schema/boolean.js +105 -44
  40. package/dist/cjs/src/schema/datetime.js +104 -38
  41. package/dist/cjs/src/schema/number.js +134 -114
  42. package/dist/cjs/src/schema/object.js +106 -43
  43. package/dist/cjs/src/schema/schema.js +123 -75
  44. package/dist/cjs/src/schema/string.js +152 -58
  45. package/dist/cjs/src/schema/union.js +412 -290
  46. package/dist/cjs/src/utils.js +42 -15
  47. package/dist/cjs/src/validators/array.js +6 -1
  48. package/dist/cjs/src/validators/boolean.js +2 -0
  49. package/dist/cjs/src/validators/datetime.js +4 -0
  50. package/dist/cjs/src/validators/number.js +12 -40
  51. package/dist/cjs/src/validators/object.js +1 -0
  52. package/dist/cjs/src/validators/schema.js +5 -1
  53. package/dist/cjs/src/validators/string.js +30 -2
  54. package/dist/cjs/src/validators/union.js +5 -4
  55. package/dist/cjs/src/validators/utils.js +99 -27
  56. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
  57. package/dist/cjs/types/adapter/fields/array.d.ts +2 -2
  58. package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -1
  59. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -1
  60. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -1
  61. package/dist/cjs/types/adapter/fields/index.d.ts +2 -2
  62. package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -1
  63. package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -1
  64. package/dist/cjs/types/adapter/fields/object.d.ts +2 -1
  65. package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -1
  66. package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -1
  67. package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -1
  68. package/dist/cjs/types/adapter/index.d.ts +1 -0
  69. package/dist/cjs/types/adapter/index.d.ts.map +1 -1
  70. package/dist/cjs/types/adapter/types.d.ts +28 -18
  71. package/dist/cjs/types/adapter/types.d.ts.map +1 -1
  72. package/dist/cjs/types/constants.d.ts +0 -1
  73. package/dist/cjs/types/constants.d.ts.map +1 -1
  74. package/dist/cjs/types/domain.d.ts +5 -4
  75. package/dist/cjs/types/domain.d.ts.map +1 -1
  76. package/dist/cjs/types/index.d.ts +78 -55
  77. package/dist/cjs/types/index.d.ts.map +1 -1
  78. package/dist/cjs/types/model.d.ts +17 -17
  79. package/dist/cjs/types/model.d.ts.map +1 -1
  80. package/dist/cjs/types/schema/array.d.ts +168 -47
  81. package/dist/cjs/types/schema/array.d.ts.map +1 -1
  82. package/dist/cjs/types/schema/boolean.d.ts +103 -44
  83. package/dist/cjs/types/schema/boolean.d.ts.map +1 -1
  84. package/dist/cjs/types/schema/datetime.d.ts +90 -30
  85. package/dist/cjs/types/schema/datetime.d.ts.map +1 -1
  86. package/dist/cjs/types/schema/number.d.ts +133 -125
  87. package/dist/cjs/types/schema/number.d.ts.map +1 -1
  88. package/dist/cjs/types/schema/object.d.ts +104 -35
  89. package/dist/cjs/types/schema/object.d.ts.map +1 -1
  90. package/dist/cjs/types/schema/schema.d.ts +62 -44
  91. package/dist/cjs/types/schema/schema.d.ts.map +1 -1
  92. package/dist/cjs/types/schema/string.d.ts +152 -65
  93. package/dist/cjs/types/schema/string.d.ts.map +1 -1
  94. package/dist/cjs/types/schema/types.d.ts +11 -2
  95. package/dist/cjs/types/schema/types.d.ts.map +1 -1
  96. package/dist/cjs/types/schema/union.d.ts +133 -40
  97. package/dist/cjs/types/schema/union.d.ts.map +1 -1
  98. package/dist/cjs/types/types.d.ts +35 -0
  99. package/dist/cjs/types/types.d.ts.map +1 -1
  100. package/dist/cjs/types/utils.d.ts +41 -27
  101. package/dist/cjs/types/utils.d.ts.map +1 -1
  102. package/dist/cjs/types/validators/array.d.ts.map +1 -1
  103. package/dist/cjs/types/validators/boolean.d.ts.map +1 -1
  104. package/dist/cjs/types/validators/datetime.d.ts.map +1 -1
  105. package/dist/cjs/types/validators/number.d.ts +5 -6
  106. package/dist/cjs/types/validators/number.d.ts.map +1 -1
  107. package/dist/cjs/types/validators/object.d.ts.map +1 -1
  108. package/dist/cjs/types/validators/schema.d.ts +2 -2
  109. package/dist/cjs/types/validators/schema.d.ts.map +1 -1
  110. package/dist/cjs/types/validators/string.d.ts +9 -9
  111. package/dist/cjs/types/validators/string.d.ts.map +1 -1
  112. package/dist/cjs/types/validators/utils.d.ts +44 -27
  113. package/dist/cjs/types/validators/utils.d.ts.map +1 -1
  114. package/dist/esm/src/adapter/fields/index.js +2 -2
  115. package/dist/esm/src/adapter/fields/object.js +6 -0
  116. package/dist/esm/src/adapter/index.js +1 -0
  117. package/dist/esm/src/constants.js +1 -2
  118. package/dist/esm/src/domain.js +11 -1
  119. package/dist/esm/src/index.js +38 -73
  120. package/dist/esm/src/model.js +83 -78
  121. package/dist/esm/src/schema/array.js +136 -54
  122. package/dist/esm/src/schema/boolean.js +98 -44
  123. package/dist/esm/src/schema/datetime.js +91 -38
  124. package/dist/esm/src/schema/number.js +127 -110
  125. package/dist/esm/src/schema/object.js +98 -43
  126. package/dist/esm/src/schema/schema.js +102 -67
  127. package/dist/esm/src/schema/string.js +147 -59
  128. package/dist/esm/src/schema/union.js +119 -40
  129. package/dist/esm/src/types.js +14 -1
  130. package/dist/esm/src/utils.js +56 -27
  131. package/dist/esm/src/validators/array.js +6 -1
  132. package/dist/esm/src/validators/boolean.js +2 -0
  133. package/dist/esm/src/validators/datetime.js +4 -0
  134. package/dist/esm/src/validators/number.js +9 -23
  135. package/dist/esm/src/validators/object.js +1 -0
  136. package/dist/esm/src/validators/schema.js +5 -1
  137. package/dist/esm/src/validators/string.js +30 -2
  138. package/dist/esm/src/validators/union.js +5 -4
  139. package/dist/esm/src/validators/utils.js +62 -36
  140. package/package.json +3 -3
  141. package/src/adapter/fields/array.ts +2 -2
  142. package/src/adapter/fields/boolean.ts +3 -8
  143. package/src/adapter/fields/datetime.ts +3 -9
  144. package/src/adapter/fields/index.ts +11 -11
  145. package/src/adapter/fields/number.ts +3 -9
  146. package/src/adapter/fields/object.ts +13 -10
  147. package/src/adapter/fields/string.ts +3 -9
  148. package/src/adapter/fields/union.ts +3 -9
  149. package/src/adapter/index.ts +1 -0
  150. package/src/adapter/types.ts +60 -45
  151. package/src/constants.ts +1 -3
  152. package/src/domain.ts +15 -1
  153. package/src/index.ts +189 -211
  154. package/src/model.ts +119 -115
  155. package/src/schema/array.ts +274 -90
  156. package/src/schema/boolean.ts +145 -60
  157. package/src/schema/datetime.ts +133 -49
  158. package/src/schema/number.ts +210 -173
  159. package/src/schema/object.ts +167 -74
  160. package/src/schema/schema.ts +205 -126
  161. package/src/schema/string.ts +221 -94
  162. package/src/schema/types.ts +44 -16
  163. package/src/schema/union.ts +193 -68
  164. package/src/types.ts +53 -0
  165. package/src/utils.ts +115 -57
  166. package/src/validators/array.ts +46 -27
  167. package/src/validators/boolean.ts +13 -7
  168. package/src/validators/datetime.ts +24 -16
  169. package/src/validators/number.ts +53 -63
  170. package/src/validators/object.ts +6 -5
  171. package/src/validators/schema.ts +33 -25
  172. package/src/validators/string.ts +122 -59
  173. package/src/validators/union.ts +8 -8
  174. package/src/validators/utils.ts +67 -42
@@ -1,5 +1,4 @@
1
1
  import Schema from './schema';
2
- import { getDefaultAdapter } from '../conf';
3
2
  import { defaultTransform, defaultTransformToAdapter } from '../utils';
4
3
  import { above, allowStringParser, below, datetimeValidation } from '../validators/datetime';
5
4
  import { nullable, optional } from '../validators/schema';
@@ -7,6 +6,17 @@ export default class DatetimeSchema extends Schema {
7
6
  __allowString;
8
7
  __above;
9
8
  __below;
9
+ __type = {
10
+ message: 'Invalid type',
11
+ check: (value)=>{
12
+ if (typeof value === 'string') {
13
+ const valueAsDate = new Date(value);
14
+ if (isNaN(valueAsDate.getTime())) return false;
15
+ return true;
16
+ }
17
+ return value instanceof Date;
18
+ }
19
+ };
10
20
  async __transformToAdapter(options) {
11
21
  return defaultTransformToAdapter(async (adapter)=>{
12
22
  return defaultTransform('datetime', this, adapter, adapter.datetime, ()=>({
@@ -15,6 +25,7 @@ export default class DatetimeSchema extends Schema {
15
25
  above: this.__above,
16
26
  nullable: this.__nullable,
17
27
  optional: this.__optional,
28
+ type: this.__type,
18
29
  parsers: {
19
30
  nullable: this.__nullable.allow,
20
31
  optional: this.__optional.allow
@@ -31,10 +42,11 @@ export default class DatetimeSchema extends Schema {
31
42
  // eslint-disable-next-line ts/require-await
32
43
  fallbackIfNotSupported: async ()=>[]
33
44
  });
34
- }, this.__transformedSchemas, options, 'datetime');
45
+ }, this, this.__transformedSchemas, options, 'datetime');
35
46
  }
36
47
  /**
37
- * This let's you refine the schema with custom validations. This is useful when you want to validate something that is not supported by default by the schema adapter.
48
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something
49
+ * that is not supported by default by the schema adapter.
38
50
  *
39
51
  * @example
40
52
  * ```typescript
@@ -46,7 +58,8 @@ export default class DatetimeSchema extends Schema {
46
58
  *
47
59
  * const { errors, parsed } = await numberSchema.parse(-1);
48
60
  *
49
- * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
61
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
62
+ * console.log(errors);
50
63
  * ```
51
64
  *
52
65
  * @param refinementCallback - The callback that will be called to validate the value.
@@ -84,8 +97,25 @@ export default class DatetimeSchema extends Schema {
84
97
  return super.optional(options);
85
98
  }
86
99
  /**
87
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value is NULL by setting
88
- * the { message: 'Your custom message', allow: false } on the options.
100
+ * Just adds a message when the value is undefined. It's just a syntax sugar for
101
+ *
102
+ * ```typescript
103
+ * p.datetime().optional({ message: 'This value should be defined', allow: false })
104
+ * ```
105
+ *
106
+ * @param options - The options of nonOptional function
107
+ * @param options.message - A custom message if the value is undefined.
108
+ *
109
+ * @returns - The schema.
110
+ */ nonOptional(options) {
111
+ return super.optional({
112
+ message: options?.message,
113
+ allow: false
114
+ });
115
+ }
116
+ /**
117
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
118
+ * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
89
119
  *
90
120
  * @example
91
121
  * ```typescript
@@ -111,14 +141,32 @@ export default class DatetimeSchema extends Schema {
111
141
  return super.nullable(options);
112
142
  }
113
143
  /**
114
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
115
- * otherwise it will set the value to undefined after it's validated.
144
+ * Just adds a message when the value is null. It's just a syntax sugar for
145
+ *
146
+ * ```typescript
147
+ * p.datetime().nullable({ message: 'This value cannot be null', allow: false })
148
+ * ```
149
+ *
150
+ * @param options - The options of nonNullable function
151
+ * @param options.message - A custom message if the value is null.
152
+ *
153
+ * @returns - The schema.
154
+ */ nonNullable(options) {
155
+ return super.nullable({
156
+ message: options?.message || '',
157
+ allow: false
158
+ });
159
+ }
160
+ /**
161
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
162
+ * that way otherwise it will set the value to undefined after it's validated.
116
163
  * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
117
164
  * function. This will remove the value from the representation of the schema.
118
165
  *
119
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
120
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
121
- * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
166
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
167
+ * function.But if you want to remove the value from the internal representation, you can pass the argument
168
+ * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
169
+ * the argument `toRepresentation` as true as well.
122
170
  *
123
171
  * @example
124
172
  * ```typescript
@@ -140,18 +188,20 @@ export default class DatetimeSchema extends Schema {
140
188
  * ```
141
189
  *
142
190
  *
143
- * @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
144
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
145
- * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
191
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
192
+ * the {@link data} function.
193
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as
194
+ * true. Then if you still want to remove the value from the representation, you will need to pass the argument
195
+ * `toRepresentation` as true as well.
146
196
  *
147
197
  * @returns The schema.
148
198
  */ omit(args) {
149
199
  return super.omit(args);
150
200
  }
151
201
  /**
152
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
153
- * like a database. You should always return the schema after you save the value, that way we will always have the correct type
154
- * of the schema after the save operation.
202
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
203
+ * source like a database. You should always return the schema after you save the value, that way we will always have
204
+ * the correct type of the schema after the save operation.
155
205
  *
156
206
  * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
157
207
  *
@@ -193,7 +243,8 @@ export default class DatetimeSchema extends Schema {
193
243
  return super.onSave(callback);
194
244
  }
195
245
  /**
196
- * This function is used to add a default value to the schema. If the value is either undefined or null, the default value will be used.
246
+ * This function is used to add a default value to the schema. If the value is either undefined or null, the default
247
+ * value will be used.
197
248
  *
198
249
  * @example
199
250
  * ```typescript
@@ -209,8 +260,9 @@ export default class DatetimeSchema extends Schema {
209
260
  return super.default(defaultValueOrFunction);
210
261
  }
211
262
  /**
212
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call this function to let you customize
213
- * the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
263
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
264
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
265
+ * No problem, you can use this function to customize the zod schema.
214
266
  *
215
267
  * @example
216
268
  * ```typescript
@@ -222,20 +274,22 @@ export default class DatetimeSchema extends Schema {
222
274
  *
223
275
  * const { errors, parsed } = await numberSchema.parse(-1);
224
276
  *
225
- * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
277
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
278
+ * console.log(errors);
226
279
  * ```
227
280
  *
228
281
  * @param callback - The callback that will be called to customize the schema.
229
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to compile the underlying schema
230
- * to a string so you can save it for future runs.
282
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to
283
+ * compile the underlying schema to a string so you can save it for future runs.
231
284
  *
232
285
  * @returns The schema.
233
286
  */ extends(callback, toStringCallback) {
234
287
  return super.extends(callback, toStringCallback);
235
288
  }
236
289
  /**
237
- * This function is used to transform the value to the representation of the schema. When using the {@link data} function. With this function you have full
238
- * control to add data cleaning for example, transforming the data and whatever. Another use case is when you want to return deeply nested recursive data.
290
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
291
+ * function. With this function you have full control to add data cleaning for example, transforming the data and
292
+ * whatever. Another use case is when you want to return deeply nested recursive data.
239
293
  * The schema maps to itself.
240
294
  *
241
295
  * @example
@@ -279,8 +333,9 @@ export default class DatetimeSchema extends Schema {
279
333
  return super.toRepresentation(toRepresentationCallback);
280
334
  }
281
335
  /**
282
- * This function is used to transform the value to the internal representation of the schema. This is useful when you want to transform the value
283
- * to a type that the schema adapter can understand. For example, you might want to transform a string to a date. This is the function you use.
336
+ * This function is used to transform the value to the internal representation of the schema. This is useful when you
337
+ * want to transform the value to a type that the schema adapter can understand. For example, you might want to
338
+ * transform a string to a date. This is the function you use.
284
339
  *
285
340
  * @example
286
341
  * ```typescript
@@ -313,8 +368,9 @@ export default class DatetimeSchema extends Schema {
313
368
  return super.toInternal(toInternalCallback);
314
369
  }
315
370
  /**
316
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string, you can convert that string to a date
317
- * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
371
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
372
+ * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
373
+ * that the schema adapter can understand.
318
374
  *
319
375
  * @example
320
376
  * ```
@@ -335,7 +391,8 @@ export default class DatetimeSchema extends Schema {
335
391
  return super.toValidate(toValidateCallback);
336
392
  }
337
393
  /**
338
- * This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the result to be a string as well.
394
+ * This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
395
+ * result to be a string as well.
339
396
  *
340
397
  * @example
341
398
  * ```typescript
@@ -365,7 +422,8 @@ export default class DatetimeSchema extends Schema {
365
422
  *
366
423
  * @param value - The value that we are comparing against.
367
424
  * @param options - The options that we are passing to the validator.
368
- * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to the specified date.
425
+ * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to
426
+ * the specified date.
369
427
  * @param options.message - The message that we are returning if the value is not above the specified date.
370
428
  *
371
429
  * @returns - The schema instance
@@ -395,7 +453,8 @@ export default class DatetimeSchema extends Schema {
395
453
  *
396
454
  * @param value - The value that we are comparing against.
397
455
  * @param options - The options that we are passing to the validator.
398
- * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to the specified date.
456
+ * @param options.inclusive - If the value is inclusive or not. In other words, if the value can be equal to the
457
+ * specified date.
399
458
  * @param options.message - The message that we are returning if the value is not above the specified date.
400
459
  *
401
460
  * @returns - The schema instance
@@ -411,12 +470,6 @@ export default class DatetimeSchema extends Schema {
411
470
  }
412
471
  static new() {
413
472
  const returnValue = new DatetimeSchema();
414
- const adapterInstance = getDefaultAdapter();
415
- returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
416
- transformed: false,
417
- adapter: adapterInstance,
418
- schemas: []
419
- };
420
473
  return returnValue;
421
474
  }
422
475
  }
@@ -1,37 +1,51 @@
1
1
  import Schema from './schema';
2
- import { getDefaultAdapter } from '../conf';
3
- import { DEFAULT_NUMBER_INTEGER_EXCEPTION, DEFAULT_NUMBER_MAX_EXCEPTION, DEFAULT_NUMBER_MIN_EXCEPTION, DEFAULT_NUMBER_NEGATIVE_EXCEPTION } from '../constants';
2
+ import { DEFAULT_NUMBER_INTEGER_EXCEPTION, DEFAULT_NUMBER_MAX_EXCEPTION, DEFAULT_NUMBER_MIN_EXCEPTION } from '../constants';
3
+ import { convertFromStringBuilder } from '../parsers';
4
4
  import { defaultTransform, defaultTransformToAdapter } from '../utils';
5
- import { max, min, numberValidation } from '../validators/number';
5
+ import { decimalPlaces, integer, max, maxDigits, min, numberValidation } from '../validators/number';
6
+ import { is, nullable, optional } from '../validators/schema';
6
7
  export default class NumberSchema extends Schema {
8
+ __allowString;
7
9
  __is;
8
10
  __integer;
9
11
  __maxDigits;
10
12
  __decimalPlaces;
11
13
  __max;
12
14
  __min;
13
- __allowNegative;
14
- __allowPositive;
15
+ __type = {
16
+ message: 'Invalid type',
17
+ check: (value)=>{
18
+ const isNumber = new RegExp('^-?\\d*(\\.\\d+)?$').test(value);
19
+ if (typeof value === 'string' && this.__allowString && isNumber) return true;
20
+ return typeof value === 'number';
21
+ }
22
+ };
15
23
  async __transformToAdapter(options) {
16
24
  return defaultTransformToAdapter(async (adapter)=>{
17
25
  return defaultTransform('number', this, adapter, adapter.number, ()=>({
18
26
  is: this.__is,
19
27
  min: this.__min,
20
- allowNegative: this.__allowNegative,
21
- allowPositive: this.__allowPositive,
22
28
  max: this.__max,
23
29
  integer: this.__integer,
24
30
  optional: this.__optional,
25
31
  nullable: this.__nullable,
26
32
  maxDigits: this.__maxDigits,
27
33
  decimalPlaces: this.__decimalPlaces,
34
+ type: this.__type,
28
35
  parsers: {
36
+ allowString: this.__allowString,
29
37
  nullable: this.__nullable.allow,
30
38
  optional: this.__optional.allow
31
39
  }
32
40
  }), {
33
41
  max,
34
- min
42
+ min,
43
+ maxDigits: maxDigits,
44
+ is: is,
45
+ optional: optional,
46
+ nullable: nullable,
47
+ decimalPlaces: decimalPlaces,
48
+ integer: integer
35
49
  }, {
36
50
  validatorsIfFallbackOrNotSupported: numberValidation(),
37
51
  shouldAddStringVersion: options.shouldAddStringVersion,
@@ -40,10 +54,11 @@ export default class NumberSchema extends Schema {
40
54
  return [];
41
55
  }
42
56
  });
43
- }, this.__transformedSchemas, options, 'number');
57
+ }, this, this.__transformedSchemas, options, 'number');
44
58
  }
45
59
  /**
46
- * 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.
60
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something
61
+ * that is not supported by default by the schema adapter.
47
62
  *
48
63
  * @example
49
64
  * ```typescript
@@ -55,7 +70,8 @@ export default class NumberSchema extends Schema {
55
70
  *
56
71
  * const { errors, parsed } = await numberSchema.parse(-1);
57
72
  *
58
- * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
73
+ * console.log(errors);
74
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
59
75
  * ```
60
76
  *
61
77
  * @param refinementCallback - The callback that will be called to validate the value.
@@ -93,8 +109,25 @@ export default class NumberSchema extends Schema {
93
109
  return super.optional(options);
94
110
  }
95
111
  /**
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.
112
+ * Just adds a message when the value is undefined. It's just a syntax sugar for
113
+ *
114
+ * ```typescript
115
+ * p.string().optional({ message: 'This value cannot be null', allow: false })
116
+ * ```
117
+ *
118
+ * @param options - The options of nonOptional function
119
+ * @param options.message - A custom message if the value is undefined.
120
+ *
121
+ * @returns - The schema.
122
+ */ nonOptional(options) {
123
+ return super.optional({
124
+ message: options?.message,
125
+ allow: false
126
+ });
127
+ }
128
+ /**
129
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
130
+ * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
98
131
  *
99
132
  * @example
100
133
  * ```typescript
@@ -120,14 +153,33 @@ export default class NumberSchema extends Schema {
120
153
  return super.nullable(options);
121
154
  }
122
155
  /**
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.
156
+ * Just adds a message when the value is null. It's just a syntax sugar for
157
+ *
158
+ * ```typescript
159
+ * p.string().nullable({ message: 'This value cannot be null', allow: false })
160
+ * ```
161
+ *
162
+ * @param options - The options of nonNullable function
163
+ * @param options.message - A custom message if the value is null.
164
+ *
165
+ * @returns - The schema.
166
+ */ nonNullable(options) {
167
+ return super.nullable({
168
+ message: options?.message || '',
169
+ allow: false
170
+ });
171
+ }
172
+ /**
173
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
174
+ * that way otherwise it will set the value to undefined after it's validated.
125
175
  * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
126
176
  * function. This will remove the value from the representation of the schema.
127
177
  *
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.
178
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
179
+ * function.
180
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
181
+ * as true. Then if you still want to remove the value from the representation, you will need to pass the argument
182
+ * `toRepresentation` as true as well.
131
183
  *
132
184
  * @example
133
185
  * ```typescript
@@ -149,18 +201,40 @@ export default class NumberSchema extends Schema {
149
201
  * ```
150
202
  *
151
203
  *
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.
204
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
205
+ * the {@link data} function.
206
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
207
+ * as true. Then if you still want to remove the value from the representation, you will need to pass the argument
208
+ * `toRepresentation` as true as well.
155
209
  *
156
210
  * @returns The schema.
157
211
  */ omit(args) {
158
212
  return super.omit(args);
159
213
  }
160
214
  /**
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.
215
+ * This will allow the value to be a string, it does not validate, it just parses inputs as strings and allows the
216
+ * result to be a string as well.
217
+ *
218
+ * @example
219
+ * ```ts
220
+ * number().allowString().parse('true') // true
221
+ * ```
222
+ *
223
+ * @returns - The schema instance
224
+ */ allowString() {
225
+ this.__allowString = true;
226
+ this.__parsers.low.set('allowString', convertFromStringBuilder((value)=>{
227
+ return {
228
+ value: Number(value),
229
+ preventNextParsers: false
230
+ };
231
+ }));
232
+ return this;
233
+ }
234
+ /**
235
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
236
+ * source like a database. You should always return the schema after you save the value, that way we will always
237
+ * have the correct type of the schema after the save operation.
164
238
  *
165
239
  * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
166
240
  *
@@ -202,7 +276,8 @@ export default class NumberSchema extends Schema {
202
276
  return super.onSave(callback);
203
277
  }
204
278
  /**
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.
279
+ * This function is used to add a default value to the schema. If the value is either undefined or null, the default
280
+ * value will be used.
206
281
  *
207
282
  * @example
208
283
  * ```typescript
@@ -218,8 +293,9 @@ export default class NumberSchema extends Schema {
218
293
  return super.default(defaultValueOrFunction);
219
294
  }
220
295
  /**
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.
296
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
297
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
298
+ * No problem, you can use this function to customize the zod schema.
223
299
  *
224
300
  * @example
225
301
  * ```typescript
@@ -231,20 +307,22 @@ export default class NumberSchema extends Schema {
231
307
  *
232
308
  * const { errors, parsed } = await numberSchema.parse(-1);
233
309
  *
234
- * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
310
+ * console.log(errors);
311
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
235
312
  * ```
236
313
  *
237
314
  * @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.
315
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you
316
+ * want to compile the underlying schema to a string so you can save it for future runs.
240
317
  *
241
318
  * @returns The schema.
242
319
  */ extends(callback, toStringCallback) {
243
320
  return super.extends(callback, toStringCallback);
244
321
  }
245
322
  /**
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.
323
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
324
+ * function. With this function you have full control to add data cleaning for example, transforming the data and
325
+ * whatever. Another use case is when you want to return deeply nested recursive data.
248
326
  * The schema maps to itself.
249
327
  *
250
328
  * @example
@@ -288,8 +366,9 @@ export default class NumberSchema extends Schema {
288
366
  return super.toRepresentation(toRepresentationCallback);
289
367
  }
290
368
  /**
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.
369
+ * This function is used to transform the value to the internal representation of the schema. This is useful when
370
+ * you want to transform the value to a type that the schema adapter can understand. For example, you might want
371
+ * to transform a string to a date. This is the function you use.
293
372
  *
294
373
  * @example
295
374
  * ```typescript
@@ -322,7 +401,8 @@ export default class NumberSchema extends Schema {
322
401
  return super.toInternal(toInternalCallback);
323
402
  }
324
403
  /**
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
404
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
405
+ * you can convert that string to a date
326
406
  * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
327
407
  *
328
408
  * @example
@@ -362,15 +442,16 @@ export default class NumberSchema extends Schema {
362
442
  * @param value - The list of numbers that are allowed
363
443
  *
364
444
  * @returns - The schema instance
365
- */ is(value) {
445
+ */ is(value, options) {
366
446
  this.__is = {
367
447
  value,
368
- message: `The value should be equal to ${value.join(',')}`
448
+ message: typeof options?.message === 'string' ? options.message : `The value should be equal to ${value.join(',')}`
369
449
  };
370
450
  return this;
371
451
  }
372
452
  /**
373
- * Allows only numbers that are less than the value passed. If you want to allow the number to be equal to the value, you can pass the option `inclusive` as `true`.
453
+ * Allows only numbers that are less than the value passed. If you want to allow the number to be equal to the value,
454
+ * you can pass the option `inclusive` as `true`.
374
455
  * Otherwise, it will only allow numbers less than the value.
375
456
  *
376
457
  * @example
@@ -404,7 +485,8 @@ export default class NumberSchema extends Schema {
404
485
  return this;
405
486
  }
406
487
  /**
407
- * This method will validate if the number is greater than the value passed. If you want to allow the number to be equal to the value, you can pass the option `inclusive` as `true`.
488
+ * This method will validate if the number is greater than the value passed. If you want to allow the number to be
489
+ * equal to the value, you can pass the option `inclusive` as `true`.
408
490
  * Otherwise, it will only allow numbers greater than the value.
409
491
  *
410
492
  * @example
@@ -438,68 +520,8 @@ export default class NumberSchema extends Schema {
438
520
  return this;
439
521
  }
440
522
  /**
441
- * Allows only negative numbers. If you want to allow zero, you can pass the option `allowZero` as `true`. Otherwise, it will only allow negative numbers.
442
- *
443
- * @example
444
- * ```typescript
445
- * import * as p from '@palmares/schema';
446
- *
447
- * const schema = p.number().negative();
448
- *
449
- * schema.parse(-10); // { errors: [], parsed: -10 }
450
- * schema.parse(0); // { errors: [{ code: 'negative', message: 'The number should be negative' }], parsed: 0 }
451
- *
452
- * const schema = p.number().negative({ allowZero: true });
453
- *
454
- * schema.parse(0); // { errors: [], parsed: 0 }
455
- * ```
456
- *
457
- * @param options - The options to be passed to the validation
458
- * @param options.allowZero - If you want to allow zero, you can pass this option as `true`. Otherwise, it will only allow negative numbers.
459
- * @param options.message - The message to be returned if the validation fails
460
- *
461
- * @returns - The schema instance
462
- */ negative(options) {
463
- const allowZero = typeof options?.allowZero === 'boolean' ? options.allowZero : true;
464
- const message = typeof options?.message === 'string' ? options.message : DEFAULT_NUMBER_NEGATIVE_EXCEPTION(allowZero);
465
- this.__allowNegative = {
466
- allowZero,
467
- message
468
- };
469
- return this;
470
- }
471
- /**
472
- * Allows only positive numbers. If you want to allow zero, you can pass the option `allowZero` as `true`. Otherwise, it will only allow positive numbers greater than zero.
473
- *
474
- * @example
475
- * ```typescript
476
- * import * as p from '@palmares/schema';
477
- *
478
- * const schema = p.number().positive();
479
- *
480
- * schema.parse(10); // { errors: [], parsed: 10 }
481
- * schema.parse(0); // { errors: [{ code: 'positive', message: 'The number should be positive' }], parsed: 0 }
482
- *
483
- * const schema = p.number().positive({ allowZero: true });
484
- * schema.parse(0); // { errors: [], parsed: 0 }
485
- * ```
486
- *
487
- * @param options - The options to be passed to the validation
488
- * @param options.allowZero - If you want to allow zero, you can pass this option as `true`. Otherwise, it will only allow positive numbers greater than zero.
489
- * @param options.message - The message to be returned if the validation fails
490
- *
491
- * @returns - The schema instance
492
- */ positive(options) {
493
- const allowZero = typeof options?.allowZero === 'boolean' ? options.allowZero : true;
494
- const message = typeof options?.message === 'string' ? options.message : DEFAULT_NUMBER_NEGATIVE_EXCEPTION(allowZero);
495
- this.__allowPositive = {
496
- allowZero,
497
- message
498
- };
499
- return this;
500
- }
501
- /**
502
- * This method will validate the number to have the exact number of decimal places. It's usually useful for decimal numbers like currencies.
523
+ * This method will validate the number to have the exact number of decimal places. It's usually useful for decimal
524
+ * numbers like currencies.
503
525
  *
504
526
  * @example
505
527
  * ```
@@ -507,7 +529,8 @@ export default class NumberSchema extends Schema {
507
529
  *
508
530
  * schema.parse(10.00); // { errors: [], parsed: 10.00}
509
531
  *
510
- * schema.parse(10.000); // { errors: [{ code: 'decimal_places', message: 'The number should have 2 decimal places' }], parsed: 10.000}
532
+ * schema.parse(10.000);
533
+ * // { errors: [{ code: 'decimal_places', message: 'The number should have 2 decimal places' }], parsed: 10.000}
511
534
  * ```
512
535
  *
513
536
  * @param value - The number of decimal places.
@@ -524,8 +547,8 @@ export default class NumberSchema extends Schema {
524
547
  return this;
525
548
  }
526
549
  /**
527
- * This method will validate the number to have at most the number of digits specified. If used in conjunction with {@link decimalPlaces}, this number should be bigger than the
528
- * value of the decimal places.
550
+ * This method will validate the number to have at most the number of digits specified. If used in conjunction with
551
+ * {@link decimalPlaces}, this number should be bigger than the value of the decimal places.
529
552
  *
530
553
  * Think about that
531
554
  *
@@ -580,12 +603,6 @@ export default class NumberSchema extends Schema {
580
603
  }
581
604
  static new() {
582
605
  const returnValue = new NumberSchema();
583
- const adapterInstance = getDefaultAdapter();
584
- returnValue.__transformedSchemas[adapterInstance.constructor.name] = {
585
- transformed: false,
586
- adapter: adapterInstance,
587
- schemas: []
588
- };
589
606
  return returnValue;
590
607
  }
591
608
  }