A minimalistic & extensible validation library for javascript
$ yarn add micro-validator
import microValidator from 'micro-validator'
const validate = (data) => {
const errors = microValidator.validate({
name: {
required: {
errorMsg: `Company Name is required`
}
},
email: {
required: {
errorMsg: `Email is required`
},
email: {
errorMsg: `Enter a valid email`
}
},
website: {
required: {
errorMsg: `Apply link is required`
},
validUrl: {
errorMsg: `Please enter a valid url`
}
}
}, data)
return errors
}
validate({
name: 'micro-validator',
email: '[email protected]',
website: 'github.com'
})
Developer can add new rules to microValidator by calling addRule
method with rule
(rule name) and validator
function which will validate the given input.
The provided validator
function will receive two arguments value
and options
passed when defining the validation schema
If validator
function returns true
it means value is valid and if it returns false
it means value is invalid.
import microValidator from 'micro-validator'
function checkBetween (value, options) {
return value > options.from && options.to > value
}
microValidator.addRule('between', checkBetween)
let validationSchema = {
count: {
between: {
from: 10,
to: 20,
errorMsg: 'Count should be between 10 to 20'
}
}
}
let errors = microValidator.validate(validationSchema, { count: 21 })
console.log(errors) // { count: [ 'Count should be between 10 to 20' ] }
microValidator.validate(validationSchema, { count: 11 })
console.log(errors) // {}
validate
method takes two arguments
- validationSchema: An object containing information about how to validate data
- data: data to validate
validationSchema
needs to be structured something like this
{
keyName1: {
rule: { ...options },
rule: { ...options }
},
keyName:2 {
rule: { ...options }
}
}
and data
should be
{
keyName1: 'Hello',
keyName2: 'World'
}
Where keyName
is the property of data
we want to apply our validation rules to, rule
is the name of the rule
we want to apply and options
are the options that we want to send to validator
function.
If all of the data validate successfully validate
method will return an empty object. If there are some errors an object will be returned which will contains all of the errors corresponding to their property.
{
keyName1: [
'first rule failed',
'second rule faild'
],
keyName2: [
'first rule failed'
]
}
microValidor doesn't control what options you pass in validationSchema
because developers can easily role their own custom rules. Only option errorMsg
is valid. You can pass errorMsg
to rules which will be returned if the validation fails for a specific rule.
Checks if a value is empty or not
{
// your field name for demo I am using *key*
key: {
required: {
errorMsg: 'key is required'
}
}
}
Check if the length of a given string falls between min and max ( You can pass min and max in the options )
{
// your field name for demo I am using *key*
key: {
length: {
min: 8,
max: 16,
errorMsg: 'key length should be between 8 and 16'
}
}
}
Check if a given string is a valid email or not
{
// your field name for demo I am using *key*
key: {
email: {
errorMsg: 'Please enter a valid email'
}
}
}
Check if a given string is a valid url or not
{
// your field name for demo I am using *key*
key: {
validUrl: {
errorMsg: 'key should be a valid URL'
}
}
}
Check if a given value is equals to
value provided
{
// your field name for demo I am using *key*
key: {
equals: {
to: 'password', // you can pass anything here for e.g. variables
errorMsg: 'key and other value are not same'
}
}
}
Check if a given value matches provided regex
{
// your field name for demo I am using *key*
key: {
regex: {
pattern: /^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,6}$/im,
errorMsg: 'Please enter a valid Phone Number'
}
}
}
microValidator focus is to become an extensible validation library for Javascript that is why there is not many inbuilt rules. You can always create your own custom rules or contribute to this project.
- My website (manojsinghnegi.com)
- Github (@manojsinghnegiwd)
- Twitter (@manojnegiwd)