Giter Site home page Giter Site logo

schema-validator's Introduction

@nib/schema-validator

A Universal-JavaScript utility for validating value objects.

Installation

npm install --save @nib/schema-validator

Usage

const validator = require('@nib/schema/validator');
const validate = require('@nib/validation-methods');

function trim(value) {
  return value.trim();
}

function stripWhitespace(value) {
  return value.replace(/\s*/g, '');
}

const schema = {

  firstName: {
    filters: [trim],
    validators: [
      [validate.minlength(5), 'First name must be at least 5 characters']
    ],
    empty: {
      default: null
    }
  },

  lastName: {
    filters: [trim],
    validators: [
      [validate.minlength(5), 'Last name must be at least 5 characters']
    ],
    empty: {
      default: null
    }
  },

  phoneNumber: {
    filters: [stripWhitespace],
    validators: [
     [validate.maxlength(10), 'Phone number must be no more than 10 digits']
    ],
    empty: {
      default: null
    }
  },

  email: {
    filters: [trim],
    validators: [
      [validate.email, 'Email must be a valid email address']
    ],
    empty: {
      default: null
    }
  }

};

const values1 = {
  firstName: 'Homer',
  phoneNumber: '02 9999 5555',
  email: 'homer.$#%@!'
};

validator.validate(schema, values1).then(result => {
  console.log(result.valid);  //false
  console.log(result.values); //{firstName: 'Homer', lastName: null, phoneNumber: '0299995555'}
  console.log(result.errors); //{email: 'Email must be a valid email address'}
});

const values2 = {
  firstName: 'Homer',
  lastName: 'Simpson',
  email: '[email protected]'
};

validator.validate(schema, values2).then(result => {
  console.log(result.valid, result.values, result.errors);
  console.log(result.valid);  //true
  console.log(result.values); //{firstName: 'Homer', lastName: 'Simpson, phoneNumber: null, email: '[email protected]'}
  console.log(result.errors); //{}
});

API

.validate(schema, values) : Promise

Validate all the fields, even where values are not provided.

schema-validator's People

Contributors

ramesius avatar

Watchers

Tomas McKinless 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.