Skip to content

Commit 6e86b4f

Browse files
authored
perf: reduce vest resolver size (#129)
* test: extract test's fixtures * test: jest fixtures config * perf: reduce superstruct resolver size * feat: add error's ref * test: add error's ref + extract fixtures * test: add nested test * refactor: rename toNestObject to toNestError * refactor: remove duplicate line * perf: recude vest resolver bundle size
1 parent 0ddf75a commit 6e86b4f

File tree

3 files changed

+34
-44
lines changed

3 files changed

+34
-44
lines changed

vest/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"types": "dist/index.d.ts",
1212
"license": "MIT",
1313
"peerDependencies": {
14-
"react-hook-form": ">=6.6.0"
14+
"react-hook-form": ">=6.6.0",
15+
"@hookform/resolvers": ">=2.0.0",
16+
"vest": ">=3.0.0"
1517
}
1618
}

vest/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type ICreateResult = ReturnType<typeof Vest.create>;
1111
export type Resolver = (
1212
schema: ICreateResult,
1313
schemaOptions?: never,
14-
factoryOptions?: { mode: 'async' | 'sync' },
14+
factoryOptions?: { mode?: 'async' | 'sync' },
1515
) => <TFieldValues extends FieldValues, TContext>(
1616
values: UnpackNestedValue<TFieldValues>,
1717
context: TContext | undefined,

vest/src/vest.ts

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,45 @@
11
import { toNestError } from '@hookform/resolvers';
2+
import { FieldError } from 'react-hook-form';
23
import promisify from 'vest/promisify';
3-
import { DraftResult, IVestResult } from 'vest/vestResult';
44
import type { VestErrors, Resolver } from './types';
55

66
const parseErrorSchema = (
77
vestError: VestErrors,
88
validateAllFieldCriteria: boolean,
99
) => {
10-
return Object.entries(vestError).reduce((prev, [key, value]) => {
11-
return {
12-
...prev,
13-
[key]: {
14-
type: '',
15-
message: value[0],
16-
...(validateAllFieldCriteria
17-
? {
18-
types: value.reduce((prev, message, index) => {
19-
return {
20-
...prev,
21-
[index]: message,
22-
};
23-
}, {}),
24-
}
25-
: {}),
26-
},
27-
};
28-
}, {});
10+
const errors: Record<string, FieldError> = {};
11+
for (const path in vestError) {
12+
if (!errors[path]) {
13+
errors[path] = { message: vestError[path][0], type: '' };
14+
}
15+
16+
if (validateAllFieldCriteria) {
17+
errors[path].types = vestError[path].reduce<Record<number, string>>(
18+
(acc, message, index) => (acc[index] = message) && acc,
19+
{},
20+
);
21+
}
22+
}
23+
return errors;
2924
};
3025

3126
export const vestResolver: Resolver = (
3227
schema,
3328
_,
34-
{ mode } = { mode: 'async' },
35-
) => async (values, _context, { criteriaMode, fields }) => {
36-
let result: IVestResult | DraftResult;
37-
if (mode === 'async') {
38-
const validateSchema = promisify(schema);
39-
result = await validateSchema(values);
40-
} else {
41-
result = schema(values);
42-
}
43-
44-
const errors = result.getErrors();
45-
46-
if (!result.hasErrors()) {
47-
return { values, errors: {} };
48-
}
29+
resolverOptions = {},
30+
) => async (values, _context, options) => {
31+
const result =
32+
resolverOptions.mode === 'sync'
33+
? schema(values)
34+
: await promisify(schema)(values);
4935

50-
return {
51-
values: {},
52-
errors: toNestError(
53-
parseErrorSchema(errors, criteriaMode === 'all'),
54-
fields,
55-
),
56-
};
36+
return result.hasErrors()
37+
? {
38+
values: {},
39+
errors: toNestError(
40+
parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),
41+
options.fields,
42+
),
43+
}
44+
: { values, errors: {} };
5745
};

0 commit comments

Comments
 (0)