From 6070ee222771b2645b4e3c9cd3528bfef6291082 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 19 Oct 2019 20:15:54 +0200 Subject: [PATCH] cleanup validators and allow string return as error message --- lib/index.ts | 23 +++++++++++------------ package.json | 2 +- test/test.tsx | 7 ++++--- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 4329373..5b1c515 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -8,7 +8,8 @@ export type useFormOptions = { setter?: string } -export type useFormValidatorFunction = (s: any) => boolean | Promise +export type useFormValidatorFunctionReturn = boolean | string +export type useFormValidatorFunction = (s: any) => useFormValidatorFunctionReturn | Promise export type useFormValidatorMethod = useFormValidatorFunction | RegExp export type useFormValidatorObject = { @@ -50,7 +51,7 @@ export const useForm = => { + const _validateAll = async (value: any, object: useFormValidator): Promise => { const validator = isFormValidatorObject(object) ? object.validator : object if (validator.constructor.name === 'Function') return (validator as useFormValidatorFunction)(value) @@ -59,28 +60,26 @@ export const useForm = + result === true ? undefined : result.constructor.name === 'String' ? result : isFormValidatorObject(validator) && validator.message ? validator.message : defaultErrorMessage(key) + const _validate = (key: keyof T, value: any) => { const validator: useFormValidatorParameter | undefined = validators[key] if (!validator) return if (Array.isArray(validator)) { - Promise.all(validator.map(v => _validateAll(value, v))).then(result => { - const index = result.indexOf(false) + Promise.all(validator.map(v => _validateAll(value, v))).then(results => { + const i = results.findIndex(result => result !== true) setErrors({ ...errors, - [key]: - index === -1 - ? undefined - : isFormValidatorObject(validator[index]) && (validator[index] as useFormValidatorObject).message - ? (validator[index] as useFormValidatorObject).message - : defaultErrorMessage(key), + [key]: i === -1 ? undefined : _getErrorMessage(results[i], key, validator[i]), }) }) } else { - _validateAll(value, validator).then(valid => { + _validateAll(value, validator).then(result => { setErrors({ ...errors, - [key]: valid ? undefined : isFormValidatorObject(validator) && validator.message ? validator.message : defaultErrorMessage(key), + [key]: _getErrorMessage(result, key, validator), }) }) } diff --git a/package.json b/package.json index 2212088..614722e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "formhero", - "version": "0.0.6", + "version": "0.0.7", "main": "dist/index.js", "typings": "dist/index.d.ts", "scripts": { diff --git a/test/test.tsx b/test/test.tsx index f995469..900eee8 100644 --- a/test/test.tsx +++ b/test/test.tsx @@ -15,13 +15,14 @@ const initial = { const Index: React.FC = () => { const { field, form, errors, isValid, setForm, setErrors, setField } = useForm(initial, { username: [ - /^test/, + /^abc/, { - validator: async () => { + validator: async (s: string) => { return true }, - message: 'Digits please', + message: 'Async shit not working', }, + (s: string) => (s.includes('d') ? true : 'Needs the D'), ], password: { validator: /^.{3,}$/,