0.6.4 • Published 6 years ago

validate-framework-utils v0.6.4

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

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)('example@example.com');

// ...

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)
0.6.4

6 years ago

0.6.3

6 years ago

0.6.2

6 years ago

0.6.1

6 years ago

0.6.0

7 years ago

0.5.0

7 years ago

0.4.0

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.1

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.3

7 years ago

0.2.0

7 years ago

0.1.1

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago