@palmares/schemas 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (219) hide show
  1. package/.turbo/turbo-build$colon$watch.log +424 -0
  2. package/.turbo/turbo-build.log +13 -0
  3. package/.turbo/turbo-build:watch.log +26 -0
  4. package/CHANGELOG.md +11 -0
  5. package/LICENSE +21 -0
  6. package/dist/cjs/src/adapter/fields/array.js +157 -0
  7. package/dist/cjs/src/adapter/fields/boolean.js +167 -0
  8. package/dist/cjs/src/adapter/fields/datetime.js +167 -0
  9. package/dist/cjs/src/adapter/fields/index.js +311 -0
  10. package/dist/cjs/src/adapter/fields/number.js +167 -0
  11. package/dist/cjs/src/adapter/fields/object.js +167 -0
  12. package/dist/cjs/src/adapter/fields/string.js +167 -0
  13. package/dist/cjs/src/adapter/fields/union.js +167 -0
  14. package/dist/cjs/src/adapter/index.js +198 -0
  15. package/dist/cjs/src/adapter/types.js +4 -0
  16. package/dist/cjs/src/compile.js +262 -0
  17. package/dist/cjs/src/conf.js +27 -0
  18. package/dist/cjs/src/constants.js +42 -0
  19. package/dist/cjs/src/domain.js +12 -0
  20. package/dist/cjs/src/exceptions.js +168 -0
  21. package/dist/cjs/src/index.js +365 -0
  22. package/dist/cjs/src/model.js +628 -0
  23. package/dist/cjs/src/parsers/convert-from-number.js +20 -0
  24. package/dist/cjs/src/parsers/convert-from-string.js +24 -0
  25. package/dist/cjs/src/parsers/index.js +25 -0
  26. package/dist/cjs/src/schema/array.js +890 -0
  27. package/dist/cjs/src/schema/boolean.js +826 -0
  28. package/dist/cjs/src/schema/datetime.js +778 -0
  29. package/dist/cjs/src/schema/index.js +17 -0
  30. package/dist/cjs/src/schema/number.js +960 -0
  31. package/dist/cjs/src/schema/object.js +999 -0
  32. package/dist/cjs/src/schema/schema.js +1788 -0
  33. package/dist/cjs/src/schema/string.js +948 -0
  34. package/dist/cjs/src/schema/types.js +4 -0
  35. package/dist/cjs/src/schema/union.js +952 -0
  36. package/dist/cjs/src/types.js +4 -0
  37. package/dist/cjs/src/utils.js +627 -0
  38. package/dist/cjs/src/validators/array.js +457 -0
  39. package/dist/cjs/src/validators/boolean.js +199 -0
  40. package/dist/cjs/src/validators/datetime.js +287 -0
  41. package/dist/cjs/src/validators/number.js +403 -0
  42. package/dist/cjs/src/validators/object.js +290 -0
  43. package/dist/cjs/src/validators/schema.js +318 -0
  44. package/dist/cjs/src/validators/string.js +439 -0
  45. package/dist/cjs/src/validators/types.js +4 -0
  46. package/dist/cjs/src/validators/union.js +232 -0
  47. package/dist/cjs/src/validators/utils.js +426 -0
  48. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -0
  49. package/dist/cjs/types/adapter/fields/array.d.ts +20 -0
  50. package/dist/cjs/types/adapter/fields/array.d.ts.map +1 -0
  51. package/dist/cjs/types/adapter/fields/boolean.d.ts +25 -0
  52. package/dist/cjs/types/adapter/fields/boolean.d.ts.map +1 -0
  53. package/dist/cjs/types/adapter/fields/datetime.d.ts +25 -0
  54. package/dist/cjs/types/adapter/fields/datetime.d.ts.map +1 -0
  55. package/dist/cjs/types/adapter/fields/index.d.ts +31 -0
  56. package/dist/cjs/types/adapter/fields/index.d.ts.map +1 -0
  57. package/dist/cjs/types/adapter/fields/number.d.ts +25 -0
  58. package/dist/cjs/types/adapter/fields/number.d.ts.map +1 -0
  59. package/dist/cjs/types/adapter/fields/object.d.ts +25 -0
  60. package/dist/cjs/types/adapter/fields/object.d.ts.map +1 -0
  61. package/dist/cjs/types/adapter/fields/string.d.ts +25 -0
  62. package/dist/cjs/types/adapter/fields/string.d.ts.map +1 -0
  63. package/dist/cjs/types/adapter/fields/union.d.ts +25 -0
  64. package/dist/cjs/types/adapter/fields/union.d.ts.map +1 -0
  65. package/dist/cjs/types/adapter/index.d.ts +25 -0
  66. package/dist/cjs/types/adapter/index.d.ts.map +1 -0
  67. package/dist/cjs/types/adapter/types.d.ts +144 -0
  68. package/dist/cjs/types/adapter/types.d.ts.map +1 -0
  69. package/dist/cjs/types/compile.d.ts +3 -0
  70. package/dist/cjs/types/compile.d.ts.map +1 -0
  71. package/dist/cjs/types/conf.d.ts +16 -0
  72. package/dist/cjs/types/conf.d.ts.map +1 -0
  73. package/dist/cjs/types/constants.d.ts +6 -0
  74. package/dist/cjs/types/constants.d.ts.map +1 -0
  75. package/dist/cjs/types/domain.d.ts +21 -0
  76. package/dist/cjs/types/domain.d.ts.map +1 -0
  77. package/dist/cjs/types/exceptions.d.ts +13 -0
  78. package/dist/cjs/types/exceptions.d.ts.map +1 -0
  79. package/dist/cjs/types/index.d.ts +240 -0
  80. package/dist/cjs/types/index.d.ts.map +1 -0
  81. package/dist/cjs/types/model.d.ts +136 -0
  82. package/dist/cjs/types/model.d.ts.map +1 -0
  83. package/dist/cjs/types/parsers/convert-from-number.d.ts +15 -0
  84. package/dist/cjs/types/parsers/convert-from-number.d.ts.map +1 -0
  85. package/dist/cjs/types/parsers/convert-from-string.d.ts +9 -0
  86. package/dist/cjs/types/parsers/convert-from-string.d.ts.map +1 -0
  87. package/dist/cjs/types/parsers/index.d.ts +3 -0
  88. package/dist/cjs/types/parsers/index.d.ts.map +1 -0
  89. package/dist/cjs/types/schema/array.d.ts +429 -0
  90. package/dist/cjs/types/schema/array.d.ts.map +1 -0
  91. package/dist/cjs/types/schema/boolean.d.ts +501 -0
  92. package/dist/cjs/types/schema/boolean.d.ts.map +1 -0
  93. package/dist/cjs/types/schema/datetime.d.ts +474 -0
  94. package/dist/cjs/types/schema/datetime.d.ts.map +1 -0
  95. package/dist/cjs/types/schema/index.d.ts +4 -0
  96. package/dist/cjs/types/schema/index.d.ts.map +1 -0
  97. package/dist/cjs/types/schema/number.d.ts +667 -0
  98. package/dist/cjs/types/schema/number.d.ts.map +1 -0
  99. package/dist/cjs/types/schema/object.d.ts +450 -0
  100. package/dist/cjs/types/schema/object.d.ts.map +1 -0
  101. package/dist/cjs/types/schema/schema.d.ts +646 -0
  102. package/dist/cjs/types/schema/schema.d.ts.map +1 -0
  103. package/dist/cjs/types/schema/string.d.ts +606 -0
  104. package/dist/cjs/types/schema/string.d.ts.map +1 -0
  105. package/dist/cjs/types/schema/types.d.ts +70 -0
  106. package/dist/cjs/types/schema/types.d.ts.map +1 -0
  107. package/dist/cjs/types/schema/union.d.ts +388 -0
  108. package/dist/cjs/types/schema/union.d.ts.map +1 -0
  109. package/dist/cjs/types/types.d.ts +11 -0
  110. package/dist/cjs/types/types.d.ts.map +1 -0
  111. package/dist/cjs/types/utils.d.ts +79 -0
  112. package/dist/cjs/types/utils.d.ts.map +1 -0
  113. package/dist/cjs/types/validators/array.d.ts +8 -0
  114. package/dist/cjs/types/validators/array.d.ts.map +1 -0
  115. package/dist/cjs/types/validators/boolean.d.ts +4 -0
  116. package/dist/cjs/types/validators/boolean.d.ts.map +1 -0
  117. package/dist/cjs/types/validators/datetime.d.ts +7 -0
  118. package/dist/cjs/types/validators/datetime.d.ts.map +1 -0
  119. package/dist/cjs/types/validators/number.d.ts +10 -0
  120. package/dist/cjs/types/validators/number.d.ts.map +1 -0
  121. package/dist/cjs/types/validators/object.d.ts +6 -0
  122. package/dist/cjs/types/validators/object.d.ts.map +1 -0
  123. package/dist/cjs/types/validators/schema.d.ts +10 -0
  124. package/dist/cjs/types/validators/schema.d.ts.map +1 -0
  125. package/dist/cjs/types/validators/string.d.ts +12 -0
  126. package/dist/cjs/types/validators/string.d.ts.map +1 -0
  127. package/dist/cjs/types/validators/types.d.ts +2 -0
  128. package/dist/cjs/types/validators/types.d.ts.map +1 -0
  129. package/dist/cjs/types/validators/union.d.ts +4 -0
  130. package/dist/cjs/types/validators/union.d.ts.map +1 -0
  131. package/dist/cjs/types/validators/utils.d.ts +83 -0
  132. package/dist/cjs/types/validators/utils.d.ts.map +1 -0
  133. package/dist/esm/src/adapter/fields/array.js +13 -0
  134. package/dist/esm/src/adapter/fields/boolean.js +20 -0
  135. package/dist/esm/src/adapter/fields/datetime.js +20 -0
  136. package/dist/esm/src/adapter/fields/index.js +37 -0
  137. package/dist/esm/src/adapter/fields/number.js +20 -0
  138. package/dist/esm/src/adapter/fields/object.js +20 -0
  139. package/dist/esm/src/adapter/fields/string.js +20 -0
  140. package/dist/esm/src/adapter/fields/union.js +20 -0
  141. package/dist/esm/src/adapter/index.js +18 -0
  142. package/dist/esm/src/adapter/types.js +1 -0
  143. package/dist/esm/src/compile.js +10 -0
  144. package/dist/esm/src/conf.js +19 -0
  145. package/dist/esm/src/constants.js +5 -0
  146. package/dist/esm/src/domain.js +2 -0
  147. package/dist/esm/src/exceptions.js +15 -0
  148. package/dist/esm/src/index.js +160 -0
  149. package/dist/esm/src/model.js +255 -0
  150. package/dist/esm/src/parsers/convert-from-number.js +8 -0
  151. package/dist/esm/src/parsers/convert-from-string.js +14 -0
  152. package/dist/esm/src/parsers/index.js +2 -0
  153. package/dist/esm/src/schema/array.js +403 -0
  154. package/dist/esm/src/schema/boolean.js +465 -0
  155. package/dist/esm/src/schema/datetime.js +423 -0
  156. package/dist/esm/src/schema/index.js +3 -0
  157. package/dist/esm/src/schema/number.js +592 -0
  158. package/dist/esm/src/schema/object.js +464 -0
  159. package/dist/esm/src/schema/schema.js +728 -0
  160. package/dist/esm/src/schema/string.js +579 -0
  161. package/dist/esm/src/schema/types.js +1 -0
  162. package/dist/esm/src/schema/union.js +388 -0
  163. package/dist/esm/src/types.js +1 -0
  164. package/dist/esm/src/utils.js +175 -0
  165. package/dist/esm/src/validators/array.js +135 -0
  166. package/dist/esm/src/validators/boolean.js +35 -0
  167. package/dist/esm/src/validators/datetime.js +85 -0
  168. package/dist/esm/src/validators/number.js +162 -0
  169. package/dist/esm/src/validators/object.js +38 -0
  170. package/dist/esm/src/validators/schema.js +114 -0
  171. package/dist/esm/src/validators/string.js +174 -0
  172. package/dist/esm/src/validators/types.js +1 -0
  173. package/dist/esm/src/validators/union.js +38 -0
  174. package/dist/esm/src/validators/utils.js +120 -0
  175. package/package.json +48 -0
  176. package/src/adapter/fields/array.ts +31 -0
  177. package/src/adapter/fields/boolean.ts +48 -0
  178. package/src/adapter/fields/datetime.ts +49 -0
  179. package/src/adapter/fields/index.ts +72 -0
  180. package/src/adapter/fields/number.ts +49 -0
  181. package/src/adapter/fields/object.ts +49 -0
  182. package/src/adapter/fields/string.ts +49 -0
  183. package/src/adapter/fields/union.ts +49 -0
  184. package/src/adapter/index.ts +34 -0
  185. package/src/adapter/types.ts +261 -0
  186. package/src/compile.ts +14 -0
  187. package/src/conf.ts +27 -0
  188. package/src/constants.ts +9 -0
  189. package/src/domain.ts +3 -0
  190. package/src/exceptions.ts +17 -0
  191. package/src/index.ts +338 -0
  192. package/src/model.ts +501 -0
  193. package/src/parsers/convert-from-number.ts +13 -0
  194. package/src/parsers/convert-from-string.ts +19 -0
  195. package/src/parsers/index.ts +2 -0
  196. package/src/schema/array.ts +633 -0
  197. package/src/schema/boolean.ts +700 -0
  198. package/src/schema/datetime.ts +613 -0
  199. package/src/schema/index.ts +5 -0
  200. package/src/schema/number.ts +885 -0
  201. package/src/schema/object.ts +699 -0
  202. package/src/schema/schema.ts +1093 -0
  203. package/src/schema/string.ts +807 -0
  204. package/src/schema/types.ts +126 -0
  205. package/src/schema/union.ts +596 -0
  206. package/src/types.ts +13 -0
  207. package/src/utils.ts +322 -0
  208. package/src/validators/array.ts +164 -0
  209. package/src/validators/boolean.ts +46 -0
  210. package/src/validators/datetime.ts +113 -0
  211. package/src/validators/number.ts +188 -0
  212. package/src/validators/object.ts +55 -0
  213. package/src/validators/schema.ts +134 -0
  214. package/src/validators/string.ts +215 -0
  215. package/src/validators/types.ts +1 -0
  216. package/src/validators/union.ts +52 -0
  217. package/src/validators/utils.ts +200 -0
  218. package/tsconfig.json +9 -0
  219. package/tsconfig.types.json +10 -0
@@ -0,0 +1,188 @@
1
+ import type NumberSchema from '../schema/number';
2
+ import type Schema from '../schema/schema';
3
+ import type { ValidationFallbackReturnType } from '../schema/types';
4
+
5
+ export function numberValidation(): ValidationFallbackReturnType {
6
+ return {
7
+ type: 'medium',
8
+ // eslint-disable-next-line ts/require-await
9
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
10
+ return {
11
+ parsed: value,
12
+ errors: [
13
+ {
14
+ isValid: typeof value === 'number',
15
+ code: 'number',
16
+ // eslint-disable-next-line ts/no-unnecessary-condition
17
+ path: path || [],
18
+ message: 'The value must be a number. Received: ' + typeof value,
19
+ },
20
+ ],
21
+ };
22
+ },
23
+ };
24
+ }
25
+
26
+ export function max(args: NumberSchema['__max']): ValidationFallbackReturnType {
27
+ return {
28
+ type: 'low',
29
+ // eslint-disable-next-line ts/require-await
30
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
31
+ if (args.inclusive)
32
+ return {
33
+ parsed: value,
34
+ errors: [
35
+ {
36
+ isValid: value <= args.value,
37
+ code: 'max',
38
+ // eslint-disable-next-line ts/no-unnecessary-condition
39
+ path: path || [],
40
+ message: args.message,
41
+ },
42
+ ],
43
+ };
44
+
45
+ return {
46
+ parsed: value,
47
+ errors: [
48
+ {
49
+ isValid: value < args.value,
50
+ code: 'max',
51
+ // eslint-disable-next-line ts/no-unnecessary-condition
52
+ path: path || [],
53
+ message: args.message,
54
+ },
55
+ ],
56
+ };
57
+ },
58
+ };
59
+ }
60
+
61
+ export function min(args: NumberSchema['__min']): ValidationFallbackReturnType {
62
+ return {
63
+ type: 'low',
64
+ // eslint-disable-next-line ts/require-await
65
+ callback: async (value: any, path?: (string | number)[]) => {
66
+ if (args.inclusive)
67
+ return {
68
+ parsed: value,
69
+ errors: [
70
+ {
71
+ isValid: value >= args.value,
72
+ message: args.message,
73
+ code: 'min',
74
+ path: path || [],
75
+ },
76
+ ],
77
+ };
78
+
79
+ return {
80
+ parsed: value,
81
+ errors: [
82
+ {
83
+ isValid: value > args.value,
84
+ message: args.message,
85
+ code: 'min',
86
+ path: path || [],
87
+ },
88
+ ],
89
+ };
90
+ },
91
+ };
92
+ }
93
+
94
+ export function negative(args: NumberSchema['__allowNegative']): ValidationFallbackReturnType {
95
+ return {
96
+ type: 'low',
97
+ // eslint-disable-next-line ts/require-await
98
+ callback: async (value: any, path?: (string | number)[]) => {
99
+ const isValid = args.allowZero ? value < 0 : value <= 0;
100
+
101
+ return {
102
+ parsed: value,
103
+ errors: isValid
104
+ ? []
105
+ : [
106
+ {
107
+ isValid: isValid,
108
+ message: args.message,
109
+ code: 'negative',
110
+ path: path || [],
111
+ },
112
+ ],
113
+ };
114
+ },
115
+ };
116
+ }
117
+
118
+ export function positive(args: NumberSchema['__allowPositive']): ValidationFallbackReturnType {
119
+ return {
120
+ type: 'low',
121
+ // eslint-disable-next-line ts/require-await
122
+ callback: async (value: any, path?: (string | number)[]) => {
123
+ const isValid = args.allowZero ? value > 0 : value >= 0;
124
+
125
+ return {
126
+ parsed: value,
127
+ errors: isValid
128
+ ? []
129
+ : [
130
+ {
131
+ isValid: isValid,
132
+ message: args.message,
133
+ code: 'positive',
134
+ path: path || [],
135
+ },
136
+ ],
137
+ };
138
+ },
139
+ };
140
+ }
141
+
142
+ export function maxDigits(args: NumberSchema['__maxDigits']): ValidationFallbackReturnType {
143
+ return {
144
+ type: 'low',
145
+ // eslint-disable-next-line ts/require-await
146
+ callback: async (value: any, path?: (string | number)[]) => {
147
+ const isValid = value.toString().replace('.', '').length <= args.value;
148
+
149
+ return {
150
+ parsed: value,
151
+ errors: isValid
152
+ ? []
153
+ : [
154
+ {
155
+ isValid: isValid,
156
+ message: args.message,
157
+ code: 'maxDigits',
158
+ path: path || [],
159
+ },
160
+ ],
161
+ };
162
+ },
163
+ };
164
+ }
165
+
166
+ export function decimalPlaces(args: NumberSchema['__decimalPlaces']): ValidationFallbackReturnType {
167
+ return {
168
+ type: 'low',
169
+ // eslint-disable-next-line ts/require-await
170
+ callback: async (value: any, path?: (string | number)[]) => {
171
+ const isValid = value.toString().split('.')[1]?.length <= args.value;
172
+
173
+ return {
174
+ parsed: value,
175
+ errors: isValid
176
+ ? []
177
+ : [
178
+ {
179
+ isValid: isValid,
180
+ message: args.message,
181
+ code: 'decimalPlaces',
182
+ path: path || [],
183
+ },
184
+ ],
185
+ };
186
+ },
187
+ };
188
+ }
@@ -0,0 +1,55 @@
1
+ import type Schema from '../schema/schema';
2
+ import type { ValidationFallbackCallbackReturnType, ValidationFallbackReturnType } from '../schema/types';
3
+
4
+ export function objectValidation(keysToFallback: { [key: string]: Schema }): ValidationFallbackReturnType {
5
+ return {
6
+ type: 'low',
7
+ callback: async (value: any, path: (string | number)[], options: Parameters<Schema['__transformToAdapter']>[0]) => {
8
+ const isNotAnObject = typeof value !== 'object' && Array.isArray(value) === false && value !== null;
9
+
10
+ if (isNotAnObject)
11
+ return {
12
+ parsed: value,
13
+ preventChildValidation: true,
14
+ errors: [
15
+ {
16
+ isValid: false,
17
+ code: 'object',
18
+ // eslint-disable-next-line ts/no-unnecessary-condition
19
+ path: path || [],
20
+ message: 'The value must be an object. Received: ' + typeof value,
21
+ },
22
+ ],
23
+ };
24
+
25
+ const errors: { [key: string]: ValidationFallbackCallbackReturnType['errors'] } = {};
26
+ const toValidateEntries = Object.entries(keysToFallback);
27
+
28
+ await Promise.all(
29
+ toValidateEntries.map(async ([key, schema]) => {
30
+ const schemaWithProtected = schema as Schema & {
31
+ __parse: Schema['__parse'];
32
+ __toInternal: Schema['__toInternal'];
33
+ };
34
+ const { parsed, errors: parseErrors } = await schemaWithProtected.__parse(
35
+ value[key],
36
+ [...path, key],
37
+ options
38
+ );
39
+ if (Array.isArray(parseErrors) && parseErrors.length > 0) errors[key] = parseErrors;
40
+ else value[key] = parsed;
41
+
42
+ // We append the toInternalToBubbleUp to the parent toInternalToBubbleUp
43
+
44
+ if (schemaWithProtected.__toInternal && options.toInternalToBubbleUp)
45
+ options.toInternalToBubbleUp.push(async () => (value[key] = await (schema as any).__toInternal(parsed)));
46
+ })
47
+ );
48
+
49
+ return {
50
+ parsed: value,
51
+ errors: Object.values(errors).flat(),
52
+ };
53
+ },
54
+ };
55
+ }
@@ -0,0 +1,134 @@
1
+ import type { ErrorCodes } from '../adapter/types';
2
+ import type BooleanSchema from '../schema/boolean';
3
+ import type NumberSchema from '../schema/number';
4
+ import type Schema from '../schema/schema';
5
+ import type StringSchema from '../schema/string';
6
+ import type { ValidationFallbackReturnType } from '../schema/types';
7
+
8
+ export function optional(args: Schema['__optional']): ValidationFallbackReturnType {
9
+ return {
10
+ type: 'high',
11
+ // eslint-disable-next-line ts/require-await
12
+ callback: async (value: any, path: (string | number)[]) => {
13
+ if (value === undefined) {
14
+ if (args.allow === true)
15
+ return {
16
+ parsed: value,
17
+ errors: [],
18
+ preventChildValidation: true,
19
+ };
20
+ return {
21
+ parsed: value,
22
+ errors: [
23
+ {
24
+ isValid: false,
25
+ message: args.message,
26
+ code: 'required' as ErrorCodes,
27
+ // eslint-disable-next-line ts/no-unnecessary-condition
28
+ path: path || [],
29
+ },
30
+ ],
31
+ preventChildValidation: true,
32
+ };
33
+ }
34
+
35
+ return {
36
+ parsed: value,
37
+ errors: [],
38
+ preventChildValidation: false,
39
+ };
40
+ },
41
+ };
42
+ }
43
+
44
+ export function nullable(args: Schema['__nullable']): ValidationFallbackReturnType {
45
+ return {
46
+ type: 'high',
47
+ // eslint-disable-next-line ts/require-await
48
+ callback: async (value: any, path: (string | number)[]) => {
49
+ if (value === null) {
50
+ if (args.allow === true)
51
+ return {
52
+ parsed: value,
53
+ errors: [],
54
+ preventChildValidation: true,
55
+ };
56
+ return {
57
+ parsed: value,
58
+ errors: [
59
+ {
60
+ isValid: false,
61
+ message: args.message,
62
+ code: 'cannot_be_null' as ErrorCodes,
63
+ // eslint-disable-next-line ts/no-unnecessary-condition
64
+ path: path || [],
65
+ },
66
+ ],
67
+ preventChildValidation: true,
68
+ };
69
+ }
70
+
71
+ return {
72
+ parsed: value,
73
+ errors: [],
74
+ preventChildValidation: false,
75
+ };
76
+ },
77
+ };
78
+ }
79
+
80
+ export function checkType(args: Schema['__type']): ValidationFallbackReturnType {
81
+ return {
82
+ type: 'medium',
83
+ // eslint-disable-next-line ts/require-await
84
+ callback: async (value: any, path: (string | number)[]) => {
85
+ if (args.check(value))
86
+ return {
87
+ parsed: value,
88
+ errors: [],
89
+ preventChildValidation: false,
90
+ };
91
+
92
+ return {
93
+ parsed: value,
94
+ errors: [
95
+ {
96
+ isValid: false,
97
+ message: args.message,
98
+ code: 'invalid_type' as ErrorCodes,
99
+ // eslint-disable-next-line ts/no-unnecessary-condition
100
+ path: path || [],
101
+ },
102
+ ],
103
+ preventChildValidation: true,
104
+ };
105
+ },
106
+ };
107
+ }
108
+
109
+ export function is(
110
+ args: BooleanSchema['__is'] | NumberSchema['__is'] | StringSchema['__is']
111
+ ): ValidationFallbackReturnType {
112
+ return {
113
+ type: 'medium',
114
+ // eslint-disable-next-line ts/require-await
115
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
116
+ const isValid = Array.isArray(args.value) ? args.value.includes(value as never) : value === args.value;
117
+ return {
118
+ parsed: value,
119
+ errors: isValid
120
+ ? []
121
+ : [
122
+ {
123
+ isValid: false,
124
+ code: 'is',
125
+ // eslint-disable-next-line ts/no-unnecessary-condition
126
+ path: path || [],
127
+ message: 'Value is not a boolean',
128
+ },
129
+ ],
130
+ preventChildValidation: true,
131
+ };
132
+ },
133
+ };
134
+ }
@@ -0,0 +1,215 @@
1
+ import type Schema from '../schema/schema';
2
+ import type StringSchema from '../schema/string';
3
+ import { type ValidationFallbackReturnType } from '../schema/types';
4
+
5
+ export function stringValidation(): ValidationFallbackReturnType {
6
+ return {
7
+ type: 'medium',
8
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
9
+ return {
10
+ parsed: value,
11
+ errors:
12
+ typeof value === 'string'
13
+ ? []
14
+ : [
15
+ {
16
+ isValid: typeof value === 'string',
17
+ code: 'string',
18
+ path: path || [],
19
+ message: 'The value must be a string. Received: ' + typeof value,
20
+ },
21
+ ],
22
+ };
23
+ },
24
+ };
25
+ }
26
+
27
+ export function maxLength(args: StringSchema['__maxLength']): ValidationFallbackReturnType {
28
+ return {
29
+ type: 'low',
30
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
31
+ const isValid = args.inclusive ? value.length <= args.value : value.length < args.value;
32
+
33
+ return {
34
+ parsed: value,
35
+ errors: isValid
36
+ ? []
37
+ : [
38
+ {
39
+ isValid: false,
40
+ code: 'maxLength',
41
+ path: path || [],
42
+ message: args.message,
43
+ },
44
+ ],
45
+ };
46
+ },
47
+ };
48
+ }
49
+
50
+ export function minLength(args: StringSchema['__maxLength']): ValidationFallbackReturnType {
51
+ return {
52
+ type: 'low',
53
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
54
+ const isValid = args.inclusive ? value.length >= args.value : value.length > args.value;
55
+
56
+ return {
57
+ parsed: value,
58
+ errors: isValid
59
+ ? []
60
+ : [
61
+ {
62
+ isValid: false,
63
+ code: 'minLength',
64
+ path: path || [],
65
+ message: args.message,
66
+ },
67
+ ],
68
+ };
69
+ },
70
+ };
71
+ }
72
+
73
+ export function endsWith(args: StringSchema['__endsWith']): ValidationFallbackReturnType {
74
+ return {
75
+ type: 'low',
76
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
77
+ const isValid = value.endsWith(args.value);
78
+
79
+ return {
80
+ parsed: value,
81
+ errors: isValid
82
+ ? []
83
+ : [
84
+ {
85
+ isValid: false,
86
+ code: 'endsWith',
87
+ path: path || [],
88
+ message: args.message,
89
+ },
90
+ ],
91
+ };
92
+ },
93
+ };
94
+ }
95
+
96
+ export function startsWith(args: StringSchema['__startsWith']): ValidationFallbackReturnType {
97
+ return {
98
+ type: 'low',
99
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
100
+ const isValid = value.startsWith(args.value);
101
+
102
+ return {
103
+ parsed: value,
104
+ errors: isValid
105
+ ? []
106
+ : [
107
+ {
108
+ isValid: false,
109
+ code: 'startsWith',
110
+ path: path || [],
111
+ message: args.message,
112
+ },
113
+ ],
114
+ };
115
+ },
116
+ };
117
+ }
118
+
119
+ export function includes(args: StringSchema['__includes']): ValidationFallbackReturnType {
120
+ return {
121
+ type: 'low',
122
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
123
+ const isValid = value.includes(args.value);
124
+
125
+ return {
126
+ parsed: value,
127
+ errors: isValid
128
+ ? []
129
+ : [
130
+ {
131
+ isValid: false,
132
+ code: 'includes',
133
+ path: path || [],
134
+ message: args.message,
135
+ },
136
+ ],
137
+ };
138
+ },
139
+ };
140
+ }
141
+
142
+ export function regex(args: StringSchema['__regex']): ValidationFallbackReturnType {
143
+ return {
144
+ type: 'low',
145
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
146
+ const isValid = args.value.test(value);
147
+
148
+ return {
149
+ parsed: value,
150
+ errors: isValid
151
+ ? []
152
+ : [
153
+ {
154
+ isValid: false,
155
+ code: 'regex',
156
+ path: path || [],
157
+ message: args.message,
158
+ },
159
+ ],
160
+ };
161
+ },
162
+ };
163
+ }
164
+
165
+ export function uuid(args: StringSchema['__uuid']): ValidationFallbackReturnType {
166
+ // const uuidRegex =
167
+ // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
168
+ const uuidRegex =
169
+ /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
170
+ return {
171
+ type: 'low',
172
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
173
+ const isValid = uuidRegex.test(value);
174
+
175
+ return {
176
+ parsed: value,
177
+ errors: isValid
178
+ ? []
179
+ : [
180
+ {
181
+ isValid: false,
182
+ code: 'uuid',
183
+ path: path || [],
184
+ message: args.message,
185
+ },
186
+ ],
187
+ };
188
+ }
189
+ }
190
+ }
191
+
192
+ export function email(args: StringSchema['__email']): ValidationFallbackReturnType {
193
+ const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
194
+ return {
195
+ type: 'low',
196
+ callback: async (value: any, path: (string | number)[], _options: Parameters<Schema['__transformToAdapter']>[0]) => {
197
+ const isValid = emailRegex.test(value);
198
+
199
+ return {
200
+ parsed: value,
201
+ errors: isValid
202
+ ? []
203
+ : [
204
+ {
205
+ isValid: false,
206
+ code: 'email',
207
+ path: path || [],
208
+ message: args.message,
209
+ },
210
+ ],
211
+ };
212
+ }
213
+ }
214
+ }
215
+
@@ -0,0 +1 @@
1
+ export type ValidatorTypes = 'low' | 'medium' | 'high';
@@ -0,0 +1,52 @@
1
+ import type Schema from '../schema/schema';
2
+ import type { ValidationFallbackReturnType } from '../schema/types';
3
+
4
+ export function unionValidation(
5
+ schemas: readonly [Schema<any, any>, Schema<any, any>, ...Schema<any, any>[]]
6
+ ): ValidationFallbackReturnType {
7
+ return {
8
+ type: 'high',
9
+ callback: async (value, path, options) => {
10
+ const parsedValues: Awaited<ReturnType<Schema['__parse']>> = {
11
+ parsed: value,
12
+ errors: [],
13
+ };
14
+ const startingToInternalBubbleUpLength = options.toInternalToBubbleUp?.length || 0;
15
+
16
+ // eslint-disable-next-line ts/prefer-for-of
17
+ for (let i = 0; i < schemas.length; i++) {
18
+ const schemaWithProtected = schemas[i] as Schema & {
19
+ __parse: Schema['__parse'];
20
+ __toInternal: Schema['__toInternal'];
21
+ };
22
+ const parsedData = await schemaWithProtected.__parse(value, path, options);
23
+ parsedValues.parsed = parsedData.parsed;
24
+ if (Array.isArray(parsedData.errors))
25
+ if (Array.isArray(parsedValues.errors)) parsedValues.errors.push(...parsedData.errors);
26
+ else parsedValues.errors = parsedData.errors;
27
+
28
+ const hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors =
29
+ // eslint-disable-next-line ts/no-unnecessary-condition
30
+ parsedData.errors === undefined || (parsedData.errors || []).length === 0;
31
+ if (hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors) {
32
+ return {
33
+ parsed: parsedValues.parsed,
34
+ errors: [],
35
+ };
36
+ } else if (startingToInternalBubbleUpLength < (options.toInternalToBubbleUp?.length || 0)) {
37
+ // If there is a new toInternalToBubbleUp we should remove the ones that we added since this is not a valid schema,
38
+ // we shouldn't be calling the `toInternal` on that schemas.
39
+ const numberOfElementsToRemove =
40
+ (options.toInternalToBubbleUp?.length || 0) - startingToInternalBubbleUpLength;
41
+ options.toInternalToBubbleUp?.splice(startingToInternalBubbleUpLength, numberOfElementsToRemove);
42
+ }
43
+ }
44
+
45
+ return {
46
+ parsed: parsedValues.parsed,
47
+ // eslint-disable-next-line ts/no-unnecessary-condition
48
+ errors: parsedValues.errors ? parsedValues.errors : [],
49
+ };
50
+ },
51
+ };
52
+ }