Giter VIP home page Giter VIP logo

validate-framework-utils's Introduction

validate-framework-utils

Some utils are used for validation

npm Travis npm

How to use?

yarn add validate-framework-utils
import Validator from 'validate-framework-utils';

const validator = new Validator();

const field = {
  rules: 'required | isEmail | maxLength(32)', // Asynchronous methods need to be put to the end
  messages: 'Can not be empty! | Please enter a valid email address. | Can not exceed {{param}} characters.',
};

const {
  result, // The verification result
  error, // contains the error message
} = await validator.validateByField(field)('[email protected]');

// ...

The field:

  • rules One or more rules (separated by | separated)
  • messages One or more messages (separated by | separated). {{Value}} is the value, and {{param}} is a parameter of like maxLength(32)
  • value The value to use for validation

Customize the validation method

Required the validation method begins with required

validator.addMethods({
  limitSelect(field, param) {
    return field.value.length <= param;
  },
  async validateFromServer(field, param) {
    return await doServerAPI();
  },
});

API

  • addMethods(methods)
  • removeMethods(...names)
  • validateByField(field) @return Promise

Built-in validation method

  • required
  • isAbc
  • isDate
  • isDecimal
  • isEmail
  • isInteger
  • isIp
  • isNumeric
  • isPhone
  • isTel
  • isUrl
  • maxLength(length)
  • minLength(length)
  • greaterThan(param)
  • lessThan(param)
  • greaterThanDate(date)
  • lessThanDate(date)

validate-framework-utils's People

Contributors

minjieliu avatar

Stargazers

 avatar  avatar

Watchers

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