@palmares/schemas 0.1.25 → 0.2.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 (305) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/src/adapter/fields/array.cjs +96 -0
  3. package/dist/src/adapter/fields/array.d.ts.map +1 -0
  4. package/dist/src/adapter/fields/array.js +70 -0
  5. package/dist/src/adapter/fields/boolean.cjs +102 -0
  6. package/dist/src/adapter/fields/boolean.d.ts.map +1 -0
  7. package/dist/src/adapter/fields/boolean.js +76 -0
  8. package/dist/src/adapter/fields/datetime.cjs +102 -0
  9. package/dist/src/adapter/fields/datetime.d.ts.map +1 -0
  10. package/dist/src/adapter/fields/datetime.js +76 -0
  11. package/dist/src/adapter/fields/index.cjs +87 -0
  12. package/dist/src/adapter/fields/index.d.ts.map +1 -0
  13. package/dist/src/adapter/fields/index.js +61 -0
  14. package/dist/src/adapter/fields/number.cjs +102 -0
  15. package/dist/src/adapter/fields/number.d.ts.map +1 -0
  16. package/dist/src/adapter/fields/number.js +76 -0
  17. package/dist/src/adapter/fields/object.cjs +108 -0
  18. package/dist/src/adapter/fields/object.d.ts.map +1 -0
  19. package/dist/src/adapter/fields/object.js +82 -0
  20. package/dist/src/adapter/fields/string.cjs +102 -0
  21. package/dist/src/adapter/fields/string.d.ts.map +1 -0
  22. package/dist/src/adapter/fields/string.js +76 -0
  23. package/dist/src/adapter/fields/union.cjs +102 -0
  24. package/dist/src/adapter/fields/union.d.ts.map +1 -0
  25. package/dist/src/adapter/fields/union.js +76 -0
  26. package/dist/src/adapter/index.cjs +65 -0
  27. package/dist/src/adapter/index.d.ts.map +1 -0
  28. package/dist/src/adapter/index.js +40 -0
  29. package/dist/src/adapter/types.cjs +18 -0
  30. package/dist/src/adapter/types.d.ts.map +1 -0
  31. package/dist/src/adapter/types.js +0 -0
  32. package/dist/src/compile.cjs +59 -0
  33. package/dist/src/compile.d.ts.map +1 -0
  34. package/dist/src/compile.js +36 -0
  35. package/dist/src/conf.cjs +53 -0
  36. package/dist/src/conf.d.ts.map +1 -0
  37. package/dist/src/conf.js +27 -0
  38. package/dist/src/constants.cjs +40 -0
  39. package/dist/src/constants.d.ts.map +1 -0
  40. package/dist/src/constants.js +14 -0
  41. package/dist/src/domain.cjs +63 -0
  42. package/dist/src/domain.d.ts.map +1 -0
  43. package/dist/src/domain.js +40 -0
  44. package/dist/src/exceptions.cjs +48 -0
  45. package/dist/{cjs/types → src}/exceptions.d.ts +0 -3
  46. package/dist/src/exceptions.d.ts.map +1 -0
  47. package/dist/src/exceptions.js +24 -0
  48. package/dist/src/index.cjs +6509 -0
  49. package/dist/src/index.d.ts.map +1 -0
  50. package/dist/src/index.js +6445 -0
  51. package/dist/src/middleware.cjs +58 -0
  52. package/dist/src/middleware.d.ts.map +1 -0
  53. package/dist/src/middleware.js +35 -0
  54. package/dist/src/model.cjs +5992 -0
  55. package/dist/src/model.d.ts.map +1 -0
  56. package/dist/src/model.js +5967 -0
  57. package/dist/src/parsers/convert-from-number.cjs +34 -0
  58. package/dist/src/parsers/convert-from-number.d.ts.map +1 -0
  59. package/dist/src/parsers/convert-from-number.js +11 -0
  60. package/dist/src/parsers/convert-from-string.cjs +40 -0
  61. package/dist/src/parsers/convert-from-string.d.ts.map +1 -0
  62. package/dist/src/parsers/convert-from-string.js +17 -0
  63. package/dist/src/parsers/index.cjs +50 -0
  64. package/dist/src/parsers/index.d.ts.map +1 -0
  65. package/dist/src/parsers/index.js +24 -0
  66. package/dist/src/schema/array.cjs +1863 -0
  67. package/dist/src/schema/array.d.ts.map +1 -0
  68. package/dist/src/schema/array.js +1837 -0
  69. package/dist/src/schema/boolean.cjs +1800 -0
  70. package/dist/src/schema/boolean.d.ts.map +1 -0
  71. package/dist/src/schema/boolean.js +1774 -0
  72. package/dist/src/schema/datetime.cjs +1789 -0
  73. package/dist/src/schema/datetime.d.ts.map +1 -0
  74. package/dist/src/schema/datetime.js +1763 -0
  75. package/dist/src/schema/index.cjs +2026 -0
  76. package/dist/src/schema/index.d.ts.map +1 -0
  77. package/dist/src/schema/index.js +2001 -0
  78. package/dist/src/schema/number.cjs +2026 -0
  79. package/dist/src/schema/number.d.ts.map +1 -0
  80. package/dist/src/schema/number.js +2000 -0
  81. package/dist/src/schema/object.cjs +1795 -0
  82. package/dist/src/schema/object.d.ts.map +1 -0
  83. package/dist/src/schema/object.js +1769 -0
  84. package/dist/src/schema/schema.cjs +844 -0
  85. package/dist/src/schema/schema.d.ts.map +1 -0
  86. package/dist/src/schema/schema.js +818 -0
  87. package/dist/src/schema/string.cjs +2126 -0
  88. package/dist/src/schema/string.d.ts.map +1 -0
  89. package/dist/src/schema/string.js +2100 -0
  90. package/dist/src/schema/types.cjs +18 -0
  91. package/dist/src/schema/types.d.ts.map +1 -0
  92. package/dist/src/schema/types.js +0 -0
  93. package/dist/src/schema/union.cjs +1737 -0
  94. package/dist/src/schema/union.d.ts.map +1 -0
  95. package/dist/src/schema/union.js +1711 -0
  96. package/dist/src/types.cjs +18 -0
  97. package/dist/src/types.d.ts.map +1 -0
  98. package/dist/src/types.js +0 -0
  99. package/dist/src/utils.cjs +477 -0
  100. package/dist/src/utils.d.ts.map +1 -0
  101. package/dist/src/utils.js +445 -0
  102. package/dist/src/validators/array.cjs +176 -0
  103. package/dist/src/validators/array.d.ts.map +1 -0
  104. package/dist/src/validators/array.js +150 -0
  105. package/dist/src/validators/boolean.cjs +71 -0
  106. package/dist/src/validators/boolean.d.ts.map +1 -0
  107. package/dist/src/validators/boolean.js +47 -0
  108. package/dist/src/validators/datetime.cjs +129 -0
  109. package/dist/src/validators/datetime.d.ts.map +1 -0
  110. package/dist/src/validators/datetime.js +103 -0
  111. package/dist/src/validators/number.cjs +194 -0
  112. package/dist/src/validators/number.d.ts.map +1 -0
  113. package/dist/src/validators/number.js +166 -0
  114. package/dist/src/validators/object.cjs +69 -0
  115. package/dist/src/validators/object.d.ts.map +1 -0
  116. package/dist/src/validators/object.js +46 -0
  117. package/dist/src/validators/schema.cjs +158 -0
  118. package/dist/src/validators/schema.d.ts.map +1 -0
  119. package/dist/src/validators/schema.js +132 -0
  120. package/dist/src/validators/string.cjs +255 -0
  121. package/dist/src/validators/string.d.ts.map +1 -0
  122. package/dist/src/validators/string.js +224 -0
  123. package/dist/src/validators/types.cjs +18 -0
  124. package/dist/src/validators/types.d.ts.map +1 -0
  125. package/dist/src/validators/types.js +0 -0
  126. package/dist/src/validators/union.cjs +69 -0
  127. package/dist/src/validators/union.d.ts.map +1 -0
  128. package/dist/src/validators/union.js +46 -0
  129. package/dist/src/validators/utils.cjs +165 -0
  130. package/dist/src/validators/utils.d.ts.map +1 -0
  131. package/dist/src/validators/utils.js +142 -0
  132. package/dist/tsconfig.types.tsbuildinfo +1 -0
  133. package/package.json +15 -23
  134. package/dist/cjs/src/adapter/fields/array.js +0 -152
  135. package/dist/cjs/src/adapter/fields/boolean.js +0 -162
  136. package/dist/cjs/src/adapter/fields/datetime.js +0 -162
  137. package/dist/cjs/src/adapter/fields/index.js +0 -311
  138. package/dist/cjs/src/adapter/fields/number.js +0 -162
  139. package/dist/cjs/src/adapter/fields/object.js +0 -171
  140. package/dist/cjs/src/adapter/fields/string.js +0 -162
  141. package/dist/cjs/src/adapter/fields/union.js +0 -162
  142. package/dist/cjs/src/adapter/index.js +0 -201
  143. package/dist/cjs/src/adapter/types.js +0 -4
  144. package/dist/cjs/src/compile.js +0 -262
  145. package/dist/cjs/src/conf.js +0 -26
  146. package/dist/cjs/src/constants.js +0 -36
  147. package/dist/cjs/src/domain.js +0 -156
  148. package/dist/cjs/src/exceptions.js +0 -168
  149. package/dist/cjs/src/index.js +0 -317
  150. package/dist/cjs/src/middleware.js +0 -213
  151. package/dist/cjs/src/model.js +0 -628
  152. package/dist/cjs/src/parsers/convert-from-number.js +0 -15
  153. package/dist/cjs/src/parsers/convert-from-string.js +0 -19
  154. package/dist/cjs/src/parsers/index.js +0 -20
  155. package/dist/cjs/src/schema/array.js +0 -1014
  156. package/dist/cjs/src/schema/boolean.js +0 -883
  157. package/dist/cjs/src/schema/datetime.js +0 -840
  158. package/dist/cjs/src/schema/index.js +0 -12
  159. package/dist/cjs/src/schema/number.js +0 -976
  160. package/dist/cjs/src/schema/object.js +0 -1052
  161. package/dist/cjs/src/schema/schema.js +0 -1840
  162. package/dist/cjs/src/schema/string.js +0 -1038
  163. package/dist/cjs/src/schema/types.js +0 -4
  164. package/dist/cjs/src/schema/union.js +0 -1070
  165. package/dist/cjs/src/types.js +0 -4
  166. package/dist/cjs/src/utils.js +0 -647
  167. package/dist/cjs/src/validators/array.js +0 -462
  168. package/dist/cjs/src/validators/boolean.js +0 -201
  169. package/dist/cjs/src/validators/datetime.js +0 -291
  170. package/dist/cjs/src/validators/number.js +0 -375
  171. package/dist/cjs/src/validators/object.js +0 -291
  172. package/dist/cjs/src/validators/schema.js +0 -322
  173. package/dist/cjs/src/validators/string.js +0 -467
  174. package/dist/cjs/src/validators/types.js +0 -4
  175. package/dist/cjs/src/validators/union.js +0 -233
  176. package/dist/cjs/src/validators/utils.js +0 -499
  177. package/dist/cjs/tsconfig.types.tsbuildinfo +0 -1
  178. package/dist/cjs/types/adapter/fields/array.d.ts.map +0 -1
  179. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +0 -1
  180. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +0 -1
  181. package/dist/cjs/types/adapter/fields/index.d.ts.map +0 -1
  182. package/dist/cjs/types/adapter/fields/number.d.ts.map +0 -1
  183. package/dist/cjs/types/adapter/fields/object.d.ts.map +0 -1
  184. package/dist/cjs/types/adapter/fields/string.d.ts.map +0 -1
  185. package/dist/cjs/types/adapter/fields/union.d.ts.map +0 -1
  186. package/dist/cjs/types/adapter/index.d.ts.map +0 -1
  187. package/dist/cjs/types/adapter/types.d.ts.map +0 -1
  188. package/dist/cjs/types/compile.d.ts.map +0 -1
  189. package/dist/cjs/types/conf.d.ts.map +0 -1
  190. package/dist/cjs/types/constants.d.ts.map +0 -1
  191. package/dist/cjs/types/domain.d.ts.map +0 -1
  192. package/dist/cjs/types/exceptions.d.ts.map +0 -1
  193. package/dist/cjs/types/index.d.ts.map +0 -1
  194. package/dist/cjs/types/middleware.d.ts.map +0 -1
  195. package/dist/cjs/types/model.d.ts.map +0 -1
  196. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +0 -1
  197. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +0 -1
  198. package/dist/cjs/types/parsers/index.d.ts.map +0 -1
  199. package/dist/cjs/types/schema/array.d.ts.map +0 -1
  200. package/dist/cjs/types/schema/boolean.d.ts.map +0 -1
  201. package/dist/cjs/types/schema/datetime.d.ts.map +0 -1
  202. package/dist/cjs/types/schema/index.d.ts.map +0 -1
  203. package/dist/cjs/types/schema/number.d.ts.map +0 -1
  204. package/dist/cjs/types/schema/object.d.ts.map +0 -1
  205. package/dist/cjs/types/schema/schema.d.ts.map +0 -1
  206. package/dist/cjs/types/schema/string.d.ts.map +0 -1
  207. package/dist/cjs/types/schema/types.d.ts.map +0 -1
  208. package/dist/cjs/types/schema/union.d.ts.map +0 -1
  209. package/dist/cjs/types/types.d.ts.map +0 -1
  210. package/dist/cjs/types/utils.d.ts.map +0 -1
  211. package/dist/cjs/types/validators/array.d.ts.map +0 -1
  212. package/dist/cjs/types/validators/boolean.d.ts.map +0 -1
  213. package/dist/cjs/types/validators/datetime.d.ts.map +0 -1
  214. package/dist/cjs/types/validators/number.d.ts.map +0 -1
  215. package/dist/cjs/types/validators/object.d.ts.map +0 -1
  216. package/dist/cjs/types/validators/schema.d.ts.map +0 -1
  217. package/dist/cjs/types/validators/string.d.ts.map +0 -1
  218. package/dist/cjs/types/validators/types.d.ts.map +0 -1
  219. package/dist/cjs/types/validators/union.d.ts.map +0 -1
  220. package/dist/cjs/types/validators/utils.d.ts.map +0 -1
  221. package/dist/esm/src/adapter/fields/array.js +0 -13
  222. package/dist/esm/src/adapter/fields/boolean.js +0 -20
  223. package/dist/esm/src/adapter/fields/datetime.js +0 -20
  224. package/dist/esm/src/adapter/fields/index.js +0 -37
  225. package/dist/esm/src/adapter/fields/number.js +0 -20
  226. package/dist/esm/src/adapter/fields/object.js +0 -26
  227. package/dist/esm/src/adapter/fields/string.js +0 -20
  228. package/dist/esm/src/adapter/fields/union.js +0 -20
  229. package/dist/esm/src/adapter/index.js +0 -21
  230. package/dist/esm/src/adapter/types.js +0 -1
  231. package/dist/esm/src/compile.js +0 -10
  232. package/dist/esm/src/conf.js +0 -18
  233. package/dist/esm/src/constants.js +0 -4
  234. package/dist/esm/src/domain.js +0 -11
  235. package/dist/esm/src/exceptions.js +0 -15
  236. package/dist/esm/src/index.js +0 -126
  237. package/dist/esm/src/middleware.js +0 -29
  238. package/dist/esm/src/model.js +0 -264
  239. package/dist/esm/src/parsers/convert-from-number.js +0 -8
  240. package/dist/esm/src/parsers/convert-from-string.js +0 -14
  241. package/dist/esm/src/parsers/index.js +0 -2
  242. package/dist/esm/src/schema/array.js +0 -487
  243. package/dist/esm/src/schema/boolean.js +0 -520
  244. package/dist/esm/src/schema/datetime.js +0 -477
  245. package/dist/esm/src/schema/index.js +0 -3
  246. package/dist/esm/src/schema/number.js +0 -610
  247. package/dist/esm/src/schema/object.js +0 -521
  248. package/dist/esm/src/schema/schema.js +0 -767
  249. package/dist/esm/src/schema/string.js +0 -668
  250. package/dist/esm/src/schema/types.js +0 -1
  251. package/dist/esm/src/schema/union.js +0 -468
  252. package/dist/esm/src/types.js +0 -14
  253. package/dist/esm/src/utils.js +0 -207
  254. package/dist/esm/src/validators/array.js +0 -140
  255. package/dist/esm/src/validators/boolean.js +0 -37
  256. package/dist/esm/src/validators/datetime.js +0 -89
  257. package/dist/esm/src/validators/number.js +0 -148
  258. package/dist/esm/src/validators/object.js +0 -39
  259. package/dist/esm/src/validators/schema.js +0 -118
  260. package/dist/esm/src/validators/string.js +0 -202
  261. package/dist/esm/src/validators/types.js +0 -1
  262. package/dist/esm/src/validators/union.js +0 -39
  263. package/dist/esm/src/validators/utils.js +0 -147
  264. /package/dist/{cjs/types → src}/adapter/fields/array.d.ts +0 -0
  265. /package/dist/{cjs/types → src}/adapter/fields/boolean.d.ts +0 -0
  266. /package/dist/{cjs/types → src}/adapter/fields/datetime.d.ts +0 -0
  267. /package/dist/{cjs/types → src}/adapter/fields/index.d.ts +0 -0
  268. /package/dist/{cjs/types → src}/adapter/fields/number.d.ts +0 -0
  269. /package/dist/{cjs/types → src}/adapter/fields/object.d.ts +0 -0
  270. /package/dist/{cjs/types → src}/adapter/fields/string.d.ts +0 -0
  271. /package/dist/{cjs/types → src}/adapter/fields/union.d.ts +0 -0
  272. /package/dist/{cjs/types → src}/adapter/index.d.ts +0 -0
  273. /package/dist/{cjs/types → src}/adapter/types.d.ts +0 -0
  274. /package/dist/{cjs/types → src}/compile.d.ts +0 -0
  275. /package/dist/{cjs/types → src}/conf.d.ts +0 -0
  276. /package/dist/{cjs/types → src}/constants.d.ts +0 -0
  277. /package/dist/{cjs/types → src}/domain.d.ts +0 -0
  278. /package/dist/{cjs/types → src}/index.d.ts +0 -0
  279. /package/dist/{cjs/types → src}/middleware.d.ts +0 -0
  280. /package/dist/{cjs/types → src}/model.d.ts +0 -0
  281. /package/dist/{cjs/types → src}/parsers/convert-from-number.d.ts +0 -0
  282. /package/dist/{cjs/types → src}/parsers/convert-from-string.d.ts +0 -0
  283. /package/dist/{cjs/types → src}/parsers/index.d.ts +0 -0
  284. /package/dist/{cjs/types → src}/schema/array.d.ts +0 -0
  285. /package/dist/{cjs/types → src}/schema/boolean.d.ts +0 -0
  286. /package/dist/{cjs/types → src}/schema/datetime.d.ts +0 -0
  287. /package/dist/{cjs/types → src}/schema/index.d.ts +0 -0
  288. /package/dist/{cjs/types → src}/schema/number.d.ts +0 -0
  289. /package/dist/{cjs/types → src}/schema/object.d.ts +0 -0
  290. /package/dist/{cjs/types → src}/schema/schema.d.ts +0 -0
  291. /package/dist/{cjs/types → src}/schema/string.d.ts +0 -0
  292. /package/dist/{cjs/types → src}/schema/types.d.ts +0 -0
  293. /package/dist/{cjs/types → src}/schema/union.d.ts +0 -0
  294. /package/dist/{cjs/types → src}/types.d.ts +0 -0
  295. /package/dist/{cjs/types → src}/utils.d.ts +0 -0
  296. /package/dist/{cjs/types → src}/validators/array.d.ts +0 -0
  297. /package/dist/{cjs/types → src}/validators/boolean.d.ts +0 -0
  298. /package/dist/{cjs/types → src}/validators/datetime.d.ts +0 -0
  299. /package/dist/{cjs/types → src}/validators/number.d.ts +0 -0
  300. /package/dist/{cjs/types → src}/validators/object.d.ts +0 -0
  301. /package/dist/{cjs/types → src}/validators/schema.d.ts +0 -0
  302. /package/dist/{cjs/types → src}/validators/string.d.ts +0 -0
  303. /package/dist/{cjs/types → src}/validators/types.d.ts +0 -0
  304. /package/dist/{cjs/types → src}/validators/union.d.ts +0 -0
  305. /package/dist/{cjs/types → src}/validators/utils.d.ts +0 -0
@@ -1,468 +0,0 @@
1
- import { Schema } from './schema';
2
- import { defaultTransform, defaultTransformToAdapter, transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas } from '../utils';
3
- import { unionValidation } from '../validators/union';
4
- import { Validator } from '../validators/utils';
5
- export class UnionSchema extends Schema {
6
- fieldType = 'union';
7
- __type = {
8
- message: 'Invalid type',
9
- check: (value)=>Array.from(this.__schemas).some((schema)=>schema['__type'].check(value))
10
- };
11
- __schemas = new Set();
12
- constructor(schemas){
13
- super();
14
- this.__schemas = new Set(schemas);
15
- }
16
- async __transformToAdapter(options) {
17
- return defaultTransformToAdapter(async (adapter)=>{
18
- const promises = [];
19
- const shouldBeHighPriorityFallback = adapter.union === undefined;
20
- const transformedSchemasAsString = [];
21
- const transformedSchemas = [];
22
- let shouldBeHandledByFallback = shouldBeHighPriorityFallback;
23
- for (const schemaToTransform of this.__schemas.values()){
24
- const awaitableTransformer = async ()=>{
25
- const [transformedData, shouldAddFallbackValidationForThisKey] = await transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas(schemaToTransform, options);
26
- if (shouldAddFallbackValidationForThisKey) shouldBeHandledByFallback = true;
27
- for (const transformedSchema of transformedData){
28
- transformedSchemasAsString.push(transformedSchema.asString);
29
- transformedSchemas.push(transformedSchema.transformed);
30
- }
31
- };
32
- promises.push(awaitableTransformer());
33
- }
34
- if (shouldBeHandledByFallback) {
35
- Validator.createAndAppendFallback(this, unionValidation(Array.from(this.__schemas)), {
36
- at: 0,
37
- removeCurrent: true
38
- });
39
- }
40
- await Promise.all(promises);
41
- return defaultTransform('union', this, adapter, adapter.union, (isStringVersion)=>({
42
- nullable: this.__nullable,
43
- type: this.__type,
44
- optional: this.__optional,
45
- schemas: isStringVersion ? transformedSchemasAsString : transformedSchemas,
46
- parsers: {
47
- nullable: this.__nullable.allow,
48
- optional: this.__optional.allow
49
- }
50
- }), {}, {
51
- shouldAddStringVersion: options.shouldAddStringVersion,
52
- fallbackIfNotSupported: async ()=>{
53
- if (options.appendFallbacksBeforeAdapterValidation) options.appendFallbacksBeforeAdapterValidation(this, 'union', async (adapter, fieldAdapter, schema, translatedSchemas, value, path, options)=>{
54
- const parsedValues = {
55
- parsed: value,
56
- errors: []
57
- };
58
- // const initialErrorsAsHashedSet = new Set(Array.from(options.errorsAsHashedSet || []));
59
- for (const translatedSchema of translatedSchemas){
60
- //options.errorsAsHashedSet = initialErrorsAsHashedSet;
61
- const { parsed, errors } = await schema.__validateByAdapter(adapter, fieldAdapter, translatedSchema, value, path, options);
62
- // eslint-disable-next-line ts/no-unnecessary-condition
63
- if ((errors || []).length <= 0) return {
64
- parsed,
65
- errors
66
- };
67
- else {
68
- parsedValues.parsed = parsed;
69
- // eslint-disable-next-line ts/no-unnecessary-condition
70
- parsedValues.errors = (parsedValues.errors || []).concat(errors || []);
71
- }
72
- }
73
- return parsedValues;
74
- });
75
- const transformedSchemasAsPromises = [];
76
- for (const schema of this.__schemas)transformedSchemasAsPromises.push(schema.__transformToAdapter(options));
77
- return (await Promise.all(transformedSchemasAsPromises)).flat();
78
- }
79
- });
80
- }, this, this.__transformedSchemas, options, 'union');
81
- }
82
- /**
83
- * This let's you refine the schema with custom validations. This is useful when you want to validate something that
84
- * is not supported by default by the schema adapter.
85
- *
86
- * @example
87
- * ```typescript
88
- * import * as p from '@palmares/schemas';
89
- *
90
- * const numberSchema = p.number().refine((value) => {
91
- * if (value < 0) return { code: 'invalid_number', message: 'The number should be greater than 0' };
92
- * });
93
- *
94
- * const { errors, parsed } = await numberSchema.parse(-1);
95
- *
96
- * // [{ isValid: false, code: 'invalid_number', message: 'The number should be greater than 0', path: [] }]
97
- * console.log(errors);
98
- * ```
99
- *
100
- * @param refinementCallback - The callback that will be called to validate the value.
101
- * @param options - Options for the refinement.
102
- * @param options.isAsync - Whether the callback is async or not. Defaults to true.
103
- */ refine(refinementCallback) {
104
- return super.refine(refinementCallback);
105
- }
106
- /**
107
- * Allows the value to be either undefined or null.
108
- *
109
- * @example
110
- * ```typescript
111
- * import * as p from '@palmares/schemas';
112
- *
113
- * const numberSchema = p.number().optional();
114
- *
115
- * const { errors, parsed } = await numberSchema.parse(undefined);
116
- *
117
- * console.log(parsed); // undefined
118
- *
119
- * const { errors, parsed } = await numberSchema.parse(null);
120
- *
121
- * console.log(parsed); // null
122
- *
123
- * const { errors, parsed } = await numberSchema.parse(1);
124
- *
125
- * console.log(parsed); // 1
126
- * ```
127
- *
128
- * @returns - The schema we are working with.
129
- */ optional(options) {
130
- return super.optional(options);
131
- }
132
- /**
133
- * Just adds a message when the value is undefined. It's just a syntax sugar for
134
- *
135
- * ```typescript
136
- * p.string().optional({ message: 'This value cannot be null', allow: false })
137
- * ```
138
- *
139
- * @param options - The options of nonOptional function
140
- * @param options.message - A custom message if the value is undefined.
141
- *
142
- * @returns - The schema.
143
- */ nonOptional(options) {
144
- return super.optional({
145
- message: options?.message,
146
- allow: false
147
- });
148
- }
149
- /**
150
- * Allows the value to be null and ONLY null. You can also use this function to set a custom message when the value
151
- * is NULL by setting the { message: 'Your custom message', allow: false } on the options.
152
- *
153
- * @example
154
- * ```typescript
155
- * import * as p from '@palmares/schemas';
156
- *
157
- * const numberSchema = p.number().nullable();
158
- *
159
- * const { errors, parsed } = await numberSchema.parse(null);
160
- *
161
- * console.log(parsed); // null
162
- *
163
- * const { errors, parsed } = await numberSchema.parse(undefined);
164
- *
165
- * console.log(errors); // [{ isValid: false, code: 'invalid_type', message: 'Invalid type', path: [] }]
166
- * ```
167
- *
168
- * @param options - The options for the nullable function.
169
- * @param options.message - The message to be shown when the value is not null. Defaults to 'Cannot be null'.
170
- * @param options.allow - Whether the value can be null or not. Defaults to true.
171
- *
172
- * @returns The schema.
173
- */ nullable(options) {
174
- return super.nullable(options);
175
- }
176
- /**
177
- * Just adds a message when the value is null. It's just a syntax sugar for
178
- *
179
- * ```typescript
180
- * p.string().nullable({ message: 'This value cannot be null', allow: false })
181
- * ```
182
- *
183
- * @param options - The options of nonNullable function
184
- * @param options.message - A custom message if the value is null.
185
- *
186
- * @returns - The schema.
187
- */ nonNullable(options) {
188
- return super.nullable({
189
- message: options?.message || '',
190
- allow: false
191
- });
192
- }
193
- /**
194
- * This method will remove the value from the representation of the schema. If the value is undefined it will keep
195
- * that way
196
- * otherwise it will set the value to undefined after it's validated.
197
- * This is used in conjunction with the {@link data} function, the {@link parse} function or {@link validate}
198
- * function. This will remove the value from the representation of the schema.
199
- *
200
- * By default, the value will be removed just from the representation, in other words, when you call the {@link data}
201
- * function. But if you want to remove the value from the internal representation, you can pass the argument
202
- * `toInternal` as true. Then if you still want to remove the value from the representation, you will need to pass
203
- * the argument `toRepresentation` as true as well.
204
- *
205
- * @example
206
- * ```typescript
207
- * import * as p from '@palmares/schemas';
208
- *
209
- * const userSchema = p.object({
210
- * id: p.number().optional(),
211
- * name: p.string(),
212
- * password: p.string().omit()
213
- * });
214
- *
215
- * const user = await userSchema.data({
216
- * id: 1,
217
- * name: 'John Doe',
218
- * password: '123456'
219
- * });
220
- *
221
- * console.log(user); // { id: 1, name: 'John Doe' }
222
- * ```
223
- *
224
- *
225
- * @param args - By default, the value will be removed just from the representation, in other words, when you call
226
- * the {@link data} function. But if you want to remove the value from the internal representation, you can pass
227
- * the argument `toInternal` as true. Then if you still want to remove the value from the representation, you
228
- * will need to pass the argument `toRepresentation` as true as well.
229
- *
230
- * @returns The schema.
231
- */ omit(args) {
232
- return super.omit(args);
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.
238
- *
239
- * You can use the {@link toRepresentation} function to transform and clean the value it returns after the save.
240
- *
241
- * @example
242
- * ```typescript
243
- * import * as p from '@palmares/schemas';
244
- *
245
- * import { User } from './models';
246
- *
247
- * const userSchema = p.object({
248
- * id: p.number().optional(),
249
- * name: p.string(),
250
- * email: p.string().email(),
251
- * }).onSave(async (value) => {
252
- * // Create or update the user on the database using palmares models or any other library of your choice.
253
- * if (value.id)
254
- * await User.default.set(value, { search: { id: value.id } });
255
- * else
256
- * await User.default.set(value);
257
- *
258
- * return value;
259
- * });
260
- *
261
- *
262
- * // Then, on your controller, do something like this:
263
- * const { isValid, save, errors } = await userSchema.validate(req.body);
264
- * if (isValid) {
265
- * const savedValue = await save();
266
- * return Response.json(savedValue, { status: 201 });
267
- * }
268
- *
269
- * return Response.json({ errors }, { status: 400 });
270
- * ```
271
- *
272
- * @param callback - The callback that will be called to save the value on an external source.
273
- *
274
- * @returns The schema.
275
- */ onSave(callback) {
276
- return super.onSave(callback);
277
- }
278
- /**
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.
281
- *
282
- * @example
283
- * ```typescript
284
- * import * as p from '@palmares/schemas';
285
- *
286
- * const numberSchema = p.number().default(0);
287
- *
288
- * const { errors, parsed } = await numberSchema.parse(undefined);
289
- *
290
- * console.log(parsed); // 0
291
- * ```
292
- */ default(defaultValueOrFunction) {
293
- return super.default(defaultValueOrFunction);
294
- }
295
- /**
296
- * This function is used to transform the value to the representation without validating it.
297
- * This is useful when you want to return a data from a query directly to the user. But for example
298
- * you are returning the data of a user, you can clean the password or any other sensitive data.
299
- *
300
- * @example
301
- * ```typescript
302
- * import * as p from '@palmares/schemas';
303
- *
304
- * const userSchema = p.object({
305
- * id: p.number().optional(),
306
- * name: p.string(),
307
- * email: p.string().email(),
308
- * password: p.string().optional()
309
- * }).toRepresentation(async (value) => {
310
- * return {
311
- * id: value.id,
312
- * name: value.name,
313
- * email: value.email
314
- * }
315
- * });
316
- *
317
- * const user = await userSchema.data({
318
- * id: 1,
319
- * name: 'John Doe',
320
- * email: 'john@gmail.com',
321
- * password: '123456'
322
- * });
323
- * ```
324
- */ async data(value) {
325
- const parsedValue = await super.data(value);
326
- for (const schema of Array.from(this.__schemas)){
327
- if (schema['__optional'].allow && value === undefined) return schema.data(parsedValue);
328
- if (schema['__nullable'].allow && value === null) return schema.data(parsedValue);
329
- if (schema['__type'].check(parsedValue)) return schema.data(parsedValue);
330
- }
331
- return parsedValue;
332
- }
333
- /**
334
- * This function let's you customize the schema your own way. After we translate the schema on the adapter we call
335
- * this function to let you customize the custom schema your own way. Our API does not support passthrough?
336
- * No problem, you can use this function to customize the zod schema.
337
- *
338
- * @example
339
- * ```typescript
340
- * import * as p from '@palmares/schemas';
341
- *
342
- * const numberSchema = p.number().extends((schema) => {
343
- * return schema.nonnegative();
344
- * });
345
- *
346
- * const { errors, parsed } = await numberSchema.parse(-1);
347
- *
348
- * // [{ isValid: false, code: 'nonnegative', message: 'The number should be nonnegative', path: [] }]
349
- * console.log(errors);
350
- * ```
351
- *
352
- * @param callback - The callback that will be called to customize the schema.
353
- * @param toStringCallback - The callback that will be called to transform the schema to a string when you want
354
- * to compile the underlying schema to a string so you can save it for future runs.
355
- *
356
- * @returns The schema.
357
- */ extends(callback, toStringCallback) {
358
- return super.extends(callback, toStringCallback);
359
- }
360
- /**
361
- * This function is used to transform the value to the representation of the schema. When using the {@link data}
362
- * function. With this function you have full control to add data cleaning for example, transforming the data and
363
- * whatever. Another use case is when you want to return deeply nested recursive data. 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
- */ toRepresentation(toRepresentationCallback) {
403
- return super.toRepresentation(toRepresentationCallback);
404
- }
405
- /**
406
- * This function is used to transform the value to the internal representation of the schema. This is useful when you
407
- * want to transform the value to a type that the schema adapter can understand. For example, you might want to
408
- * transform a string to a date. This is the function you use.
409
- *
410
- * @example
411
- * ```typescript
412
- * import * as p from '@palmares/schemas';
413
- *
414
- * const dateSchema = p.string().toInternal((value) => {
415
- * return new Date(value);
416
- * });
417
- *
418
- * const date = await dateSchema.parse('2021-01-01');
419
- *
420
- * console.log(date); // Date object
421
- *
422
- * const rgbToColorSchema = p.object({
423
- * r: p.number().min(0).max(255),
424
- * g: p.number().min(0).max(255),
425
- * b: p.number().min(0).max(255),
426
- * }).toInternal(async (value) => {
427
- * if (value.r === 255 && value.g === 0 && value.b === 0) return 'red';
428
- * if (value.r === 0 && value.g === 255 && value.b === 0) return 'green';
429
- * if (value.r === 0 && value.g === 0 && value.b === 255) return 'blue';
430
- * return `rgb(${value.r}, ${value.g}, ${value.b})`;
431
- * });
432
- * ```
433
- *
434
- * @param toInternalCallback - The callback that will be called to transform the value to the internal representation.
435
- *
436
- * @returns The schema with a new return type.
437
- */ toInternal(toInternalCallback) {
438
- return super.toInternal(toInternalCallback);
439
- }
440
- /**
441
- * Called before the validation of the schema. Let's say that you want to validate a date that might receive a string,
442
- * you can convert that string to a date here BEFORE the validation. This pretty much transforms the value to a type
443
- * that the schema adapter can understand.
444
- *
445
- * @example
446
- * ```
447
- * import * as p from '@palmares/schemas';
448
- * import * as z from 'zod';
449
- *
450
- * const customRecordToMapSchema = p.schema().appendSchema(z.map()).toValidate(async (value) => {
451
- * return new Map(value); // Before validating we transform the value to a map.
452
- * });
453
- *
454
- * const { errors, parsed } = await customRecordToMapSchema.parse({ key: 'value' });
455
- * ```
456
- *
457
- * @param toValidateCallback - The callback that will be called to validate the value.
458
- *
459
- * @returns The schema with a new return type.
460
- */ toValidate(toValidateCallback) {
461
- return super.toValidate(toValidateCallback);
462
- }
463
- static new(schemas) {
464
- const returnValue = new UnionSchema(schemas);
465
- return returnValue;
466
- }
467
- }
468
- export const union = UnionSchema.new;
@@ -1,14 +0,0 @@
1
- /**
2
- * Retrieve the typescript type of a schema.
3
- *
4
- * First generic is `typeof myCustomSchema`.
5
- *
6
- * Second generic is:
7
- * - 'input' - The data passed to `.parse` and `.validate` functions. Defaults to this.
8
- * - 'output' - (use `'representation'` to get the data format you are
9
- * passing to the user) The data passed to `.data` function to return to the
10
- * user.
11
- * - 'representation' - The data after `toRepresentation`, usually, use this over 'output'
12
- * - 'internal' - The data after it's transformed for the `toSave` callback.
13
- * - 'validate' - The data for `toValidate` callback.
14
- */ export { };
@@ -1,207 +0,0 @@
1
- import { getDefaultAdapter } from './conf';
2
- import { checkType, nullable, optional } from './validators/schema';
3
- import { Validator } from './validators/utils';
4
- /**
5
- * The usage of this is that imagine that the library doesn't support a specific feature that we support on
6
- * our schema definition, it can return an instance of this class and with this instance we are able to
7
- * fallback to our default implementation of the schema validation.
8
- */ export class WithFallback {
9
- $$type = '$PWithFallback';
10
- fallbackFor;
11
- transformedSchema;
12
- adapterType;
13
- constructor(adapterType, fallbackFor, transformedSchema){
14
- this.adapterType = adapterType;
15
- this.fallbackFor = new Set(fallbackFor);
16
- this.transformedSchema = transformedSchema;
17
- }
18
- }
19
- /**
20
- * Factory function for creating a new instance of WithFallback. We call that function when parsing the
21
- * schema adapter, and then, inside of the adapter the user will can the inner function to create a new
22
- * instance of WithFallback.
23
- *
24
- * @param adapterType - The type of the adapter that we are using.
25
- *
26
- * @returns - A currying function that will create a new instance of WithFallback.
27
- */ export function withFallbackFactory(adapterType) {
28
- return (fallbackFor, transformedSchema)=>new WithFallback(adapterType, fallbackFor, transformedSchema);
29
- }
30
- export function parseErrorsFactory(schemaAdapter) {
31
- return async (errorOrErrors, metadata)=>{
32
- const errorsIsAnArray = Array.isArray(errorOrErrors);
33
- if (errorsIsAnArray) return Promise.all(errorOrErrors.map((error)=>schemaAdapter.formatError.bind(schemaAdapter)(error, metadata)));
34
- return [
35
- await schemaAdapter.formatError.bind(schemaAdapter)(errorOrErrors, metadata)
36
- ];
37
- };
38
- }
39
- /**
40
- * The default transform function that we use for the schema adapters. This function tries to abstract away
41
- * the complexity of translating the schema to the adapter.
42
- *
43
- * So first things first, WHAT IS a fallback? A fallback is a function that we call when the user defines a
44
- * validation that is not supported by the adapter. For example, imagine that for some reason the adapter
45
- * doesn't support the `max` validation, we can define a fallback for that validation and then, when the
46
- * user defines that validation, we call the fallback function. So, even if the adapter doesn't support that
47
- * validation our schema will still be able to validate that.
48
- *
49
- * @param type - The type of the adapter that we are using, can be a number, an object, all of the possible
50
- * schema adapters.
51
- * @param schema - The schema that we are translating.
52
- * @param validationData - The data that we are using to validate the schema. This means for example, the
53
- * `max` validation, the `min` validation, etc. The message of the validation when it is not valid, etc.
54
- * @param fallbackFunctions - The fallback functions that we are using to validate the schema. Those are
55
- * the functions we fallback to when the user defines a validation that is not supported by the adapter.
56
- *
57
- * @returns - The translated schema for something that the adapter is able to understand.
58
- */ export async function defaultTransform(type, schema, adapter, fieldAdapter, getValidationData, fallbackFunctions, options) {
59
- const validationData = await Promise.resolve(getValidationData(false));
60
- const validationDataForStringVersion = options.shouldAddStringVersion ? await Promise.resolve(getValidationData(true)) : undefined;
61
- const schemaWithPrivateFields = schema;
62
- const checkIfShouldUseParserAndAppend = (parser)=>{
63
- const isValidationDataAParser = validationData.parsers?.[parser] !== undefined;
64
- if (isValidationDataAParser) schema.__parsers._fallbacks.add(parser);
65
- };
66
- const getExtendedOrNotSchemaAndString = (schema, toStringVersion)=>{
67
- const extendedOrNotSchema = typeof schemaWithPrivateFields.__extends?.callback === 'function' ? schemaWithPrivateFields.__extends.callback(schema) : schema;
68
- const extendedOrNotSchemaString = typeof schemaWithPrivateFields.__extends?.toStringCallback === 'function' ? schemaWithPrivateFields.__extends.toStringCallback(toStringVersion) : toStringVersion;
69
- return [
70
- extendedOrNotSchema,
71
- extendedOrNotSchemaString
72
- ];
73
- };
74
- const checkIfShouldAppendFallbackAndAppend = (fallback)=>{
75
- const wereArgumentsForThatFallbackDefinedAndFallbackFunctionDefined = validationData[fallback] !== undefined && fallbackFunctions[fallback] !== undefined;
76
- if (wereArgumentsForThatFallbackDefinedAndFallbackFunctionDefined) {
77
- const fallbackReturnType = fallbackFunctions[fallback](validationData[fallback]);
78
- Validator.createAndAppendFallback(schema, fallbackReturnType);
79
- }
80
- };
81
- const appendRootFallback = ()=>{
82
- if (options.validatorsIfFallbackOrNotSupported) {
83
- const validatorsIfFallbackOrNotSupported = Array.isArray(options.validatorsIfFallbackOrNotSupported) ? options.validatorsIfFallbackOrNotSupported : [
84
- options.validatorsIfFallbackOrNotSupported
85
- ];
86
- for (const fallback of validatorsIfFallbackOrNotSupported)Validator.createAndAppendFallback(schema, fallback);
87
- }
88
- };
89
- const appendRequiredFallbacks = ()=>{
90
- // eslint-disable-next-line ts/no-unnecessary-condition
91
- const hasFallbacks = schemaWithPrivateFields.__rootFallbacksValidator?.['$$type'] === '$PValidator';
92
- if (hasFallbacks) {
93
- Validator.createAndAppendFallback(schema, optional(schemaWithPrivateFields.__optional));
94
- Validator.createAndAppendFallback(schema, nullable(schemaWithPrivateFields.__nullable));
95
- Validator.createAndAppendFallback(schema, checkType(schemaWithPrivateFields.__type));
96
- }
97
- };
98
- const isFieldAdapterNotSupportedForThatFieldType = fieldAdapter === undefined;
99
- if (options.fallbackIfNotSupported !== undefined && isFieldAdapterNotSupportedForThatFieldType) {
100
- const existingFallbacks = Object.keys(fallbackFunctions);
101
- const allParsers = Object.keys(validationData['parsers']);
102
- appendRootFallback();
103
- for (const fallback of existingFallbacks)checkIfShouldAppendFallbackAndAppend(fallback);
104
- for (const parser of allParsers)checkIfShouldUseParserAndAppend(parser);
105
- appendRequiredFallbacks();
106
- return options.fallbackIfNotSupported();
107
- }
108
- if (!fieldAdapter) throw new Error('The field adapter is not supported and no fallback was provided.');
109
- const translatedSchemaOrWithFallback = await Promise.resolve(fieldAdapter.translate(adapter.field, {
110
- withFallback: withFallbackFactory(type),
111
- ...validationData
112
- }));
113
- let stringVersion = '';
114
- if (options.shouldAddStringVersion) stringVersion = await fieldAdapter.toString(adapter, adapter.field, validationDataForStringVersion);
115
- // eslint-disable-next-line ts/no-unnecessary-condition
116
- if (translatedSchemaOrWithFallback?.['$$type'] === '$PWithFallback') {
117
- appendRootFallback();
118
- for (const fallback of translatedSchemaOrWithFallback.fallbackFor){
119
- checkIfShouldAppendFallbackAndAppend(fallback);
120
- checkIfShouldUseParserAndAppend(fallback);
121
- }
122
- const [extendedOrNotSchema, extendedOrNotSchemaString] = getExtendedOrNotSchemaAndString(translatedSchemaOrWithFallback.transformedSchema, stringVersion);
123
- appendRequiredFallbacks();
124
- return [
125
- {
126
- transformed: extendedOrNotSchema,
127
- asString: extendedOrNotSchemaString
128
- }
129
- ];
130
- }
131
- const [extendedOrNotSchema, extendedOrNotSchemaString] = getExtendedOrNotSchemaAndString(translatedSchemaOrWithFallback, stringVersion);
132
- return [
133
- {
134
- transformed: extendedOrNotSchema,
135
- asString: extendedOrNotSchemaString
136
- }
137
- ];
138
- }
139
- /**
140
- * This function is used to transform the schema to the adapter. By default it caches the transformed schemas on
141
- * the schema instance. So on subsequent validations we don't need to transform to the schema again.
142
- */ export async function defaultTransformToAdapter(callback, schema, transformedSchemas, options, type) {
143
- const isTransformedSchemasEmpty = Object.keys(transformedSchemas).length <= 0;
144
- if (isTransformedSchemasEmpty) {
145
- const adapterInstanceToUse = // eslint-disable-next-line ts/no-unnecessary-condition
146
- options.schemaAdapter?.['$$type'] === '$PSchemaAdapter' ? options.schemaAdapter : getDefaultAdapter();
147
- schema['__transformedSchemas'][adapterInstanceToUse.name] = {
148
- transformed: false,
149
- adapter: adapterInstanceToUse,
150
- schemas: []
151
- };
152
- }
153
- const schemaAdapterNameToUse = options.schemaAdapter?.name || Object.keys(transformedSchemas)[0];
154
- const isACustomSchemaAdapterAndNotYetDefined = // eslint-disable-next-line ts/no-unnecessary-condition
155
- transformedSchemas[schemaAdapterNameToUse] === undefined && options.schemaAdapter !== undefined;
156
- if (isACustomSchemaAdapterAndNotYetDefined) transformedSchemas[schemaAdapterNameToUse] = {
157
- transformed: false,
158
- adapter: options.schemaAdapter,
159
- schemas: []
160
- };
161
- const shouldTranslate = transformedSchemas[schemaAdapterNameToUse].transformed === false || options.force === true;
162
- if (shouldTranslate) {
163
- const translatedSchemas = await callback(transformedSchemas[schemaAdapterNameToUse].adapter);
164
- transformedSchemas[schemaAdapterNameToUse].schemas = translatedSchemas;
165
- transformedSchemas[schemaAdapterNameToUse].transformed = true;
166
- }
167
- transformedSchemas[schemaAdapterNameToUse].transformed = true;
168
- return transformedSchemas[schemaAdapterNameToUse].schemas;
169
- }
170
- export async function formatErrorFromParseMethod(adapter, fieldAdapter, error, received, schema, path, errorsAsHashedSet) {
171
- const formattedError = await fieldAdapter.formatError(adapter, adapter.field, schema, error);
172
- formattedError.path = Array.isArray(formattedError.path) ? [
173
- ...path,
174
- ...formattedError.path
175
- ] : path;
176
- const formattedErrorAsParseResultError = formattedError;
177
- formattedErrorAsParseResultError.isValid = false;
178
- const sortedError = Object.fromEntries(Object.entries(formattedErrorAsParseResultError).sort(([a], [b])=>a.localeCompare(b)));
179
- const hashedError = JSON.stringify(sortedError);
180
- errorsAsHashedSet.add(JSON.stringify(sortedError));
181
- formattedErrorAsParseResultError.received = received;
182
- return formattedErrorAsParseResultError;
183
- }
184
- /**
185
- * Transform the schema and check if we should add a fallback validation for that schema. This is used for complex
186
- * schemas like Objects, arrays, unions, etc.
187
- */ export async function transformSchemaAndCheckIfShouldBeHandledByFallbackOnComplexSchemas(schema, options) {
188
- const schemaWithProtected = schema;
189
- // This should come first because we will get the fallbacks of the field here.
190
- const transformedData = await schemaWithProtected.__transformToAdapter(options);
191
- // eslint-disable-next-line ts/no-unnecessary-condition
192
- const doesKeyHaveFallback = schemaWithProtected.__rootFallbacksValidator !== undefined;
193
- const doesKeyHaveToInternal = typeof schemaWithProtected.__toInternal === 'function';
194
- const doesKeyHaveToValidate = typeof schemaWithProtected.__toValidate === 'function';
195
- const doesKeyHaveToDefault = typeof schemaWithProtected.__defaultFunction === 'function';
196
- const doesKeyHaveRunBeforeParseAndData = typeof schemaWithProtected.__runBeforeParseAndData === 'function';
197
- const doesKeyHaveParserFallback = schemaWithProtected.__parsers._fallbacks.size > 0;
198
- const shouldAddFallbackValidation = doesKeyHaveFallback || doesKeyHaveToInternal || doesKeyHaveToValidate || doesKeyHaveToDefault || doesKeyHaveParserFallback || doesKeyHaveRunBeforeParseAndData || // eslint-disable-next-line ts/no-unnecessary-condition
199
- transformedData === undefined;
200
- return [
201
- transformedData,
202
- shouldAddFallbackValidation
203
- ];
204
- }
205
- export function shouldRunDataOnComplexSchemas(schema) {
206
- return schema['__parsers'].high.size > 0 || schema['__parsers'].medium.size > 0 || schema['__parsers'].low.size > 0 || schema['__parsers']._fallbacks.size > 0 || typeof schema['__runBeforeParseAndData'] === 'function' || typeof schema['__toRepresentation'] === 'function' || typeof schema['__defaultFunction'] === 'function';
207
- }