GithubHelp home page GithubHelp logo

ms88privat / validate-model Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danielweinmann/validate-model

0.0 2.0 0.0 5 KB

Validate model objects with validator.js

License: MIT License

JavaScript 100.00%

validate-model's Introduction

validate-model

Validate model objects with validator.js. Read validator.js documentation to see available validators.

Installation

npm install validate-model --save

Usage

var ValidateModel = require('validate-model');
var validate = ValidateModel.validate;
var validateAll = ValidateModel.validateAll;

var UserValidators = {
  name: {
    title: 'Name',
    validate: [{
      validator: 'isLength',
      arguments: [1, 255],
    }]
  },
  email: {
    title: 'Email',
    validate: [{
      validator: 'isLength',
      arguments: [20, 255],
      message: '{TITLE} is too short'
    },
    {
      validator: 'isEmail',
      message: '{TITLE} must be valid'
    }]
  },
  password: {
    title: 'Password',
    validate: [{
      validator: 'isLength',
      arguments: [8, 255],
      message: '{TITLE} is too short'
    }]
  }
};

var user = {
  name: 'Foo',
  email: 'invalid@email',
  password: 'short'
};

var nameValidation = validate(UserValidators.name, user.name);
// { valid: true, messages: [] }

var emailValidation = validate(UserValidators.email, user.email);
// { valid: false, messages: ['Email is too short', 'Email must be valid'] }

var passwordValidation = validate(UserValidators.password, user.password);
// { valid: false, messages: ['Password is too short'] }

var userValidation = validateAll(UserValidators, user)
// { valid: false, messages: {email: ['Email is too short', 'Email must be valid'], password: ['Password is too short']}}

Inspiration

This package is inspired by the way FaridSafi/react-native-gifted-form implements form validation.

Contributing

Please create issues and send pull requests!

License

MIT

validate-model's People

Contributors

danielweinmann avatar

Watchers

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