Skip to content

danwillm/refute.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RefuteJS

Simple Data Validation

An example

Define how you want data to be structured:

const myRules = {
    firstname: new Refute.TypeCheck('Please enter a firstname').isString(),
    lastname: new Refute.TypeCheck('Please enter a lastname').isString(),
    contactDetails: {
        email: [
            new Refute.TypeCheck('Please enter an email').isString(),
            new Refute.PatternCheck('Please enter a valid email').isEmail()
        ],
        phone: [new Refute.PatternCheck('Please enter a valid phone number').isPhoneNumber()],
    }
};

//Could be the rules for allowing this data:

const validData = {
    firstname: 'John',
    lastname: 'Appleseed',
    contactDetails: {
        email: 'test@example.com',
        phone: '+001234567890'
    }
};

Validate the rules:

const result = Refute.validate(validData, myRules);

if(result.succeeded) {
    //All good!
} else {
    result.rulesFailed.forEach(rule => {
        //interrogate what rules failed
    })
}

Structure

  • Your rule object layout describes the structure you expect your data to be in
  • Rules of the same type can be chained together .isLengthGreaterThan(4).isLengthLessThan(100)
  • An array of rules acts as a logical "AND", where all array elements must pass for the data to be valid
  • An array inside an array element acts as a logical "OR", where only one of the elements needs to be successful to pass

About

A super simple data validation library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •