react-fastest-validator
v1.1.0
Published
React hook for validating forms or inputs, is an adaptation of the fastest-validation library.
Downloads
10
Readme
react-fastest-validator
⚡ An adaptation for the JS library fastest-validator. To validate forms or inputs the easiest and fastest way.
Installation
npm i react-fastest-validator --save
or
yarn add react-fastest-validator
Usage
To use this library you need to have reactJS library installed in your app.
Validate
The validation is maked by a hook, so in order to validate anything we need to import the hook. And we use the hook as shows as following.
import useFastestValidator from 'react-fastest-validator'
function App() {
const [form, setForm] = useState({
name:''
})
const formSchemaValidations = { name: { type: "string", min: 3, max: 20 }}
//Fastest Validator Hook
const [validationError, validationTrigger] = useFastestValidator(formSchemaValidations)
const handleOnChange = ({target}) => {
const formData = { ...form, [target.name]: target.value}
/*ValidationTrigger
target: extracted from event of onChange
formData: state object where the key is the name of the input and the value the content to be validated.*/
validationTrigger(target, formData)
setForm(formData)
}
return (
<div className="App">
<input onChange={handleOnChange} type="text" name='name'></input>
<p>{validationError.name}</p>
</div>
)
}
to see more information about the usage of fastest-validator please, see their docs
Contributions
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some tests, because these things are important.
License
react-fastest-validator is available under the MIT license.
Thanks to
IceBob for making the awesome library that this library is based on!