All Projects → hexindai → Egg Valid

hexindai / Egg Valid

Licence: mit
👮Validation plugin for eggjs

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Egg Valid

egg-swagger
swagger-ui plugin for egg ,Demo:
Stars: ✭ 26 (+160%)
Mutual labels:  egg, egg-plugin
egg-rest
Restful API plugin for egg
Stars: ✭ 106 (+960%)
Mutual labels:  egg, egg-plugin
egg-userservice
userservice plugin for egg
Stars: ✭ 21 (+110%)
Mutual labels:  egg, egg-plugin
egg-view-pug
egg view plugin for pug.
Stars: ✭ 24 (+140%)
Mutual labels:  egg, egg-plugin
Egg Mongoose
Stars: ✭ 386 (+3760%)
Mutual labels:  egg, egg-plugin
egg-oracle
OracleDB plugin for egg
Stars: ✭ 23 (+130%)
Mutual labels:  egg, egg-plugin
egg-view-vue-ssr
Egg Vue Server Side Render (SSR) Plugin
Stars: ✭ 90 (+800%)
Mutual labels:  egg, egg-plugin
egg-vue-webpack-dev
基于egg + vue2 + webpack2 的前后端集成开发编译构建插件
Stars: ✭ 29 (+190%)
Mutual labels:  egg, egg-plugin
Egg Graphql
Stars: ✭ 345 (+3350%)
Mutual labels:  egg, egg-plugin
Egg Mysql
MySQL plugin for egg
Stars: ✭ 276 (+2660%)
Mutual labels:  egg, egg-plugin
egg-elasticsearch
elasticsearch client for egg.js
Stars: ✭ 22 (+120%)
Mutual labels:  egg, egg-plugin
Egg Restfulapi
🏅 基于Egg.js 2.0 & {mongoose,jwt}RESTful API 模板,用于快速集成开发RESTful前后端分离的服务端。
Stars: ✭ 524 (+5140%)
Mutual labels:  egg, egg-plugin
egg-view-react
egg view plugin for react
Stars: ✭ 89 (+790%)
Mutual labels:  egg, egg-plugin
egg-parameters
Merge all parameters (ctx.params, ctx.request.query, ctx.request.body) into ctx.params like Rails application.
Stars: ✭ 24 (+140%)
Mutual labels:  egg, egg-plugin
egg-mailer
🥚 mailer plugin for egg
Stars: ✭ 23 (+130%)
Mutual labels:  egg, egg-plugin
egg-y-validator
☯️ Egg Magic Validator (Egg 魔法验证工具)
Stars: ✭ 30 (+200%)
Mutual labels:  egg, egg-plugin
egg-session-redis
redis store for egg session
Stars: ✭ 41 (+310%)
Mutual labels:  egg, egg-plugin
egg-kafkajs
☎️kafka plugin for eggjs
Stars: ✭ 26 (+160%)
Mutual labels:  egg, egg-plugin
egg-session
session plugin for egg
Stars: ✭ 48 (+380%)
Mutual labels:  egg, egg-plugin
Egg 24time
A Twitter-like news and social server for Egg. 微信小程序社区全栈解决方案
Stars: ✭ 493 (+4830%)
Mutual labels:  egg, egg-plugin

egg-valid

A better Validation egg plugin based on @killara/validation

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-valid -S

Usage

// config
exports.valid = {
  enable: true,
  package: 'egg-valid',
};
// controller
class HomeController extends Controller {
  async index() {
    const { app, ctx } = this;
    const rule = { username: 'required|alpha' };
    const errors = await app.validator.validate(ctx.request.body, rule)
    // ...
  }
}

Rule

Derived from @killara/validation

  • accepted

    • string style: field: 'accepted'
    • object style: field: { accepted: true }
  • alpha

    • string style: field: 'alpha:6' or field: 'alpha:len=6'
    • object style: field: { alpha: { len: 6 } }
  • confirmed

    Rule confirmed: the field need to have the same value as the value that be filled by field_confirmation. We can change field_confirmation to any names with confirmed:"custom"

    • string style: field: 'confirmed' or field: 'confirmed:"custom_field_name"'
    • object style: field: { confirmed: "custom_field_name" }
  • date

    • string style: field: 'date'
    • object style: field: { date: true }
  • datetime

    • string style: field: 'datetime'
    • object style: field: { datetime: true }
  • time

    • string style: field: 'time'
    • object style: field: { time: true }
  • email

    • string style: field: 'email:true'
    • object style: field: { email: true }
  • in

    • array style: field: [ 'basketball', 'football' ]
    • object style: field: { in: [ 'basketball', 'football' ] }
  • money

    • string style: field: 'money' or field: 'money:0' field: 'money:2' (default)
    • object style: field: { money: { decimal: true } } or field: { money: { decimal: 0 } } or field: { money: { decimal: 2 } }
  • numeric

    • string style: field: 'numeric:6' or field: 'numeric:len=6'
    • object style: field: { numeric: { len: 6 } }
  • regexp

    • string style: field: 'regexp:"^123456$"'
    • object style: field: { regexp: new RegExp(/abc/, 'i') } or field: { regexp: /^[0-9a-zA-z]{8,16}$/ }
  • required

    • string style: field: 'required' or field: 'required:true'
    • object style: field: { required: true }

More rules

Custom rules

Messages

Customize validation messages

Via API

class HomeController extends Controller {
  async index() {
    const { app, ctx } = this;
    const rule = { username: 'required|alpha:6' };
    const messages = { 'username.alpha': '该字段应该为长度为6的字母串' };
    const errors = await app.validator.validate(ctx.request.body, rule, messages);
    if (errors) {
      // ...
    } else {
      // ...
    }
  }
}

Via configuration

exports.valid = {
  rules: {
    custom: field => context => params => {
      // We can get `app` and `ctx` from context
      const { app, ctx, options } = context;
      //... return a boolean
    }
  },
  messages: {
    custom: 'a custom message...',
  },
};

API

validation.addRule(name: string, ruleFunc: (field: string) => (context: object) => (params: object) => bool)

  • Add custom rule

validation.addMessage(name: string, message: string)

  • Add custom message

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].