@palmares/schemas 0.1.20 → 0.1.22

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 (111) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/cjs/src/adapter/index.js +2 -0
  3. package/dist/cjs/src/conf.js +3 -4
  4. package/dist/cjs/src/model.js +34 -38
  5. package/dist/cjs/src/schema/array.js +1 -0
  6. package/dist/cjs/src/schema/object.js +2 -8
  7. package/dist/cjs/src/schema/schema.js +1 -0
  8. package/dist/cjs/src/utils.js +12 -14
  9. package/dist/cjs/src/validators/utils.js +1 -0
  10. package/dist/cjs/tsconfig.types.tsbuildinfo +1 -1
  11. package/dist/cjs/types/adapter/index.d.ts +2 -0
  12. package/dist/cjs/types/adapter/index.d.ts.map +1 -1
  13. package/dist/cjs/types/conf.d.ts +3 -0
  14. package/dist/cjs/types/conf.d.ts.map +1 -1
  15. package/dist/cjs/types/domain.d.ts +3 -0
  16. package/dist/cjs/types/domain.d.ts.map +1 -1
  17. package/dist/cjs/types/model.d.ts +2 -2
  18. package/dist/cjs/types/model.d.ts.map +1 -1
  19. package/dist/cjs/types/schema/array.d.ts +1 -0
  20. package/dist/cjs/types/schema/array.d.ts.map +1 -1
  21. package/dist/cjs/types/schema/object.d.ts.map +1 -1
  22. package/dist/cjs/types/schema/schema.d.ts +1 -0
  23. package/dist/cjs/types/schema/schema.d.ts.map +1 -1
  24. package/dist/cjs/types/utils.d.ts +2 -1
  25. package/dist/cjs/types/utils.d.ts.map +1 -1
  26. package/dist/cjs/types/validators/utils.d.ts +1 -0
  27. package/dist/cjs/types/validators/utils.d.ts.map +1 -1
  28. package/dist/esm/src/adapter/index.js +2 -0
  29. package/dist/esm/src/conf.js +3 -4
  30. package/dist/esm/src/model.js +32 -30
  31. package/dist/esm/src/schema/array.js +1 -0
  32. package/dist/esm/src/schema/object.js +2 -1
  33. package/dist/esm/src/schema/schema.js +1 -0
  34. package/dist/esm/src/utils.js +9 -6
  35. package/dist/esm/src/validators/utils.js +1 -0
  36. package/package.json +11 -5
  37. package/.turbo/turbo-build$colon$watch.log +0 -24
  38. package/.turbo/turbo-build.log +0 -4
  39. package/.turbo/turbo-build:watch.log +0 -26
  40. package/__tests__/.drizzle/migrations/0000_skinny_harrier.sql +0 -22
  41. package/__tests__/.drizzle/migrations/meta/0000_snapshot.json +0 -156
  42. package/__tests__/.drizzle/migrations/meta/_journal.json +0 -13
  43. package/__tests__/.drizzle/schema.ts +0 -35
  44. package/__tests__/drizzle.config.ts +0 -11
  45. package/__tests__/eslint.config.js +0 -10
  46. package/__tests__/manage.ts +0 -5
  47. package/__tests__/node_modules/.bin/drizzle-kit +0 -17
  48. package/__tests__/node_modules/.bin/tsc +0 -17
  49. package/__tests__/node_modules/.bin/tsserver +0 -17
  50. package/__tests__/node_modules/.bin/tsx +0 -17
  51. package/__tests__/package.json +0 -34
  52. package/__tests__/sqlite.db +0 -0
  53. package/__tests__/src/core/array.test.ts +0 -131
  54. package/__tests__/src/core/boolean.test.ts +0 -66
  55. package/__tests__/src/core/datetime.test.ts +0 -102
  56. package/__tests__/src/core/index.ts +0 -35
  57. package/__tests__/src/core/model.test.ts +0 -260
  58. package/__tests__/src/core/models.ts +0 -50
  59. package/__tests__/src/core/numbers.test.ts +0 -177
  60. package/__tests__/src/core/object.test.ts +0 -218
  61. package/__tests__/src/core/string.test.ts +0 -222
  62. package/__tests__/src/core/test.test.ts +0 -59
  63. package/__tests__/src/core/types.test.ts +0 -97
  64. package/__tests__/src/core/union.test.ts +0 -99
  65. package/__tests__/src/settings.ts +0 -69
  66. package/__tests__/tsconfig.json +0 -11
  67. package/src/adapter/fields/array.ts +0 -31
  68. package/src/adapter/fields/boolean.ts +0 -43
  69. package/src/adapter/fields/datetime.ts +0 -43
  70. package/src/adapter/fields/index.ts +0 -72
  71. package/src/adapter/fields/number.ts +0 -43
  72. package/src/adapter/fields/object.ts +0 -52
  73. package/src/adapter/fields/string.ts +0 -43
  74. package/src/adapter/fields/union.ts +0 -43
  75. package/src/adapter/index.ts +0 -35
  76. package/src/adapter/types.ts +0 -276
  77. package/src/compile.ts +0 -14
  78. package/src/conf.ts +0 -27
  79. package/src/constants.ts +0 -7
  80. package/src/domain.ts +0 -15
  81. package/src/exceptions.ts +0 -17
  82. package/src/index.ts +0 -318
  83. package/src/middleware.ts +0 -52
  84. package/src/model.ts +0 -507
  85. package/src/parsers/convert-from-number.ts +0 -13
  86. package/src/parsers/convert-from-string.ts +0 -19
  87. package/src/parsers/index.ts +0 -2
  88. package/src/schema/array.ts +0 -824
  89. package/src/schema/boolean.ts +0 -792
  90. package/src/schema/datetime.ts +0 -704
  91. package/src/schema/index.ts +0 -5
  92. package/src/schema/number.ts +0 -929
  93. package/src/schema/object.ts +0 -798
  94. package/src/schema/schema.ts +0 -1178
  95. package/src/schema/string.ts +0 -941
  96. package/src/schema/types.ts +0 -154
  97. package/src/schema/union.ts +0 -724
  98. package/src/types.ts +0 -66
  99. package/src/utils.ts +0 -383
  100. package/src/validators/array.ts +0 -183
  101. package/src/validators/boolean.ts +0 -52
  102. package/src/validators/datetime.ts +0 -121
  103. package/src/validators/number.ts +0 -178
  104. package/src/validators/object.ts +0 -56
  105. package/src/validators/schema.ts +0 -142
  106. package/src/validators/string.ts +0 -278
  107. package/src/validators/types.ts +0 -1
  108. package/src/validators/union.ts +0 -52
  109. package/src/validators/utils.ts +0 -225
  110. package/tsconfig.json +0 -9
  111. package/tsconfig.types.json +0 -10
@@ -1,278 +0,0 @@
1
- import { type ValidationFallbackReturnType } from '../schema/types';
2
-
3
- import type { Schema } from '../schema/schema';
4
- import type { StringSchema } from '../schema/string';
5
-
6
- export function stringValidation(): ValidationFallbackReturnType {
7
- return {
8
- name: 'string',
9
- type: 'medium',
10
- // eslint-disable-next-line ts/require-await
11
- callback: async (
12
- value: any,
13
- path: (string | number)[],
14
- _options: Parameters<Schema['__transformToAdapter']>[0]
15
- ) => {
16
- return {
17
- parsed: value,
18
- errors:
19
- typeof value === 'string'
20
- ? []
21
- : [
22
- {
23
- isValid: typeof value === 'string',
24
- code: 'string',
25
- // eslint-disable-next-line ts/no-unnecessary-condition
26
- path: path || [],
27
- message: 'The value must be a string. Received: ' + typeof value
28
- }
29
- ]
30
- };
31
- }
32
- };
33
- }
34
-
35
- export function maxLength(args: NonNullable<StringSchema['__maxLength']>): ValidationFallbackReturnType {
36
- return {
37
- name: 'maxLength',
38
- type: 'low',
39
- // eslint-disable-next-line ts/require-await
40
- callback: async (
41
- value: any,
42
- path: (string | number)[],
43
- _options: Parameters<Schema['__transformToAdapter']>[0]
44
- ) => {
45
- const isValid = value.length <= args.value;
46
-
47
- return {
48
- parsed: value,
49
- errors: isValid
50
- ? []
51
- : [
52
- {
53
- isValid: false,
54
- code: 'maxLength',
55
- // eslint-disable-next-line ts/no-unnecessary-condition
56
- path: path || [],
57
- message: args.message
58
- }
59
- ]
60
- };
61
- }
62
- };
63
- }
64
-
65
- export function minLength(args: NonNullable<StringSchema['__maxLength']>): ValidationFallbackReturnType {
66
- return {
67
- name: 'minLength',
68
- type: 'low',
69
- // eslint-disable-next-line ts/require-await
70
- callback: async (
71
- value: any,
72
- path: (string | number)[],
73
- _options: Parameters<Schema['__transformToAdapter']>[0]
74
- ) => {
75
- const isValid = value.length >= args.value;
76
-
77
- return {
78
- parsed: value,
79
- errors: isValid
80
- ? []
81
- : [
82
- {
83
- isValid: false,
84
- code: 'minLength',
85
- // eslint-disable-next-line ts/no-unnecessary-condition
86
- path: path || [],
87
- message: args.message
88
- }
89
- ]
90
- };
91
- }
92
- };
93
- }
94
-
95
- export function endsWith(args: NonNullable<StringSchema['__endsWith']>): ValidationFallbackReturnType {
96
- return {
97
- name: 'endsWith',
98
- type: 'low',
99
- // eslint-disable-next-line ts/require-await
100
- callback: async (
101
- value: any,
102
- path: (string | number)[],
103
- _options: Parameters<Schema['__transformToAdapter']>[0]
104
- ) => {
105
- const isValid = value.endsWith(args.value);
106
-
107
- return {
108
- parsed: value,
109
- errors: isValid
110
- ? []
111
- : [
112
- {
113
- isValid: false,
114
- code: 'endsWith',
115
- // eslint-disable-next-line ts/no-unnecessary-condition
116
- path: path || [],
117
- message: args.message
118
- }
119
- ]
120
- };
121
- }
122
- };
123
- }
124
-
125
- export function startsWith(args: NonNullable<StringSchema['__startsWith']>): ValidationFallbackReturnType {
126
- return {
127
- name: 'startsWith',
128
- type: 'low',
129
- // eslint-disable-next-line ts/require-await
130
- callback: async (
131
- value: any,
132
- path: (string | number)[],
133
- _options: Parameters<Schema['__transformToAdapter']>[0]
134
- ) => {
135
- const isValid = value.startsWith(args.value);
136
-
137
- return {
138
- parsed: value,
139
- errors: isValid
140
- ? []
141
- : [
142
- {
143
- isValid: false,
144
- code: 'startsWith',
145
- // eslint-disable-next-line ts/no-unnecessary-condition
146
- path: path || [],
147
- message: args.message
148
- }
149
- ]
150
- };
151
- }
152
- };
153
- }
154
-
155
- export function includes(args: NonNullable<StringSchema['__includes']>): ValidationFallbackReturnType {
156
- return {
157
- name: 'includes',
158
- type: 'low',
159
- // eslint-disable-next-line ts/require-await
160
- callback: async (
161
- value: any,
162
- path: (string | number)[],
163
- _options: Parameters<Schema['__transformToAdapter']>[0]
164
- ) => {
165
- const isValid = value.includes(args.value);
166
-
167
- return {
168
- parsed: value,
169
- errors: isValid
170
- ? []
171
- : [
172
- {
173
- isValid: false,
174
- code: 'includes',
175
- // eslint-disable-next-line ts/no-unnecessary-condition
176
- path: path || [],
177
- message: args.message
178
- }
179
- ]
180
- };
181
- }
182
- };
183
- }
184
-
185
- export function regex(args: NonNullable<StringSchema['__regex']>): ValidationFallbackReturnType {
186
- return {
187
- name: 'regex',
188
- type: 'low',
189
- // eslint-disable-next-line ts/require-await
190
- callback: async (
191
- value: any,
192
- path: (string | number)[],
193
- _options: Parameters<Schema['__transformToAdapter']>[0]
194
- ) => {
195
- const isValid = args.value.test(value);
196
-
197
- return {
198
- parsed: value,
199
- errors: isValid
200
- ? []
201
- : [
202
- {
203
- isValid: false,
204
- code: 'regex',
205
- // eslint-disable-next-line ts/no-unnecessary-condition
206
- path: path || [],
207
- message: args.message
208
- }
209
- ]
210
- };
211
- }
212
- };
213
- }
214
-
215
- export function uuid(args: NonNullable<StringSchema['__uuid']>): ValidationFallbackReturnType {
216
- // const uuidRegex =
217
- // /^([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;
218
- const uuidRegex = /^[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;
219
- return {
220
- name: 'uuid',
221
- type: 'low',
222
- // eslint-disable-next-line ts/require-await
223
- callback: async (
224
- value: any,
225
- path: (string | number)[],
226
- _options: Parameters<Schema['__transformToAdapter']>[0]
227
- ) => {
228
- const isValid = uuidRegex.test(value);
229
-
230
- return {
231
- parsed: value,
232
- errors: isValid
233
- ? []
234
- : [
235
- {
236
- isValid: false,
237
- code: 'uuid',
238
- // eslint-disable-next-line ts/no-unnecessary-condition
239
- path: path || [],
240
- message: args.message
241
- }
242
- ]
243
- };
244
- }
245
- };
246
- }
247
-
248
- export function email(args: NonNullable<StringSchema['__email']>): ValidationFallbackReturnType {
249
- const emailRegex = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
250
- console.log('aquiiiiii');
251
- return {
252
- name: 'email',
253
- type: 'low',
254
- // eslint-disable-next-line ts/require-await
255
- callback: async (
256
- value: any,
257
- path: (string | number)[],
258
- _options: Parameters<Schema['__transformToAdapter']>[0]
259
- ) => {
260
- const isValid = emailRegex.test(value);
261
-
262
- return {
263
- parsed: value,
264
- errors: isValid
265
- ? []
266
- : [
267
- {
268
- isValid: false,
269
- code: 'email',
270
- // eslint-disable-next-line ts/no-unnecessary-condition
271
- path: path || [],
272
- message: args.message
273
- }
274
- ]
275
- };
276
- }
277
- };
278
- }
@@ -1 +0,0 @@
1
- export type ValidatorTypes = 'low' | 'medium' | 'high';
@@ -1,52 +0,0 @@
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
- name: 'union',
9
- type: 'medium',
10
- callback: async (value, path, options) => {
11
- const parsedValues: Awaited<ReturnType<Schema['__parse']>> = {
12
- parsed: value,
13
- errors: []
14
- };
15
- const startingToInternalBubbleUpLength = options.toInternalToBubbleUp?.length || 0;
16
-
17
- // eslint-disable-next-line ts/prefer-for-of
18
- for (let i = 0; i < schemas.length; i++) {
19
- const schemaWithProtected = schemas[i] as Schema & {
20
- __parse: Schema['__parse'];
21
- __toInternal: Schema['__toInternal'];
22
- };
23
- const parsedData = await schemaWithProtected.__parse(value, path, options);
24
- parsedValues.parsed = parsedData.parsed;
25
- if (Array.isArray(parsedData.errors))
26
- if (Array.isArray(parsedValues.errors)) parsedValues.errors.push(...parsedData.errors);
27
- else parsedValues.errors = parsedData.errors;
28
-
29
- const hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors =
30
- // eslint-disable-next-line ts/no-unnecessary-condition
31
- parsedData.errors === undefined || (parsedData.errors || []).length === 0;
32
- if (hasNoErrorsSoItsAValidSchemaAndShouldResetOldErrors) {
33
- return {
34
- parsed: parsedValues.parsed,
35
- errors: []
36
- };
37
- } else if (startingToInternalBubbleUpLength < (options.toInternalToBubbleUp?.length || 0)) {
38
- // If there is a new toInternalToBubbleUp we should remove the ones that we added since this is not a
39
- // valid schema, we shouldn't be calling the `toInternal` on that schemas.
40
- const numberOfElementsToRemove =
41
- (options.toInternalToBubbleUp?.length || 0) - startingToInternalBubbleUpLength;
42
- options.toInternalToBubbleUp?.splice(startingToInternalBubbleUpLength, numberOfElementsToRemove);
43
- }
44
- }
45
- return {
46
- parsed: parsedValues.parsed,
47
- // eslint-disable-next-line ts/no-unnecessary-condition
48
- errors: Array.isArray(parsedValues.errors) ? parsedValues.errors : []
49
- };
50
- }
51
- };
52
- }
@@ -1,225 +0,0 @@
1
- import type { ValidatorTypes } from './types';
2
- import type { ErrorCodes } from '../adapter/types';
3
- import type { Schema } from '../schema/schema';
4
- import type {
5
- ValidationFallbackCallbackReturnType,
6
- ValidationFallbackCallbackType,
7
- ValidationFallbackReturnType
8
- } from '../schema/types';
9
-
10
- const priorityByType = {
11
- low: 0,
12
- medium: 1,
13
- high: 2
14
- };
15
-
16
- const typeByPriority = Object.entries(priorityByType).reduce(
17
- (acc, [key, value]) => {
18
- acc[value] = key as ValidatorTypes;
19
- return acc;
20
- },
21
- {} as { [key: number]: ValidatorTypes }
22
- );
23
-
24
- /**
25
- * Okay, so what is this? This is a validator class, it represents a Node on a linked list. The linked list
26
- * has lower priority validators on the end of the list and higher priority validators on the start of the
27
- * list. Maybe in the future we can change that to a binary tree, but for now this is enough.
28
- *
29
- * Why did we choose this approach? Because what i was doing was that i saw myself repeating the same code 3
30
- * times on the schema in order to make the validation work. Each validator had a different return type, i
31
- * didn't like that. I wanted to add more power and control on the validator, not on the schema. So i created
32
- * this class. So pretty much, over here and on each validator we can define the type it is. It can actually
33
- * be three: `low`, `medium` and `high`. The `low` validators are the ones that are going to be executed last,
34
- * The `high` validators are the ones that are going to be executed first. High validators validate if the value
35
- * is null or undefined, if it allows that. It can stop the execution of the other validators if it wants to.
36
- *
37
- * Example: Let's say that the value is null, if the value is null, is there a reason to check if it's a number?
38
- * No, right? So the high validator can stop the execution of the other validators.
39
- * Same as before, if the value is not a number, is there a reason to check if it's value is greater than the
40
- * `max` allowed 10? No, right? So the medium validator can stop the execution of the other validators.
41
- *
42
- * That's what this solve, it's a better approach than repeating the same code 3 times on the schema. It's also
43
- * more powerful, because if we need to add any extra priorities we can do that easily without changing the schema.
44
- */
45
- export class Validator {
46
- child?: Validator;
47
- parent?: Validator;
48
- fallbackNamesAdded = new Set();
49
- priority: number;
50
- fallbacks: ((
51
- value: any,
52
- path: (string | number)[],
53
- options: Parameters<Schema['__transformToAdapter']>[0]
54
- ) => Promise<{
55
- parsed: any;
56
- errors: {
57
- isValid: boolean;
58
- code: ErrorCodes;
59
- message: string;
60
- path: (string | number)[];
61
- }[];
62
- preventChildValidation?: boolean;
63
- }>)[] = [];
64
-
65
- constructor(type: ValidatorTypes) {
66
- this.fallbackNamesAdded = new Set();
67
- this.fallbacks = [];
68
- this.priority = priorityByType[type];
69
- }
70
-
71
- /**
72
- * We create all of the validators on the schema in order, i actually didn't want to go on that route but i
73
- * found it easier to do so.
74
- *
75
- * The logic here is simple, if it's not the same priority we will walk on the linked list until we find
76
- * a validator that matches the priority we are expecting. If we can't walk anymore, we create the next
77
- * priority validator and append it to the linked list. Be aware that it's a double linked list, so we
78
- * can walk both ways, from the end to the start and from the start to the end.
79
- * So you don't really need to start from the root, the linked list can start from anywhere and it will
80
- * find it's way through.
81
- *
82
- * I know there are better ways to do this instead of walking through the linked list, but like i explained
83
- * before, this is enough for now.
84
- *
85
- * If the priority is higher than the current priority saved on the schema, we should substitute the
86
- * rootValidator on the schema with the new one.
87
- *
88
- * @param schema - The schema that we are working on right now, all fallbacks are tied to that specific schema.
89
- * @param type - The type of the fallback that we are adding.
90
- * @param fallback - The fallback function that we are adding.
91
- * @param childOrParent - If we are adding a fallback to the child or to the parent.
92
- * @param options - The options that we are passing to the fallback.
93
- */
94
- private checkAppendOrCreate(
95
- schema: Schema,
96
- type: ValidatorTypes,
97
- fallbackName: string,
98
- fallback: ValidationFallbackCallbackType,
99
- childOrParent: 'child' | 'parent',
100
- options?: Parameters<(typeof Validator)['createAndAppendFallback']>[2]
101
- ) {
102
- const schemaWithProtected = schema as Schema & { __rootFallbacksValidator?: Schema['__rootFallbacksValidator'] };
103
-
104
- if (this[childOrParent])
105
- (this as any)[childOrParent].addFallback(schemaWithProtected, type, fallbackName, fallback, options);
106
- else {
107
- const nextPriority = childOrParent === 'child' ? this.priority - 1 : this.priority + 1;
108
- if (Object.keys(typeByPriority).includes(String(nextPriority))) {
109
- const nextType = typeByPriority[nextPriority];
110
-
111
- const validatorInstance = new Validator(nextType);
112
- this[childOrParent] = validatorInstance;
113
- (this as any)[childOrParent][childOrParent === 'parent' ? 'child' : 'parent'] = this;
114
- (this as any)[childOrParent].addFallback(schemaWithProtected, type, fallbackName, fallback, options);
115
- if (nextPriority > schemaWithProtected.__rootFallbacksValidator.priority)
116
- schemaWithProtected.__rootFallbacksValidator = validatorInstance;
117
- }
118
- }
119
- }
120
-
121
- addFallback(
122
- schema: Schema,
123
- type: ValidatorTypes,
124
- fallbackName: string,
125
- fallback: ValidationFallbackCallbackType,
126
- options?: Parameters<(typeof Validator)['createAndAppendFallback']>[2]
127
- ) {
128
- if (this.fallbackNamesAdded.has(fallbackName) && options?.removeCurrent !== true) return;
129
- this.fallbackNamesAdded.add(fallbackName);
130
- const priority = priorityByType[type];
131
- if (this.priority === priority) {
132
- if (typeof options?.at === 'number')
133
- this.fallbacks.splice(options.at, options.removeCurrent === true ? 1 : 0, fallback);
134
- else this.fallbacks.push(fallback);
135
- } else if (priority > this.priority)
136
- this.checkAppendOrCreate(schema, type, fallbackName, fallback, 'parent', options);
137
- else if (priority < this.priority) this.checkAppendOrCreate(schema, type, fallbackName, fallback, 'child', options);
138
- }
139
-
140
- /**
141
- * Validates the value against all of the fallbacks, the fallbacks are executed in order, from the highest
142
- * priority to the lowest priority. A validator can stop the execution of the other validators if it feels
143
- * like so. Like on the example of a value being null or undefined.
144
- *
145
- * @param errorsAsHashedSet - This is a set that contains all of the errors that we already found, this is
146
- * used to avoid duplicated errors.
147
- * @param path - The path that we are validating right now.
148
- * @param parseResult - The result of the parsing, it contains the parsed value and the errors that we found.
149
- * @param options - The options that we are passing to the fallback.
150
- */
151
- async validate(
152
- errorsAsHashedSet: Set<string>,
153
- path: ValidationFallbackCallbackReturnType['errors'][number]['path'],
154
- parseResult: {
155
- errors: undefined | ValidationFallbackCallbackReturnType['errors'];
156
- parsed: any;
157
- },
158
- options: Parameters<Schema['__transformToAdapter']>[0]
159
- ): Promise<ValidationFallbackCallbackReturnType> {
160
- let doesItShouldPreventChildValidation = false;
161
-
162
- for (const fallback of this.fallbacks) {
163
- const { parsed, errors, preventChildValidation } = await fallback(parseResult.parsed, path, options);
164
- parseResult.parsed = parsed;
165
- for (const error of errors) {
166
- if (error.isValid === false) {
167
- const sortedError = Object.fromEntries(Object.entries(error).sort(([a], [b]) => a.localeCompare(b)));
168
-
169
- const hashedError = JSON.stringify(sortedError);
170
- if (errorsAsHashedSet.has(hashedError)) continue;
171
- errorsAsHashedSet.add(hashedError);
172
- if (!Array.isArray(parseResult.errors)) parseResult.errors = [];
173
- parseResult.errors.push({
174
- ...error,
175
- received: parseResult.parsed
176
- });
177
- }
178
- }
179
- doesItShouldPreventChildValidation = doesItShouldPreventChildValidation || preventChildValidation || false;
180
- }
181
-
182
- if (this.child && doesItShouldPreventChildValidation === false)
183
- return await this.child.validate(errorsAsHashedSet, path, parseResult, options);
184
-
185
- return parseResult as unknown as Promise<ValidationFallbackCallbackReturnType>;
186
- }
187
-
188
- /**
189
- * This static method takes care of everything for you. This means that you should only call this method
190
- * for appending new fallbacks, it takes care of creating the root validator and making sure that the
191
- * rootValidator on the schema is the highest priority one.
192
- *
193
- * @param schema - The schema that we are working on right now, all fallbacks are tied to that specific
194
- * schema. We automatically define the rootValidator on the schema so you don't need to worry about that.
195
- * @param fallback - The fallback that we are adding. This is an object that contains the type of the
196
- * fallback and the callback that we are adding.
197
- * @param options - The options that we are passing to the fallback. Options like `at` and `removeCurrent`
198
- * are passed to the `addFallback` method.
199
- */
200
- static createAndAppendFallback(
201
- schema: Schema<any, any>,
202
- fallback: ValidationFallbackReturnType,
203
- options?: {
204
- at?: number;
205
- removeCurrent?: boolean;
206
- }
207
- ) {
208
- const schemaWithProtected = schema as Schema & { __rootFallbacksValidator?: Schema['__rootFallbacksValidator'] };
209
-
210
- let validatorInstance = schemaWithProtected.__rootFallbacksValidator;
211
- // eslint-disable-next-line ts/no-unnecessary-condition
212
- if (schemaWithProtected.__rootFallbacksValidator === undefined) {
213
- validatorInstance = new Validator(fallback.type);
214
- schemaWithProtected.__rootFallbacksValidator = validatorInstance;
215
- }
216
- validatorInstance.addFallback(schema, fallback.type, fallback.name, fallback.callback, options);
217
- return validatorInstance;
218
- }
219
-
220
- toString(ident = 0): string {
221
- return `Priority: ${this.priority}\nFallbacks: ${this.fallbacks.length}\n${
222
- this.child ? `Children:\n${this.child.toString(ident + 2)}` : ''
223
- }`;
224
- }
225
- }
package/tsconfig.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "declaration": true,
5
- "outDir": "./dist",
6
- "types": ["node"]
7
- },
8
- "include": ["src/**/*"]
9
- }
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../build/tsconfig.types.json",
3
- "compilerOptions": {
4
- "declaration": true, /* Generate d.ts files */
5
- "rootDir": "./src",
6
- "outDir": "./dist/cjs/types",
7
- "types": ["node"]
8
- },
9
- "include": ["src/**/*"]
10
- }