Code Monkey home page Code Monkey logo

vschema's Introduction

vschema

Schema validation can be used to validate data coming from forms, HTTPS requests and basically anything that needs to match a specific schema.

Usage

var vschema = require('vschema');
var mySchema = {
	name: {
      type: 'alpha',
    },
    email: {
      type: 'email',
      required: true
    },
    phone: {
      type: 'number',
    },
    birthdate: {
      type: 'date',
    },
    gender: {
      type: 'radio',
      options: {
      	male: 'Male',
      	female: 'Female'
      }
    },
    favoriteColors: [{ // An array of elements
      type: 'string',
      validators: [ // Custom validation
        function(val){ // Hex Color
          return val.startsWith('#') && val.length === 7;
        }
      ],
      filters: [ // Custom filters
        function(val){
          return val.toLowerCase();
        }
      ]
    }]
};

var myData = {
	name: 'John',
    email: '[email protected]',
    phone: '001122334455',
    birthdate: '1985-01-01',
    favoriteColors: ['#AAAAAA', '#bbbbbb', '#cccccc']
};

// Validate myData against mySchema
vschema.validate(mySchema, myData)
.then(function(values){
	console.log(values); // Final values after applying filters on them
}, function(errors){
	console.log(errors);
});


// You can also validate a single field like this:
/*
vschema.validateField({
	name: 'fieldname',
	type: 'email'
}, 'invalidEmailAddress').then(...);
*/

Built in data types

  • string
  • number
  • integer
  • float
  • select
  • radio
  • checkbox
  • bool
  • date
  • color
  • email
  • alpha
  • alnum
  • decimal
  • mongoid
  • url
  • uuid
  • schema

Built in filters

  • escape: replace <, >, &, ', " and / with HTML entities
  • unescape: replaces HTML encoded entities with <, >, &, ', " and /
  • ltrim: trim characters from the left-side of the input
  • normalizeEmail: canonicalizes an email address
  • rtrim: trim characters from the right-side of the input
  • stripLow: remove characters with a numerical value < 32 and 127, mostly control characters
  • toBoolean: convert the input string to a boolean. Everything except for '0', 'false' and '' returns true
  • toDate: convert the input string to a date, or null if the input is not a date
  • toFloat: convert the input string to a float, or NaN if the input is not a float
  • toInt: convert the input string to an integer, or NaN if the input is not an integer
  • trim: trim characters (whitespace by default) from both sides of the input

Custom validators

In addition to built in validators, you can also use your own either synchronously or asynchronously:

vschema.validateField({
	name: 'myfield',
	type: 'string',
	validators: [
	  function(val){ // Synchronous validation: either return true or anything else describing the error
	  	return true;
	  },
	  function(val){ // To do async validation, simply return a promise
	    return new Promise(function(resolve, reject){
	      setTimeout(function(){
	        reject('Invalid');
	      }, 200);
	    });
	  }
]
}, 'hello');

Custom filters

Please see the usage section

vschema's People

Contributors

abdelilah avatar

Watchers

 avatar James Cloos 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.