From c61fd8f845c2ba0024c2819ce9ecde11e668dbc6 Mon Sep 17 00:00:00 2001 From: Joris Date: Sat, 10 Apr 2021 11:15:28 +0200 Subject: [PATCH] perf(class-validator): reduce classValidator resolver bundle size --- class-validator/package.json | 4 ++- class-validator/src/class-validator.ts | 44 +++++++++++++------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/class-validator/package.json b/class-validator/package.json index 8bbad363..cfe67b5e 100644 --- a/class-validator/package.json +++ b/class-validator/package.json @@ -12,6 +12,8 @@ "license": "MIT", "peerDependencies": { "react-hook-form": ">=6.6.0", - "@hookform/resolvers": ">=2.0.0" + "@hookform/resolvers": ">=2.0.0", + "class-transformer": "^0.4.0", + "class-validator": "^0.12.0" } } diff --git a/class-validator/src/class-validator.ts b/class-validator/src/class-validator.ts index b5b32151..d40907f3 100644 --- a/class-validator/src/class-validator.ts +++ b/class-validator/src/class-validator.ts @@ -1,8 +1,8 @@ -import type { Resolver } from './types'; -import { plainToClass } from 'class-transformer'; -import { validate, validateSync, ValidationError } from 'class-validator'; import { FieldErrors } from 'react-hook-form'; import { toNestError } from '@hookform/resolvers'; +import { plainToClass } from 'class-transformer'; +import { validate, validateSync, ValidationError } from 'class-validator'; +import type { Resolver } from './types'; const parseErrors = ( errors: ValidationError[], @@ -14,19 +14,19 @@ const parseErrors = ( const _path = path ? `${path}.${error.property}` : error.property; if (error.constraints) { - const [key, message] = Object.entries(error.constraints)[0]; + const key = Object.keys(error.constraints)[0]; acc[_path] = { type: key, - message, + message: error.constraints[key], }; if (validateAllFieldCriteria && acc[_path]) { - acc[_path] = { ...acc[_path], types: error.constraints }; + Object.assign(acc[_path], { types: error.constraints }); } } - if (error.children?.length) { - parseErrors(error.children, validateAllFieldCriteria, acc, `${_path}`); + if (error.children && error.children.length) { + parseErrors(error.children, validateAllFieldCriteria, acc, _path); } return acc; @@ -39,18 +39,18 @@ export const classValidatorResolver: Resolver = ( resolverOptions = {}, ) => async (values, _, options) => { const user = plainToClass(schema, values); - const rawErrors = - resolverOptions.mode === 'sync' - ? validateSync(user, schemaOptions) - : await validate(user, schemaOptions); - - if (rawErrors.length === 0) { - return { values, errors: {} }; - } - - const errors = toNestError( - parseErrors(rawErrors, options.criteriaMode === 'all'), - options.fields, - ); - return { values: {}, errors }; + + const rawErrors = await (resolverOptions.mode === 'sync' + ? validateSync + : validate)(user, schemaOptions); + + return rawErrors.length + ? { + values: {}, + errors: toNestError( + parseErrors(rawErrors, options.criteriaMode === 'all'), + options.fields, + ), + } + : { values, errors: {} }; };