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

schema.io

v0.6.9

Published

schema.io是一个js基本数据结构的校验库

Downloads

58

Readme

schema.io

NPM version

1. 这是什么?

schema.io是一个js基本数据结构的校验库。

2. 业务场景?

schema.io可用于所有需要在运行时对结构化数据进行校验的场景;如http请求的参数校验等。

3. 怎么使用?

// 使用 yarn 或 npm 进行安装后导入
import Schema from 'schema.io';

// 创建结构化的数据模板用于校验
const schemaType = {
  foo: String,
  bar: Number,
  complex: [{
    key: {
      foobar: Boolean,
    },
  }]
};

// 创建校验器实例
const schema = new Schema(schemaType);

// 进行数据校验
try {
  const parsedValue = schema.validate(value);
} catch(e) {
  // 校验错误
}

4. 支持的数据结构

null String Number Boolean Date Object {} *alias Object* Array [] *alias Array*

可以通过使用{}[]嵌套更加复杂的数据结构。

注意:当使用[...elements]来声明数据结构时,若数组长度大于1,则被解释为【满足数组中任一模板】,此时不作为数组校验。

如:[String]表示校验数据为数组,并且每一项为字符串;[String, Number]表示校验数据为字符串或数字;[[String, Number]]则表示校验数据为数组,并且每一项为字符串或数字。

5. 怎么写模板?

  • 直接使用上述支持的数据结构组合模板
// 举个栗子
String // 匹配字符串
[Number] // 匹配数字数组
{foo: Boolean} // 匹配对象,含有一个成员foo的类型是布尔值
[Number, {key: String}] // 匹配数字 或者 含有成员key为字符串的对象
  • 使用结构化的模板声明方式
const schemaType = {
    type: any; // 定义匹配的类型。可以是支持的基本数据结构,也可以是结构化的模板声明
    $type: any; // alias: type  为了避免特定情况下对象数据结构的key冲突,推荐使用这种方式代替type
    enums?: any[]; // 定义匹配的枚举值
    invalid?: any[]; // 定义不可匹配的值
    match?: RegExp; // 定义匹配的正则表达式(针对于字符串)
    disallowEmpty?: boolean; // 定义是否允许空字符串(针对于字符串)
    max?: number; // 定义最大值(针对于数字)
    min?: number; // 定义最小值(针对于数字)
    default?: any; // 定义默认值
    required: boolean; // 定义匹配值是否可以为undefined
    explain?: string; // 定义模板的描述
    error?: string; // 定义匹配失败时的自定义错误信息
}
  • 使用Type生成器快速生成模板
import { $, Type } from 'schema.io';

const schemaType1 = new Type({
  foo: String,
}).required().explain('schema type 1');

const schemaType2 = $(String).optional().explain('schema type 2');

6. 附加功能

Schema.example()

根据提供的数据结构校验器,生成符合模板的数据。

const exampleData = new Schema(schemaType).example()
Schema.summary()

根据提供的数据结构校验器,生成结构化的数据模板描述。可用于生成数据结构文档。

const summary = new Schema(schemaType).summary()