Git Product home page Git Product logo

bailer's Introduction

Build Status

bailer logo

bailer

A lightweight validation library. Inspired by leonardoborges's bouncer for Clojure.

Setup

npm install bailer

Usage

Built-in validators are:

  • bailer.validations.required Validates when not null or undefined
  • bailer.validations.email Check email format via a regexp
  • bailer.validations.number Validates when typeof returns "number"
  • bailer.validations.string Validates when typeof returns "string"
  • bailer.validations.object Validates when typeof returns "object". Arrays are objects.

You can create your own (see here).

var b = require('bailer'),
    v = b.validations;

// Define a dummy object.
var person = {name: "John", age: 20,
              corporation: '',
              email: "[email protected]"};

// Check if it matches the schema.
b.validate(person, {
  name: [v.required],
  age: [v.required],
  email: [v.email]
});

// Every validations passed.
// Result:
null

Errors

b.validate(person, {
  name: [b.required],
  corporation: [b.required],
  firstname: [b.required]
});

// Uh-oh. The person object object doesn't have a truthy corporation
// (it is empty) or firstname (it is undefined).
// Result:
{corporation: ["corporation must be present"],
 firstname: ["firstname must be present"]}

You can use a custom message.

b.validate(person, {
  corporation: [b.required, "What! You don't work for a corporation?!"]
});
// which results in:
{corporation: ["What! You don't work for a corporation?!"]}

Custom validators

A custom validator is a function with this signature:

function customValidator(obj, attributeName) {
  // Do your stuff here
  // ...
  var correctResult = obj[attributeName].length > 3;

  // If the provided value is valid, return null.
  // Otherwise, return a description.
  return correctResult ? null : "".concat(attributename, " should contains at least 3 elements");
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.