1.1.1 • Published 6 years ago

bemu-validator v1.1.1

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

Bemu Validator

Bemu Validator is a simple, easy customizable JavaScript object validator. It contains many ways of validating predefined and also custom defined objects in a configurable manner. The package has no dependencies making it small and portable. Every method has multiple tests, with 100% code coverage making it somewhat well tested.

Setting up

To install the package simply do

npm i --save bemu-validator

After installaton you can require or import it to your project as

const bemuValidator = require('bemu-validator');

or

import bemuValidator from 'bemu-validator';

Or you can require any individual validator via the destructuring assignment syntax

// Require only the methods you need
const {
    isString,
    isNumber,
    isBoolean,
    isUrl,
    isEmail,
    isArray,
    objectValidator,
    stringValidator,
    numberValidator,
    booleanValidator,
    urlValidator,
    emailValidator,
    arrayValidator,
    customValidator
} = require('bemu-validator');

// Example with import

import {
    isString,
    isNumber
} from 'bemu-validator';

Example usage

In this advanced example we are creating a composite object validator and checking if an object is valid. In this case we are testing if a if objects are valid Books.

// Lets create a object validator to check if an object is a Book
const isBook = objectValidator({
    title: stringValidator(true, {
        minLength: 3,
        maxLength: 64,
        allowedCharacters: {
            allowLetters: true,
            allowNumbers: true,
            specialCharacters: [' ']
        }
    }),
    rating: numberValidator({
        min: 1,
        max: 5
    }),
    numberOfPages: numberValidator({
        min: 1,
        integer: true
    })
});

// This is a object which we want to test if it is really a valid book
const potentialBook = {
    title: 'A song of ice and fire',
    rating: 4.95,
    numberOfPages: 387
}

// To check if the object we recieved is a valid book we simply do
if(!isBook(potentialBook)) {
    console.log('The book we recieved is not valid!');
    
} else {
    console.log('The book we recieved is valid.'); // This will be logged
    
}

For additional examples and docs please refer to this page.

1.1.1

6 years ago

1.1.0

6 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago