Giter VIP home page Giter VIP logo

object-validator's Introduction

object-validator rules

Build Status

Validate plain objects against a set of rules. This component just takes an object and a set of rules like this:

var schema = {
  "email": {
    "Email is required": function(val) {
      return val != null;
    },
    "Email is invalid": function(val) {
      return isEmail(val);
    }
  }
};

and returns errors like this:

{
  "email": "Email is required"
}

This component contains no validation methods itself. You need to bring your own. This just runs an object through functions and returns the messages.

Installation

$ component install nib-components/object-validator

API

var validate = require('object-validator');
validate(schema, data);

The function takes two parameters, a schema (the rules for validation) and the data to validate.

Validation Rules

Schemas are just objects:

var schema = {
  "phoneNumber": {
    "This field is required": function(val) {
      return val != null;
    },
    "Please enter a valid phone number": function(val){
      return /0-9{10}/.test(val);
    },
    "Confirmation doesn't match": function(val, data){
      return val === data['confirmPhoneNumber'];
    }
  }
};

The keys of the schema match keys in the data object you are validating. The value is an object of messages with a corrosponding validation method.

Validation Functions

The validation method has the signature:

function(value, data) {

}

This function will be passed the value of the attribute and all of the data that was submitted for validation. This function should return false if the validation fails, anything else will be treated as successful and an error message won't be created.

Interpolation

Messages can access the value that was sent if they wish to be dynamic:

"confirmEmail": {
  "{{value}} does not match email": function(){}
}

{{value}} will be replace with whatever the value is for confirmEmail.

Example Schema

var schema = {
  "givenName": {
    "Given name is required": required,
    "Given name must only contain a-zA-Z": isString
  },
  "phoneNumber": {
    "Please enter a valid phone number e.g. 02 1234 5678": phoneNumber
  },
  "reason": {
    "Reason is required": required,
    "Must be less than 500 characters": maxLength(500)
  },
  "email": {
    "Email must be 10 characters long": length(10),
    "Email address is required": required,
    "Email must equal [email protected]": equals("[email protected]")
  },
  "confirmEmail": {
    "{{value}} does not match email": matches('email')
  },
  "partnerGender": {
    "Partner gender is required": function(partnerGender, data){
      if(data.hasPartner && !partnerGender) {
        return false;
      }
    }
  },
};

Functions as Rules

You can also use functions instead of objects for the attribute rules. If this function returns a message it will be treated as invalid the the message will be use.

var schema = {
  "title": function(title) {
    if(!title) {
      return "Title is required";
    }
  },
  "givenName": function(val){
    if(!val) {
      return "Given name is required";
    }
    if(isString(val) === false) {
      return "Given name must only contain a-zA-Z";
    }
  },
};

Use this syntax if you need advanced or unique validation requirements.

License

MIT

object-validator's People

Contributors

anthonyshort avatar chrisbuttery avatar

Watchers

 avatar  avatar

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.