diff --git a/README.md b/README.md index 3d8146f7..c382f298 100644 --- a/README.md +++ b/README.md @@ -85,16 +85,16 @@ import { struct } from "superstruct"; const schema = struct({ name: "string", - age: "number" + age: "number", }); const App = () => { const { register, handleSubmit } = useForm({ - resolver: superstructResolver(schema) + resolver: superstructResolver(schema), }); return ( -
console.log(d))}> + console.log(d))}> @@ -117,16 +117,16 @@ import { joiResolver } from "@hookform/resolvers"; import Joi from "@hapi/joi"; const schema = Joi.object({ - username: Joi.string().required() + username: Joi.string().required(), }); const App = () => { const { register, handleSubmit } = useForm({ - resolver: joiResolver(schema) + resolver: joiResolver(schema), }); return ( - console.log(d))}> + console.log(d))}> diff --git a/package.json b/package.json index f23ad6a1..3c6eebce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hookform/resolvers", - "version": "0.0.3", + "version": "0.0.4", "description": "React Hook Form validation resolvers: Yup, Joi, Superstruct and etc.", "main": "dist/index.js", "module": "dist/index.esm.js", diff --git a/src/joi.ts b/src/joi.ts index 4e9eea89..22ec39db 100644 --- a/src/joi.ts +++ b/src/joi.ts @@ -1,23 +1,14 @@ -import { appendErrors, transformToNestObject } from 'react-hook-form'; +import { appendErrors, transformToNestObject, Resolver } from 'react-hook-form'; import Joi from '@hapi/joi'; import convertArrayToPathName from './utils/convertArrayToPathName'; -type JoiError = { - details: any; -}; - -type FieldValues = Record; - -type FieldError = { - path: (string | number)[]; - message: string; - type: string; -}; - -const parseErrorSchema = (error: JoiError, validateAllFieldCriteria: boolean) => +const parseErrorSchema = ( + error: Joi.ValidationError, + validateAllFieldCriteria: boolean, +) => Array.isArray(error.details) ? error.details.reduce( - (previous: FieldValues, { path, message = '', type }: FieldError) => { + (previous: Record, { path, message = '', type }) => { const currentPath = convertArrayToPathName(path); return { @@ -51,19 +42,21 @@ const parseErrorSchema = (error: JoiError, validateAllFieldCriteria: boolean) => ) : []; -export const joiResolver = ( - validationSchema: Joi.Schema, - config: any = { +export const joiResolver = >( + schema: Joi.Schema, + options: Joi.AsyncValidationOptions = { abortEarly: false, }, -) => async (data: any, _: any = {}, validateAllFieldCriteria = false) => { +): Resolver => async ( + values, + _, + validateAllFieldCriteria = false, +) => { try { - const values = await validationSchema.validateAsync(data, { - ...config, - }); - return { - values, + values: await schema.validateAsync(values, { + ...options, + }), errors: {}, }; } catch (e) { diff --git a/src/superstruct.ts b/src/superstruct.ts index ec2aa37a..c0df688b 100644 --- a/src/superstruct.ts +++ b/src/superstruct.ts @@ -1,25 +1,14 @@ -import { appendErrors, transformToNestObject } from 'react-hook-form'; +import { appendErrors, transformToNestObject, Resolver } from 'react-hook-form'; +import Superstruct from 'superstruct'; import convertArrayToPathName from './utils/convertArrayToPathName'; -type SuperStructError = { - failures: any; -}; - -type FieldValues = Record; - -type FieldError = { - path: (string | number)[]; - message: string; - type: string; -}; - const parseErrorSchema = ( - error: SuperStructError, + error: Superstruct.StructError, validateAllFieldCriteria: boolean, ) => Array.isArray(error.failures) ? error.failures.reduce( - (previous: FieldValues, { path, message = '', type }: FieldError) => { + (previous: Record, { path, message = '', type }) => { const currentPath = convertArrayToPathName(path); return { @@ -31,7 +20,7 @@ const parseErrorSchema = ( currentPath, validateAllFieldCriteria, previous, - type, + type || '', message, ), } @@ -41,7 +30,7 @@ const parseErrorSchema = ( type, ...(validateAllFieldCriteria ? { - types: { [type]: message || true }, + types: { [type || '']: message || true }, } : {}), }, @@ -53,14 +42,16 @@ const parseErrorSchema = ( ) : []; -export const superstructResolver = (validationSchema: any) => async ( - data: any, - _: any = {}, +export const superstructResolver = >( + schema: Superstruct.Struct, +): Resolver => async ( + values, + _, validateAllFieldCriteria = false, ) => { try { return { - values: validationSchema(data), + values: schema(values), errors: {}, }; } catch (e) { diff --git a/src/yup.test.ts b/src/yup.test.ts index 0672eadc..14b33805 100644 --- a/src/yup.test.ts +++ b/src/yup.test.ts @@ -96,15 +96,15 @@ describe('validateWithSchema', () => { validate: () => { throw errors; }, - })({}), + } as any)({}), ).toMatchSnapshot(); }); it('should return empty object when validate pass', async () => { expect( await yupResolver({ - validate: () => new Promise((resolve) => resolve()) as any, - })({}), + validate: () => new Promise((resolve) => resolve()), + } as any)({}), ).toEqual({ errors: {}, values: undefined, diff --git a/src/yup.ts b/src/yup.ts index 17fa3026..c11b020b 100644 --- a/src/yup.ts +++ b/src/yup.ts @@ -1,21 +1,13 @@ -import { appendErrors, transformToNestObject } from 'react-hook-form'; - -type YupValidationError = { - inner: { path: string; message: string; type: string }[]; - path: string; - message: string; - type: string; -}; - -type FieldValues = Record; +import { appendErrors, transformToNestObject, Resolver } from 'react-hook-form'; +import Yup from 'yup'; const parseErrorSchema = ( - error: YupValidationError, + error: Yup.ValidationError, validateAllFieldCriteria: boolean, ) => Array.isArray(error.inner) ? error.inner.reduce( - (previous: FieldValues, { path, message, type }: FieldValues) => ({ + (previous: Record, { path, message, type }) => ({ ...previous, ...(path ? previous[path] && validateAllFieldCriteria @@ -47,17 +39,21 @@ const parseErrorSchema = ( [error.path]: { message: error.message, type: error.type }, }; -export const yupResolver = ( - validationSchema: any, - config: any = { +export const yupResolver = >( + schema: Yup.ObjectSchema, + options: Yup.ValidateOptions = { abortEarly: false, }, -) => async (data: any, _: any = {}, validateAllFieldCriteria = false) => { +): Resolver => async ( + values, + _, + validateAllFieldCriteria = false, +) => { try { return { - values: await validationSchema.validate(data, { - ...config, - }), + values: (await schema.validate(values, { + ...options, + })) as any, errors: {}, }; } catch (e) {