Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

check-data

Package Overview
Dependencies
Maintainers
1
Versions
75
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

check-data

JS数据验证器

  • 1.6.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

Installation

  npm install check-data --save

使用

let Validator = require('check-data')

let { error, data, $, ... } = Validator(data, options, constructor)

参数输入

  • data * - 导入验证数据,类型参考type选项

  • options * - 数据验证表达式,类型参考type选项

  • constructor Objcte - 自定义数据导出构造方法(可选)

结果输出

  • error String - 验证错误信息,验证成功时为null

  • data Objcte - 经过验证、处理后导出数据(空值过滤)

  • ${name} Objcte - 由constructor对象中构造器生成的对象,命名与构造器名称一致(空值过滤)

选项说明

当只需要验证数据类型时可以直接用type赋值,不需要使用对象表达式

通用选项

  • type * - 用于定义数据类型,可选类型有String、Number、Object、 Array、Date、Boolean、"ObjectId"、"MobilePhone",带引号字符串表示扩展类型(必选)

  • name String - 自定义参数名称,用于错误返回值中替换默认参数名

  • default * - 值为空时使用默认值

  • allowNull Boolean - 是否允许为空,默认为true

  • and Array - 依赖的参数名数组

  • or Array - 依赖的参数名数组

  • method Function - 参数自定义转换方法,空值时不执行

内置数据类型(用构造函数表示)

String
  • minLength Number - 限制字符串最小长度

  • maxLength Number - 限制字符串最大长度

  • reg RegExp - 正则表达式验证

  • in Array - 匹配多个可选值中的一个

Number
  • min Number - 限制最小值

  • max Number - 限制最大值

  • in Array - 匹配多个可选值中的一个

  • to * - 类型转换,仅支持Boolean值

Date、Array、Object、Boolean

仅支持类型验证

扩展类型(用字符串表示)

'ObjectId'

表示mongodb中的ObjectId

'MobilePhone'

表示手机号

使用示例

验证数据
  let data = {
     "tenderName": "测试",
     "tenderNum": "123456789987",
     "tenderEndTime": "2017-07-07T09:53:30.000Z",
     "files": ["abc.js", "334", "null", "666", , , "kkk.js"],
     "auth": {
           "weixin": "abc",
     },
     "beneficiariesName": "莉莉",
     "guaranteeMoney": 2,
     "guaranteeFormat": 0,
     "addressee": "嘟嘟",
     "receiveAddress": "北京市",
     "phone": "18666666666",
     "coupon": "uuuu",
     "integral": {
           "lala": 168,
           "kaka": "3"
     },
     "search": "深圳",
     "email": "xxx@xx.xx",
     "key": {
           a: "1",
           b: 2,
           c: 666,
           d: 4
     }
  }
验证数据表达式
  let { error, data, filter } = Validator(query,
     {
        "tenderName": {
           "type": String,
           "name": "标书名称",
           "allowNull": false
        },
        "tenderNum": String,
        "tenderEndTime": {
           "type": Date,
           "name": "截标时间",
           "allowNull": false,
        },
        "auth": {
           "weixin": String,
        },
        "beneficiariesName": String,
        "guaranteeMoney": {
           "type": Number,
           "in": [1, 2]
        },
        "files": [{
           "type": String,
           "allowNull": false,
        }],
        "guaranteeFormat": {
           "type": Number,
           "conversion": Boolean
        },
        "addressee": {
           "type": String,
        },
        "search": {
           "type": String,
        },
        "phone": {
           "type": "MobilePhone"
        },
        "receiveAddress": String,
        "coupon": {
           "type": String,
           method(value) {
              return { "$gt": new Date() }
           }
        },
        "integral": {
           "lala": {
              "type": Number,
           },
           "kaka": {
              "type": Number,
              "in": [1, 2, 3],
           }
        },
        "email": {
           "type": String,
           "default": "releaseTime",
           method(value) {
              return [value, , , "xx"]
           }
        },
        "key": {
           "$": {
              type: Number,
           }
        },
     },
     {
        filter() {
           let { search, email, integral } = this
           return {
              "email": email,
              "integral": integral,
              "test": {
                 a: 1,
                 b: undefined,
                 c: "",
                 d: null,
                 e: NaN,
                 e: 0,
              },
           }
        }
     }
  )

相同数据结构和类型的可复用表达式

  let { error, data } = Validator({
     "t0": false,
     "t1": false,
     "t2": true,
     "t3": true,
     "t4": false,
     "t5": true,
     "t6": true,
     "t7": false,
     "t8": true
  }, {
     $: {
        type: Boolean,
        allowNull: true,
     }
  })

数组验证

  let { error, data } = Validator(["a.js", "b.js", "c.js"], [String])

扩展数据类型验证

  let { error, data } = Validator({
     "id": "5968d3b4956fe04299ea5c18",
     "mobilePhone": "18555555555"
  }, {
     "id": "ObjectId",
     "mobilePhone": "MobilePhone"
  })

关联验证,用于存在参数依赖关系的非空值验证

  # 与
  let { error, data } = Validator({
     "username": "莉莉",
     "addressee": "嘟嘟",
  }, {
     "username": {
        "type": String,
        "and": ["addressee", "address"]
     },
     "addressee": {
        "type": String,
        "allowNull": true
     },
     "address": {
        "type": String,
        "allowNull": true
     }
  })

  # 或
  let { error, data } = Validator({
     "username": "莉莉",
     "addressee": "嘟嘟",
  }, {
     "username": {
        "type": String,
        "or": ["addressee", "address"]
     },
     "addressee": {
        "type": String,
        "allowNull": true
     },
     "address": {
        "type": String,
        "allowNull": true
     }
  })

Keywords

FAQs

Package last updated on 01 Sep 2017

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