vulidate

0.2.3 • Public • Published

vulidate

A pure, extendable, very useful validator base Promise and Decorator

  • Object literals : Base es7 Decorator, we can add a decorator on a object literals. Vulidate will add some unenumerable keys to store rules.
  • Asynchronous : Vulidate support async validator function
  • Ordered : When you call $validate function, Vulidate will exec validator function in ordered.

Install

npm install vulidate --save
npm install @babel/plugin-proposal-decorators --save-dev

Usage

config babel.config.js

plugins: [
  [
    '@babel/plugin-proposal-decorators',
    {
      legacy: true
    }
  ]
]

For example, we validate a user object.
nickname and phone is required, also phone is validate from remote server.
import vulidate from 'vulidate'

const requiredRule = {
  validator: val => val != null && val !== '',
  message: 'required'
}
const required = vulidate(requiredRule)
const checkPhone = vulidate(value => fetch(`xxx/${value}`))

const user = {
  @required('nickname is required')
  nickname: '',
  @checkPhone('phone valid fail')
  @required
  phone: ''
}

user
  .$validate()
  .then(/* success */)
  .catch(reason => {
    /* fail */
    alert(reason[0].message)
  })

Interface

arguments structure, describe with typescript interface.

interface vulidate {
  (rule: Validator | Rule): AppendDecorator
}

interface ProxyObject {
  readonly __rules: Map<string, Rule[]>
  readonly $validate(): Promise<ResolvedSource[]>
}

interface Decorator {
  (target: ProxyObject, property: string): void
}

interface AppendDecorator {
  (args: string | Rule | any): Decorator
}

interface Validator {
  (value: any, source: Source): boolean | Promise<any>
}

interface Rule {
  validator: Validator
  message?: string
}

interface Source extends Rule {
  value: any
}

interface ResolvedSource extends Source {
  reason?: any
}

Package Sidebar

Install

npm i vulidate

Weekly Downloads

108

Version

0.2.3

License

MIT

Unpacked Size

14.5 kB

Total Files

6

Last publish

Collaborators

  • zeusshuai