New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vulidate

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vulidate

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

  • 0.2.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
73
decreased by-31.13%
Maintainers
1
Weekly downloads
 
Created
Source

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
}

Keywords

FAQs

Package last updated on 04 Jun 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc