comp-val
v1.0.2
Published
composable validation
Downloads
26
Readme
@tww/cval Composable Validation!
comes with typescript definitions
npm install --save @tww/cval
Examples
import cval from '@tww/cval'
// using validations individually
const validResultFromEmail = cval.isEmail('myEmailFeild', '[email protected]')
// '' because [email protected] is valid
console.log('email error', validResultFromEmail)
const invalidResultFromEmail = cval.isEmail('myEmailFeild', 'bob$bob.com')
// 'myEmailField must be a valid email' because bob$bob.com is invalid
console.log('email error', validResultFromEmail)
const validateEmail = cval.composeValidations(
cval.isEmail,
cval.minLength(6),
cval.maxLength(15)
)
// empty array is returned, no errors
let errors = validateEmail('myEmailField', '[email protected]')
console.log(errors.length) // 0
// steve@bobcom is not a valid email
errors = validateEmail('myEmailField', 'steve@bobcom')
console.log(errors.length, errors) // 1 error returned
// ['myEmailField must be a valid email']
// a@bc is not a valid email, and it's too short!
errors = validateEmail('myEmailField', 'a@bc')
console.log(errors.length, errors) // 1 error returned
// ['myEmailField must be a valid email', 'myEmailField must be at least 9 characters']
Docs and bundled validation
Checkout our validation docs