13 Star 50 Fork 6

porco / psv

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Porco Schema Validate (psv)

(自2.1.7版本之后,数组的定义改变,支持数组嵌套)

psv 是一款轻量级 js 数据格式验证工具,相比于其他功能齐备的验证工具,psv 的优势在于体积非常小(之所以要写它,就是因为对其他 validate 框架不满意,我只是想要一个格式化验证工具,却给我一个 1M 的项目),最开始的核心代码只有 130 行。

下载、安装

npm install psv --save
yarn add psv

使用

首先你需要定义出自己的 schema,比如:

var schema = {
    str: {
        type: String,             // string 类型
        required: true,           // 是否必填,默认为 false
        max: 3,                   // 最大长度
        min: 2,                   // 最小长度
        enum: ['12', '13', '14'], // 枚举
        regex: '^[8-9]*$',        // 正则
        // 正则(兼容老版本,不推荐使用,优先级低,当它与 regex 同时出现时会被忽略)
        pattern: '^[5-9]*$',
        error: {                  // 自定义错误提示
            type: '类型错误',
            required: '必填错误',
            max: '不能超过最大长度',
            min: '不能低于最小长度',
            enum: 'enum 必须正确',
            regex: '正则验证错误',
            pattern: '正则验证错误',
        }      
    },
    num: {
        type: Number,
        required: true,
        max: 3,           // 最大长度
        min: 1,           // 最小长度
        enum: [1, 2, 3],  // 枚举
        error: {          // 自定义错误提示
            type: '类型错误',
            required: '必填错误',
            max: '不能超过最大值',
            min: '不能低于最小值',
            enum: 'enum 必须正确',
        },
    },
    boo: {
        type: Boolean, 
        required: true,
        error: {          // 自定义错误提示
            type: '类型错误',
            required: '必填错误',
        },
    },
    arr: {
    	type: Array, 
        required: true,
        max: 3,          // 最大长度
        min: 2,          // 最大长度
        error: {          // 自定义错误提示
            type: '类型错误',
            required: '必填错误',
            max: '不能超过最大长度',
            min: '不能低于最小长度',
        },
    },
    obj: {
    	type: Object, 
        required: true,
        error: {          // 自定义错误提示
            type: '类型错误',
            required: '必填错误',
        },
    }
};

schema 是预先定义的数据格式,接下来将会拿 data 与 schema 进行比对, 对于一个 schema 来说只有 type 字段是必填的,其他都可以缺省。

var data = {
    str: '12',
    num: 2,
    boo: true,
    array: [1, 2],
    obj: {},
}

接着我们导入并创建 Psv 对象进行验证

import Psv from 'psv';
function testPsv(schema, data) {
	const psv = new Psv(schema, data);
	const validate = psv.validate();
	if (!validate) {
		psv.printErrors();
	}
}

api

  • 数据类型

    • String
      • type
      • default // 默认值
      • trim // 去除数据两边空格,默认 false
      • required
      • max
      • min
      • enum
      • regex
      • pattern
      • error (自定义错误提示,可使用默认值)
        • type (type 错误提示信息)
        • required (required 错误提示信息)
        • max (max 错误提示信息)
        • min (min 错误提示信息)
        • enum (enum 错误提示信息)
        • regex (regex 错误提示信息)
        • pattern (pattern 错误提示信息)
    • Number
      • type
      • default
      • required
      • max
      • min
      • enum
      • error
        • type
        • required
        • max
        • min
        • enum
    • Array
      • type
      • default
      • required
      • max
      • min
      • error
        • type
        • required
        • max
        • min
    • Boolean
      • type
      • default
      • required
      • error
        • type
        • required
    • Object
      • type
      • default
      • required
      • error
        • type
        • required
  • 函数

    • validate // 验证入口
    • printErrors // 打印错误信息
    • getErrors // 获取错误信息

注意:当 type = Object 时,说明该字段可以是任何 js 基本类型或对象,甚至可以是 一个 函数(慎用)。

4.1.0 之后,全面支持 default 默认值,default 可以是任何值,String 添加 trim 去空格支持。default 以及 trim 都支持无限嵌套

const schema = {
    key1: {
        type: String,
        default: '123',
    }
};
const data = {};
const psv = new Psv(schema, data);
const res = psv.validate();
// data.key1 === '123'
const schema = {
    key1: {
        type: String,
        trim: true,
    }
};
const data = {
	key1: '  123'
};
const psv = new Psv(schema, data);
const res = psv.validate();
// data.key1 === '123'

同样,psv 支持嵌套定义

const schema2 = {
    str2: {
        type: String,
        required: true
    }
}
const schema = {
    str1: {
        type: schema2,
        required: true
    },
};

(如果对使用有疑问,可以参考 test 目录下代码)

MIT License Copyright (c) 2018 creativeLabs Porco Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

js 数据 格式 验证 展开 收起
JavaScript
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/GTchao555/psv.git
git@gitee.com:GTchao555/psv.git
GTchao555
psv
psv
master

搜索帮助