use-validate-form
Advanced tools
Comparing version 2.1.0 to 2.1.1
{ | ||
"name": "use-validate-form", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "react hook for form validation", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -31,4 +31,8 @@ # use-validate-form | ||
Each item in the `validators` arrays must be an objects with a `func` property which returns `true` or `false` and an `error` property which is a string that will explain to a user why their input is invalid. | ||
Each item in the `validators` arrays must be an objects with a) a `func` property which takes the new value, the field's type, and an object containing all fields as arguments, and returns `true` or `false` and b) an `error` property which is a string that will explain to a user why their input is invalid. | ||
```js | ||
(value, type, allFields) => value > allFields.otherField.value | ||
``` | ||
Each of the field objects returned by `useValidateForm` (i.e. `name`, `age`) have the following properties: | ||
@@ -44,3 +48,3 @@ | ||
name, // string | ||
validators // array of functions | ||
validators // array of validtor objects | ||
} = age | ||
@@ -47,0 +51,0 @@ ``` |
@@ -1,2 +0,2 @@ | ||
import { useReducer } from 'react' | ||
import { useReducer, useMemo } from 'react' | ||
@@ -41,2 +41,3 @@ enum FieldType { | ||
} | ||
const { isValid: isFieldValid, errors, isDirty } = runValidators(initialValue, type, validators, false, formFields) | ||
@@ -62,3 +63,5 @@ return { | ||
export const useValidateForm = (formFields: Array<FormField<any>>) => { | ||
const form = createForm(formFields) | ||
const form = useMemo(() => { | ||
return createForm(formFields) | ||
}, [JSON.stringify(formFields)]) | ||
return useReducer(reducer, { | ||
@@ -98,3 +101,6 @@ ...form, | ||
payload: { name: fieldName, value, newForm } | ||
}: { type: string; payload: { name: string; value: any; newForm: Array<FormField<any>> } } | ||
}: { | ||
type: string | ||
payload: { name: string; value: any; newForm: Array<FormField<any>> } | ||
} | ||
) => { | ||
@@ -159,4 +165,4 @@ switch (type) { | ||
export const isRequired = { | ||
func: (value: any, fieldType: FieldType) => { | ||
if (fieldType === FieldType.DATE) { | ||
func: (value: any, fieldType: string) => { | ||
if (fieldType === 'date') { | ||
return value && Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value) | ||
@@ -163,0 +169,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
514389
352
61