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 🙏

© 2026 – Pkg Stats / Ryan Hefner

ipa.js

v4.2.1

Published

IPA interface manager

Readme

IPA.js数据校验库v3.2.0

官方文档地址

  • 更快开发
  • 更易维护
  • 更鲁棒
           _____                      _____ _____                _____ _____           
          /\    \                    /::::::\    \              /::::::\    \          
         /::\    \                  /::::::::\    \            /::::::::\    \         
        /:::/    /                 /:::/--\:::\    \          /:::/--\:::\    \        
       /:::/    /                 /:::/   _\:::\    \        /:::/   _\:::\    \       
      /:::/____/                 /:::/   /  |:::|    |      /:::/   /  \:::\    \      
     /::::\    \                /:::/   /   |:::| __ |     /:::/   /   /::::\    \     
    /::::::\    \              /:::/   /   /:::/    /     /:::/   /   /::::::\    \    
   /:::/\:::\    \            /:::/___/   /:::/    /      |:::|   |  /:::/\:::\    \   
   \::/  \:::\    \           \:::\   \  /:::/    /       |:::|   | /:::/  \:::\____\  
    \/___/\:::\    \ ____      \:::\   \/:::/    /        \:::\   \/:::/    \::/    /  
           \:::\    /\    \     \:::\  /:::/    /          \:::\  /:::/    / \/____/   
            \:::\  /::\ ___\     \:::\/:::/    /            \:::\/:::/    /            
             \:::\/:::/    /      \::::::/    /              \::::::/    /             
              \::::::/    /        \::::/____/                \::::/____/              
               \::::/    /          \:::\    \                 \:::\    \              
               /:::/    /            \:::\    \                 \:::\    \             
              /:::/    /              \:::\    \                 \:::\    \            
             /:::/    /                \:::\____\                 \:::\____\           
             \::/    /                  \::/    /                  \::/    /           
              \/____/                    \/____/                    \/____/            

什么是IPA.js

IPA.js是一个数据结构校验库,可以同时运行于浏览器端和node环境

它通过一种类似Mongoose.Schema的简单易懂语法来声明对数据结构的要求:

const personSchema = new IPA({
    name: String,
    age: Number,
    girlfriends: [String],
});

并通过checkguaranteemock三种方法来分别实现对数据的深层校验保障自动生成

  • check方法校验数据结构的合法性:
personSchema.check({
    name: '李雷',
    age: 13,
    girlfriends: ['韩梅梅', '钟梅梅', '李梅梅'],
    location: '上海',
}); // true
  • guarantee方法保障数据的合法性:
personSchema.guarantee({
    name: '李雷',
    age: '13',
    girlfiends: null,
    location: '上海',
});

// { 
//     name: '李雷',
//     age: 13,
//     girlfriends: [],
//     location: '上海',
// }
  • mock方法生成随机的合法数据:
personSchema.mock();

// { 
//     name: 'magna',
//     age: 13,
//     girlfriends: ['ipsum', 'ad', 'veniam']
// }

除了对象深层校验,IPA还对数组提供了强大的长度校验、保障和生成机制:

const tableSchema = new IPA({
    thead: [String, 'cols'],
    tbody: [[Number, 'cols'], 'rows'],
});

const table = {
    thead: ['列1', '列2'],
    tbody: [[3, 10], [2, 4, 6], [8, 9]],
}

tableSchema.check(table); // false

tableSchema.guaratee(table);
// {
//     thead: ['列1', '列2'],
//     tbody: [[3, 10], [2, 4], [8, 9]],
// }

tableSchema.mock({ cols: 3, rows: 2 }); // 指定生成的长度
// {
//     thead: ['irure', 'mollit', 'aute'],
//     tbody: [[6, 1, 7], [2, 9, 4]],
// }

通过上述核心功能,IPA在完成繁琐易漏的数据结构校验的同时,帮助增强模块的容错能力,并提高开发效率。使得端对端/多模块的工程在开发层面和稳定性层面同时解耦。

为了方便在多种场景下实现对复杂数据结构的校验,IPA还提供了一系列易用的内置类型校验和默认值校验,并且支持实例间的相互嵌套:

例如:

const { or, Range, Integer } = IPA;

const unitSchema = new IPA({
    id: or(String, Number),
    value: 0,
    count: Integer,
    type: Range(1, 4),
});

const listSchema = new IPA([
    unitSchema,
]);

IPA还支持具有高扩展性的规则自定义。在模板编译阶段,它将解析模板语法的compile函数作为参数传入用户自定义的校验函数,获得返回的验证规则,这使得构造自定义的嵌套规则成为可能。

如下示例了一个可以对校验规则进行与操作的函数,并基于此生成了一个可以用来校验是否是合法的ASCII码值的校验器:

function and (...templates) {
    return (compile) => {
        const rules = templates.map(template => compile(template));
        return {
            check(val) {
                return rules.filter(rule => rule.check(val) === false).length === 0;
            } 
        }
    }
};

function RangeInt (min, max) {
    return and(IPA.Range(min, max), IPA.Integer);
}

const ASCIICodeSchema = new IPA(RangeInt(0, 127));

如下示例了一个标准的HTTP响应数据的基本结构,以及针对不同响应类型的扩展结构:

const Res = (subtemplate = Object) => {
    return (compile) => {
        return compile({
            code: Number,
            msg: String,
            data: subtemplate,
        });
    };
};

const dataSchema1 = new IPA(Res(String));
const dataSchema2 = new IPA(Res({
    name: String,
    value: Number,
}));

此外,IPA提供了如全局注入开发环境设置等功能来贴合工程化开发场景中的需求,更多IPA的语法细节和工程化用法请见IPA详细文档