@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,11 +1,15 @@
1
1
  import { getDefaultAdapter } from '../conf';
2
2
  import { formatErrorFromParseMethod } from '../utils';
3
3
  export default class Schema {
4
- // Those functions will assume control of the validation process on adapters, instead of the schema. Why this is used? The idea is that the Schema has NO idea
5
- // that one of it's children might be an UnionSchema for example. The adapter might not support unions, so then we give control to the union. The parent schema
6
- // Will already have an array of translated adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number and one for the value as String.
7
- // Of course this gets multiplied. So if we have a union with Number and String. We should take those two schemas from the array and validate them individually. This logic is
8
- // handled by the union schema. If we have an intersection type for example, instead of validating One schema OR the other, we validate one schema AND the other. This will be handled
4
+ // Those functions will assume control of the validation process on adapters, instead of the schema.
5
+ // Why this is used? The idea is that the Schema has NO idea
6
+ // that one of it's children might be an UnionSchema for example. The adapter might not support unions,
7
+ // so then we give control to the union. The parent schema will already have an array of translated
8
+ // adapter schemas. This means for a union with Number and String it'll generate two schemas, one for number
9
+ // and one for the value as String. Of course this gets multiplied. So if we have a union with Number and String.
10
+ // We should take those two schemas from the array and validate them individually. This logic is
11
+ // handled by the union schema. If we have an intersection type for example, instead of validating
12
+ // One schema OR the other, we validate one schema AND the other. This will be handled
9
13
  // by the schema that contains that intersection logic.
10
14
  __beforeValidationCallbacks = new Map();
11
15
  __cachedGetParent;
@@ -15,7 +19,7 @@ export default class Schema {
15
19
  get __getParent() {
16
20
  return this.__cachedGetParent;
17
21
  }
18
- __alreadyAppliedModel = false;
22
+ __alreadyAppliedModel;
19
23
  __runBeforeParseAndData;
20
24
  __rootFallbacksValidator;
21
25
  __saveCallback;
@@ -45,21 +49,33 @@ export default class Schema {
45
49
  message: 'Invalid type',
46
50
  check: ()=>true
47
51
  };
52
+ __getDefaultTransformedSchemas() {
53
+ const adapterInstance = getDefaultAdapter();
54
+ // eslint-disable-next-line ts/no-unnecessary-condition
55
+ if (this.__transformedSchemas[adapterInstance.constructor.name] === undefined) this.__transformedSchemas[adapterInstance.constructor.name] = {
56
+ transformed: false,
57
+ adapter: adapterInstance,
58
+ schemas: []
59
+ };
60
+ }
48
61
  /**
49
- * This will validate the data with the fallbacks, so internally, without relaying on the schema adapter. This is nice because we can support things that the schema adapter is not able to support by default.
62
+ * This will validate the data with the fallbacks, so internally, without relaying on the schema adapter.
63
+ * This is nice because we can support things that the schema adapter is not able to support by default.
50
64
  *
51
65
  * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors.
52
66
  * @param path - The path of the error.
53
67
  * @param parseResult - The result of the parse method.
54
68
  */ async __validateByFallbacks(path, parseResult, options) {
55
69
  // eslint-disable-next-line ts/no-unnecessary-condition
56
- if (this.__rootFallbacksValidator) return this.__rootFallbacksValidator.validate(options.errorsAsHashedSet || new Set(), path, parseResult, options);
70
+ if (this.__rootFallbacksValidator) return this.__rootFallbacksValidator.validate(options.errorsAsHashedSet, path, parseResult, options);
57
71
  return parseResult;
58
72
  }
59
73
  /**
60
- * This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate that data.
61
- * So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate from the adapter
62
- * we validate with the fallbacks so we can do all of the extra validations not handled by the adapter.
74
+ * This will validate by the adapter. In other words, we send the data to the schema adapter and then we validate
75
+ * that data.
76
+ * So understand that, first we send the data to the adapter, the adapter validates it, then, after we validate
77
+ * from the adapter we validate with the fallbacks so we can do all of the extra validations not handled by
78
+ * the adapter.
63
79
  *
64
80
  * @param value - The value to be validated.
65
81
  * @param errorsAsHashedSet - The errors as a hashed set. This is used to prevent duplicate errors on the validator.
@@ -80,11 +96,12 @@ export default class Schema {
80
96
  const adapterParseResult = await fieldAdapter.parse(adapter, adapter.field, schema.transformed, value, options.args);
81
97
  parseResult.parsed = adapterParseResult.parsed;
82
98
  if (adapterParseResult.errors) {
83
- if (Array.isArray(adapterParseResult.errors)) parseResult.errors = await Promise.all(adapterParseResult.errors.map(async (error)=>formatErrorFromParseMethod(adapter, fieldAdapter, error, path, options.errorsAsHashedSet || new Set())));
99
+ if (Array.isArray(adapterParseResult.errors)) parseResult.errors = await Promise.all(adapterParseResult.errors.map(async (error)=>formatErrorFromParseMethod(adapter, fieldAdapter, error, value, schema.transformed, path, options.errorsAsHashedSet || new Set())));
84
100
  else parseResult.errors = [
85
- await formatErrorFromParseMethod(adapter, fieldAdapter, parseResult.errors, path, options.errorsAsHashedSet || new Set())
101
+ await formatErrorFromParseMethod(adapter, fieldAdapter, parseResult.errors, value, schema.transformed, path, options.errorsAsHashedSet || new Set())
86
102
  ];
87
103
  }
104
+ parseResult.errors = parseResult.errors.filter((error)=>typeof error !== 'undefined');
88
105
  return parseResult;
89
106
  }
90
107
  // eslint-disable-next-line ts/require-await
@@ -121,11 +138,15 @@ export default class Schema {
121
138
  return value;
122
139
  }
123
140
  async __parse(value, path = [], options) {
141
+ this.__getDefaultTransformedSchemas();
124
142
  if (typeof this.__runBeforeParseAndData === 'function') await this.__runBeforeParseAndData(this);
125
- // This is used to run the toInternal command. If we didn't do this, we would need to parse through all of the schemas to run the toInternal command,
126
- // from the leafs (ObjectSchemas) to the root schema. This is not a good idea, so what we do is that during validation the leafs attach a function to
127
- // the options.toInternalToBubbleUp like `options.toInternalToBubbleUp.push(async () => (value[key] = await (schema as any).__toInternal(parsed)));``
128
- // This way, when the root schema finishes the validation, it will run all of the functions in the toInternalToBubbleUp array, modifying the parsed value.
143
+ // This is used to run the toInternal command. If we didn't do this, we would need to parse through all of
144
+ // the schemas to run the toInternal command, from the leafs (ObjectSchemas) to the root schema. This is not
145
+ // a good idea, so what we do is that during validation the leafs attach a function to the
146
+ // options.toInternalToBubbleUp like
147
+ // `options.toInternalToBubbleUp.push(async () => (value[key] = await (schema as any).__toInternal(parsed)));``
148
+ // This way, when the root schema finishes the validation, it will run all of the functions in the
149
+ // toInternalToBubbleUp array, modifying the parsed value.
129
150
  const shouldRunToInternalToBubbleUp = options.toInternalToBubbleUp === undefined;
130
151
  if (shouldRunToInternalToBubbleUp) options.toInternalToBubbleUp = [];
131
152
  if (options.errorsAsHashedSet instanceof Set === false) options.errorsAsHashedSet = new Set();
@@ -138,25 +159,31 @@ export default class Schema {
138
159
  errors: [],
139
160
  parsed: value
140
161
  };
141
- if (options.appendFallbacksBeforeAdapterValidation === undefined) options.appendFallbacksBeforeAdapterValidation = (name, callback)=>{
142
- this.__beforeValidationCallbacks.set(name, callback);
162
+ value = await this.__parsersToTransformValue(value, this.__parsers._fallbacks);
163
+ if (options.appendFallbacksBeforeAdapterValidation === undefined) options.appendFallbacksBeforeAdapterValidation = (schema, name, callback)=>{
164
+ // We just need this if the union adapter is net defined but the parent is not of the same type.
165
+ // For example, it's a union child of o object schema
166
+ if (this !== schema) this.__beforeValidationCallbacks.set(name, callback);
143
167
  };
144
168
  if (this.__transformedSchemas[options.schemaAdapter?.constructor.name || getDefaultAdapter().constructor.name].transformed === false) await this.__transformToAdapter(options);
145
- value = await this.__parsersToTransformValue(value, this.__parsers._fallbacks);
146
169
  const adapterToUse = options.schemaAdapter ? options.schemaAdapter : Object.values(this.__transformedSchemas)[0].adapter;
147
170
  const parsedResultsAfterFallbacks = await this.__validateByFallbacks(path, {
148
- errors: [],
171
+ errors: parseResult.errors,
149
172
  parsed: value
150
173
  }, options);
151
174
  parseResult.parsed = parsedResultsAfterFallbacks.parsed;
152
175
  // eslint-disable-next-line ts/no-unnecessary-condition
153
176
  parseResult.errors = (parseResult.errors || []).concat(parsedResultsAfterFallbacks.errors || []);
154
- // With this, the children takes control of validating by the adapter. For example on a union schema we want to validate all of the schemas and choose the one that has no errors.
177
+ // With this, the children takes control of validating by the adapter. For example on a union schema we
178
+ // want to validate all of the schemas and choose the one that has no errors.
155
179
  if (this.__beforeValidationCallbacks.size > 0) {
156
180
  for (const callback of this.__beforeValidationCallbacks.values()){
157
181
  const parsedValuesAfterValidationCallbacks = await callback(adapterToUse, adapterToUse[schemaAdapterFieldType], this, this.__transformedSchemas[adapterToUse.constructor.name].schemas, value, path, options);
158
182
  parseResult.parsed = parsedValuesAfterValidationCallbacks.parsed;
159
- parseResult.errors = parsedValuesAfterValidationCallbacks.errors;
183
+ parseResult.errors = Array.isArray(parseResult.errors) && Array.isArray(parsedValuesAfterValidationCallbacks.errors) ? [
184
+ ...parseResult.errors,
185
+ ...parsedValuesAfterValidationCallbacks.errors
186
+ ] : Array.isArray(parseResult.errors) ? parseResult.errors : parsedValuesAfterValidationCallbacks.errors;
160
187
  }
161
188
  } else {
162
189
  const parsedValuesAfterValidatingByAdapter = await this.__validateByAdapter(adapterToUse, adapterToUse[schemaAdapterFieldType], this.__transformedSchemas[adapterToUse.constructor.name].schemas[0], value, path, options);
@@ -164,9 +191,8 @@ export default class Schema {
164
191
  // eslint-disable-next-line ts/no-unnecessary-condition
165
192
  parseResult.errors = (parseResult.errors || []).concat(parsedValuesAfterValidatingByAdapter.errors);
166
193
  }
167
- const doesNotHaveErrors = !Array.isArray(parseResult.errors) || parseResult.errors.length === 0;
168
194
  const hasToInternalCallback = typeof this.__toInternal === 'function';
169
- const shouldCallToInternalDuringParse = doesNotHaveErrors && hasToInternalCallback && Array.isArray(options.toInternalToBubbleUp) === false;
195
+ const shouldCallToInternalDuringParse = hasToInternalCallback && (options.toInternalToBubbleUp?.length === 0 || Array.isArray(options.toInternalToBubbleUp) === false);
170
196
  // eslint-disable-next-line ts/no-unnecessary-condition
171
197
  const hasNoErrors = parseResult.errors === undefined || (parseResult.errors || []).length === 0;
172
198
  await Promise.all(this.__refinements.map(async (refinement)=>{
@@ -176,6 +202,7 @@ export default class Schema {
176
202
  isValid: false,
177
203
  code: errorOrNothing.code,
178
204
  message: errorOrNothing.message,
205
+ received: parseResult.parsed,
179
206
  path
180
207
  });
181
208
  }));
@@ -184,7 +211,8 @@ export default class Schema {
184
211
  return parseResult;
185
212
  }
186
213
  /**
187
- * 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.
214
+ * This let's you refine the schema with custom validations. This is useful when you want to validate something
215
+ * that is not supported by default by the schema adapter.
188
216
  *
189
217
  * @example
190
218
  * ```typescript
@@ -196,7 +224,8 @@ export default class Schema {
196
224
  *
197
225
  * const { errors, parsed } = await numberSchema.parse(-1);
198
226
  *
199
- * console.log(errors); // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
227
+ * console.log(errors);
228
+ * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
200
229
  * ```
201
230
  *
202
231
  * @param refinementCallback - The callback that will be called to validate the value.
@@ -237,8 +266,8 @@ export default class Schema {
237
266
  return this;
238
267
  }
239
268
  /**
240
- * 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
241
- * the { message: 'Your custom message', allow: false } on the options.
269
+ * Allows the value to be null and ONLY null. You can also use this function to set a custom message when
270
+ * the value is NULL by setting the { message: 'Your custom message', allow: false } on the options.
242
271
  *
243
272
  * @example
244
273
  * ```typescript
@@ -270,8 +299,8 @@ export default class Schema {
270
299
  /**
271
300
  * Appends a custom schema to the schema, this way it will bypass the creation of the schema in runtime.
272
301
  *
273
- * By default when validating, on the first validation we create the schema. Just during the first validation. With this function, you bypass that,
274
- * so you can speed up the validation process.
302
+ * By default when validating, on the first validation we create the schema. Just during the first validation.
303
+ * With this function, you bypass that, so you can speed up the validation process.
275
304
  *
276
305
  * @example
277
306
  * ```typescript
@@ -300,14 +329,15 @@ export default class Schema {
300
329
  return this;
301
330
  }
302
331
  /**
303
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep that way
304
- * otherwise it will set the value to undefined after it's validated.
332
+ * This method will remove the value from the representation of the schema. If the value is undefined it will keep
333
+ * that way otherwise it will set the value to undefined after it's validated.
305
334
  * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
306
335
  * function. This will remove the value from the representation of the schema.
307
336
  *
308
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
309
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
310
- * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
337
+ * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
338
+ * function. But if you want to remove the value from the internal representation, you can pass the argument
339
+ * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
340
+ * the argument `toRepresentation` as true as well.
311
341
  *
312
342
  * @example
313
343
  * ```typescript
@@ -329,9 +359,11 @@ export default class Schema {
329
359
  * ```
330
360
  *
331
361
  *
332
- * @param args - By default, the value will be removed just from the representation, in other words, when you call the {@link data} function.
333
- * But if you want to remove the value from the internal representation, you can pass the argument `toInternal` as true.
334
- * Then if you still want to remove the value from the representation, you will need to pass the argument `toRepresentation` as true as well.
362
+ * @param args - By default, the value will be removed just from the representation, in other words, when you call
363
+ * the {@link data} function.
364
+ * But if you want to remove the value from the internal representation, you can pass the argument `toInternal`
365
+ * as true. Then if you still want to remove the value from the representation, you will need to pass the
366
+ * argument `toRepresentation` as true as well.
335
367
  *
336
368
  * @returns The schema.
337
369
  */ omit(args) {
@@ -360,9 +392,9 @@ export default class Schema {
360
392
  return this;
361
393
  }
362
394
  /**
363
- * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external source
364
- * like a database. You should always return the schema after you save the value, that way we will always have the correct type
365
- * of the schema after the save operation.
395
+ * This function is used in conjunction with the {@link validate} function. It's used to save a value to an external
396
+ * source like a database. You should always return the schema after you save the value, that way we will always have
397
+ * the correct type of the schema after the save operation.
366
398
  *
367
399
  * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
368
400
  *
@@ -408,10 +440,11 @@ export default class Schema {
408
440
  * This function is used to validate the schema and save the value to the database. It is used in
409
441
  * conjunction with the {@link onSave} function.
410
442
  *
411
- * Different from other validation libraries, palmares schemas is aware that you want to save. On your routes/functions
412
- * we recommend to ALWAYS use this function instead of {@link parse} directly. This is because this function by default
413
- * will return an object with the property `save` or the `errors`. If the errors are present, you can return the errors
414
- * to the user. If the save property is present, you can use to save the value to an external source. e.g. a database.
443
+ * Different from other validation libraries, palmares schemas is aware that you want to save. On your
444
+ * routes/functions we recommend to ALWAYS use this function instead of {@link parse} directly. This is because
445
+ * this function by default will return an object with the property `save` or the `errors`. If the errors are present,
446
+ * you can return the errors to the user. If the save property is present, you can use to save the value to an
447
+ * external source. e.g. a database.
415
448
  *
416
449
  * @example
417
450
  * ```typescript
@@ -524,6 +557,7 @@ export default class Schema {
524
557
  * });
525
558
  * ```
526
559
  */ async data(value) {
560
+ this.__getDefaultTransformedSchemas();
527
561
  if (typeof this.__runBeforeParseAndData === 'function') await this.__runBeforeParseAndData(this);
528
562
  value = await this.__parsersToTransformValue(value);
529
563
  if (this.__toRepresentation) value = await Promise.resolve(this.__toRepresentation(value));
@@ -536,7 +570,8 @@ export default class Schema {
536
570
  return this;
537
571
  }
538
572
  /**
539
- * 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.
573
+ * This function is used to add a default value to the schema. If the value is either undefined or null,
574
+ * the default value will be used.
540
575
  *
541
576
  * @example
542
577
  * ```typescript
@@ -555,8 +590,9 @@ export default class Schema {
555
590
  return this;
556
591
  }
557
592
  /**
558
- * 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
559
- * the custom schema your own way. Our API does not support passthrough? No problem, you can use this function to customize the zod schema.
593
+ * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
594
+ * this function to let you customize the custom schema your own way. Our API does not support passthrough?
595
+ * No problem, you can use this function to customize the zod schema.
560
596
  *
561
597
  * @example
562
598
  * ```typescript
@@ -568,12 +604,13 @@ export default class Schema {
568
604
  *
569
605
  * const { errors, parsed } = await numberSchema.parse(-1);
570
606
  *
571
- * console.log(errors); // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
607
+ * console.log(errors);
608
+ * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
572
609
  * ```
573
610
  *
574
611
  * @param callback - The callback that will be called to customize the schema.
575
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want to compile the underlying schema
576
- * to a string so you can save it for future runs.
612
+ * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
613
+ * to compile the underlying schema to a string so you can save it for future runs.
577
614
  *
578
615
  * @returns The schema.
579
616
  */ extends(callback, toStringCallback) {
@@ -584,8 +621,9 @@ export default class Schema {
584
621
  return this;
585
622
  }
586
623
  /**
587
- * 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
588
- * 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.
624
+ * This function is used to transform the value to the representation of the schema. When using the {@link data}
625
+ * function. With this function you have full control to add data cleaning for example, transforming the data
626
+ * and whatever. Another use case is when you want to return deeply nested recursive data.
589
627
  * The schema maps to itself.
590
628
  *
591
629
  * @example
@@ -624,15 +662,16 @@ export default class Schema {
624
662
  * ```
625
663
  * @param toRepresentationCallback - The callback that will be called to transform the value to the representation.
626
664
  * @param options - Options for the toRepresentation function.
627
- * @param options.after - Whether the toRepresentationCallback should be called after the existing toRepresentationCallback. Defaults to true.
628
- * @param options.before - Whether the toRepresentationCallback should be called before the existing toRepresentationCallback. Defaults to true.
665
+ * @param options.after - Whether the toRepresentationCallback should be called after the existing
666
+ * toRepresentationCallback. Defaults to true.
667
+ * @param options.before - Whether the toRepresentationCallback should be called before the existing
668
+ * toRepresentationCallback. Defaults to true.
629
669
  *
630
670
  * @returns The schema with a new return type
631
671
  */ toRepresentation(toRepresentationCallback, options) {
632
672
  if (this.__toRepresentation) {
633
673
  const before = typeof options?.before === 'boolean' ? options.before : typeof options?.after === 'boolean' ? !options.after : true;
634
674
  const existingToRepresentation = this.__toRepresentation;
635
- console.log('existing to representation', existingToRepresentation, before, options?.after);
636
675
  this.__toRepresentation = async (value)=>{
637
676
  if (before) return toRepresentationCallback(await existingToRepresentation(value));
638
677
  else return existingToRepresentation(await toRepresentationCallback(value));
@@ -641,8 +680,9 @@ export default class Schema {
641
680
  return this;
642
681
  }
643
682
  /**
644
- * 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
645
- * 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.
683
+ * This function is used to transform the value to the internal representation of the schema. This is useful
684
+ * when you want to transform the value to a type that the schema adapter can understand. For example, you
685
+ * might want to transform a string to a date. This is the function you use.
646
686
  *
647
687
  * @example
648
688
  * ```typescript
@@ -682,8 +722,9 @@ export default class Schema {
682
722
  return this;
683
723
  }
684
724
  /**
685
- * 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
686
- * here BEFORE the validation. This pretty much transforms the value to a type that the schema adapter can understand.
725
+ * Called before the validation of the schema. Let's say that you want to validate a date that might receive a
726
+ * string, you can convert that string to a date here BEFORE the validation. This pretty much transforms the value
727
+ * to a type that the schema adapter can understand.
687
728
  *
688
729
  * @example
689
730
  * ```
@@ -716,12 +757,6 @@ export default class Schema {
716
757
  }
717
758
  static new(..._args) {
718
759
  const result = new Schema();
719
- const adapterInstance = getDefaultAdapter();
720
- result.__transformedSchemas[adapterInstance.constructor.name] = {
721
- transformed: false,
722
- adapter: adapterInstance,
723
- schemas: []
724
- };
725
760
  return result;
726
761
  }
727
762
  }