An easy use Validate
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
src
.babelrc
.editorconfig
.eslintrc.js
.gitattributes
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
README_CN.md
index.html
package.json
webpack.config.js
yarn.lock

README.md

mu2.validate

An easy use Validate.

mu2.validate

Installation

$ npm install mu2.validate

Usage

import validate from 'mu2.validate'

let obj = {
  email: 'mu2atgamil.com',
  password: '12334',
  repeat: '123345677',
  inviter: '123'
}

let condition = {
  email: {
    rules: 'required|isEmail',
    errors: 'Email cannot be empty|Please enter correct email'
  },
  password: {
    rules: 'required|minLength(6)|isAlphanumeric',
    errors: 'Password cannot be empty|Password length must at least 6|Password must include letters and numbers'
  },
  repeat: {
    rules: 'required|same(password)',
    errors: 'Repeat password cannot be empty|Password must be same'
  },
  inviter: {
    rules: 'isPhone',
    errors: 'Please enter correct phone number'
  }
}

let errors = validate(obj, condition)

// errors
[
  {
    "path": "email.isEmail",
    "error": "Please enter correct email"
  },
  {
    "path": "password.minLength(6)",
    "error": "Password length must at least 6"
  },
  {
    "path": "repeat.same(password)",
    "error": "Password must be same"
  },
  {
    "path": "inviter.isPhone",
    "error": "Please enter correct phone number"
  }
]

Option

  • errors - Error messages split with "|"
  • rules - Validation rules split with "|"
  • required
  • isArray
  • isNumber(len) - optinal: len, string length
  • isNumeric(len) - optinal: len, string length
  • isAlpha(len) - optinal: len, string length
  • isAlphanumeric(len) - optinal: len, string length
  • isEmail
  • isPhone
  • isChinese
  • isEqual(obj)
  • isNotEqual(obj)
  • isIn([])
  • isNotIn([])
  • maxLength(16)
  • minLength(6)
  • maxValue(99)
  • minValue(10)
  • same(path)
  • notSame(path)

Custom Regular Expression

{
  path: {
    rules: 'matchPhone|matchEmail|matchNumber',
    matchPhone: /\phone/,
    matchEmail: /\email/,
    matchNumber: /\number/,
    errors: 'not match phone|not match email|not match number'
  }
}

MIT license

Copyright (c) 2017 mufeng mufeng.me@gmail.com.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.