Skip to content

Commit

Permalink
perf: reduce vest resolver size (#129)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
jorisre authored Feb 3, 2021
1 parent 0ddf75a commit 6e86b4f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
4 changes: 3 additions & 1 deletion vest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"types": "dist/index.d.ts",
"license": "MIT",
"peerDependencies": {
"react-hook-form": ">=6.6.0"
"react-hook-form": ">=6.6.0",
"@hookform/resolvers": ">=2.0.0",
"vest": ">=3.0.0"
}
}
2 changes: 1 addition & 1 deletion vest/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type ICreateResult = ReturnType<typeof Vest.create>;
export type Resolver = (
schema: ICreateResult,
schemaOptions?: never,
factoryOptions?: { mode: 'async' | 'sync' },
factoryOptions?: { mode?: 'async' | 'sync' },
) => <TFieldValues extends FieldValues, TContext>(
values: UnpackNestedValue<TFieldValues>,
context: TContext | undefined,
Expand Down
72 changes: 30 additions & 42 deletions vest/src/vest.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,45 @@
import { toNestError } from '@hookform/resolvers';
import { FieldError } from 'react-hook-form';
import promisify from 'vest/promisify';
import { DraftResult, IVestResult } from 'vest/vestResult';
import type { VestErrors, Resolver } from './types';

const parseErrorSchema = (
vestError: VestErrors,
validateAllFieldCriteria: boolean,
) => {
return Object.entries(vestError).reduce((prev, [key, value]) => {
return {
...prev,
[key]: {
type: '',
message: value[0],
...(validateAllFieldCriteria
? {
types: value.reduce((prev, message, index) => {
return {
...prev,
[index]: message,
};
}, {}),
}
: {}),
},
};
}, {});
const errors: Record<string, FieldError> = {};
for (const path in vestError) {
if (!errors[path]) {
errors[path] = { message: vestError[path][0], type: '' };
}

if (validateAllFieldCriteria) {
errors[path].types = vestError[path].reduce<Record<number, string>>(
(acc, message, index) => (acc[index] = message) && acc,
{},
);
}
}
return errors;
};

export const vestResolver: Resolver = (
schema,
_,
{ mode } = { mode: 'async' },
) => async (values, _context, { criteriaMode, fields }) => {
let result: IVestResult | DraftResult;
if (mode === 'async') {
const validateSchema = promisify(schema);
result = await validateSchema(values);
} else {
result = schema(values);
}

const errors = result.getErrors();

if (!result.hasErrors()) {
return { values, errors: {} };
}
resolverOptions = {},
) => async (values, _context, options) => {
const result =
resolverOptions.mode === 'sync'
? schema(values)
: await promisify(schema)(values);

return {
values: {},
errors: toNestError(
parseErrorSchema(errors, criteriaMode === 'all'),
fields,
),
};
return result.hasErrors()
? {
values: {},
errors: toNestError(
parseErrorSchema(result.getErrors(), options.criteriaMode === 'all'),
options.fields,
),
}
: { values, errors: {} };
};

0 comments on commit 6e86b4f

Please sign in to comment.