-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
34 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: {} }; | ||
}; |