Elegant and composable validations.
Revalidate is a library for creating and composing together small validation functions to create complex, robust validations. There is no need for awkward configuration rules to define validations. Just use functions.
All right. No more upselling. Just look at an example ❤️.
// ES2015; // Or ES5var r = ;var createValidator = rcreateValidator;var composeValidators = rcomposeValidators;var combineValidators = rcombineValidators;var isRequired = risRequired;var isAlphabetic = risAlphabetic;var isNumeric = risNumeric; // Usageconst dogValidator = ; ; // { name: 'Name is required' } ;// { name: 'Name must be alphabetic', age: 'Age must be numeric' } ; // {}
Install
Install with yarn or npm.
yarn add revalidate
npm install --save revalidate
Getting Started
Docs
Revalidate has a host of options along with helper functions for building validations and some common validation functions right out of the box. To learn more, check out the docs at revalidate.jeremyfairbank.com.
Redux Form
Just one more example! You might have heard about revalidate through Redux Form. Revalidate was originally conceived as a library for writing validation functions for Redux Form. Revalidate is still a great companion to Redux Form! Here is the simple synchronous form validation from Redux Form's docs rewritten to use revalidate:
const isValidEmail = const isGreaterThan = const customIsRequired = const validate = const warn = { const warnings = {} if valuesage < 19 warningsage = 'Hmm, you seem a bit young...' return warnings} const renderField = <div> <label>label</label> <div> <input ...input placeholder=label type=type/> touched && error && <span>error</span> || warning && <span>warning</span> </div> </div> const SyncValidationForm = { const handleSubmit pristine reset submitting = props return <form onSubmit=handleSubmit> <Field name="username" type="text" component=renderField label="Username"/> <Field name="email" type="email" component=renderField label="Email"/> <Field name="age" type="number" component=renderField label="Age"/> <div> <button type="submit" disabled=submitting>Submit</button> <button type="button" disabled=pristine || submitting onClick=reset> Clear Values </button> </div> </form> } form: 'syncValidation' // a unique identifier for this form validate // <--- validation function given to redux-form warn // <--- warning function given to redux-formSyncValidationForm