0.1.1 • Published 3 years ago

refute.js v0.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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
0.1.0

3 years ago

0.0.3

3 years ago

0.1.1

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago