npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

psv

v4.1.3

Published

porco schema validate

Downloads

31

Readme

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 目录下代码)