@0x4447/pretzel 中文文档教程

发布于 6年前 浏览 34 项目主页 更新于 3年前

Pretzel

在意识到没有 npm 模块可以递归验证具有多个嵌套值对象的 JSON 对象后,我们创建了 Pretzel 递归 JSON 数据验证。

我们知道 Validate.js,但正如作者所解释的,对完整对象的支持非常基础。 我们确实考虑过向该项目提出 PR,但事实证明源代码太难重新分区了。 因此,Pretzel 诞生了。

该工具的目标不仅仅是处理这样的嵌套 JSON

{
    "company_name": "0x4447",
    "address": {
        "street": "42 Life",
        "state": "TX",
        "code": 123456,
        "country": "United States",
        "contact": {
            "email": "hello@0x4447.email",
            "phone_nr": "+1 (555) 555-6666"
        }
    },
    "employs": {
        "ceo": {
            "individual": {
                "first_name": "David",
                "last_name": "Gatti"
            },
            "assistant": {
                "first_name": "Very",
                "last_name": "Helpful"
            }
        }
    }
}

......

{
    company_name: {
        type: 'string'
    },
    address: {
        street: {
            type: 'string'
        },
        state: {
            includes: ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
        },
        code: {
            type: 'number'
        },
        country: {
            includes: ['United States', 'Canada']
        },
        contact: {
            email: {
                type: 'string'
            },
            phone_nr: {
                type: 'string'
            }
        }
    },
    employs: {
        ceo: {
            individual: {
                first_name: {
                    type: 'string'
                },
                last_name: {
                    type: 'string'
                }
            },
            assistant: {
                first_name: {
                    type: 'string'
                },
                last_name: {
                    type: 'string'
                }
            }
        }
    }
}

文件 如果您检查 index.js 文件,您就会明白我们的意思。 使用它,修改它,从中学习。

How to Install

] npm install @0x4447/pretzel

How to Test

] npm run test

How to Require

let pretzel = require('pretzel');

How to Use

let pretzel = require('pretzel');

//
//    RULES
//
let rules = {
    name: {
        type: "string"
    }
};

//
//    Good DATA
//
let good_data = {
    name: '0x4447'
};

//
//    Bad DATA
//
let bad_data = {
    name: 123321
};

//
//    1.  Execute validation.
//
let good = pretzel(good_data, rules);
let bad = pretzel(bad_data, rules);

//
//    2.  Check the validation result.
//
console.info(good);
console.info(bad);

Rules

以下是模块目前支持的所有规则的列表。

Bigger than

{
    biggerThan: 5
}

Smaller than

{
    smallerThan: 5
}

Comparison

{
    comparison: 1
}

Includes

{
    includes: [9, '0x4447']
}

Regular Expression

{
    regexp: '[aAzZ.-]'
}

Types

{
    type: "string || number || object || array"
}

The End

如果您喜欢这个项目,请考虑给它一个????。 并查看我们的 0x4447 GitHub 帐户,我们在其中提供您可能会觉得有用或有趣的其他资源。

该项目由 0x4447 LLC 提供,这是一家专门在 AWS 上构建自定义解决方案的软件公司。 通过以下链接了解更多信息:https://0x4447.com 或者说 hello@0x4447.email

???? Pretzel

We created our Pretzel recursive JSON data validation after realizing that there were no npm modules that could recursively validate a JSON object with multiple nested values objects.

We are aware of Validate.js, but as the author explains, the support for complete object is very basic. We did consider proposing a PR to the project, but the source code turned out to be too hard to rezone about. Thus, Pretzel was born.

The goal of this tool is not just to handle nested JSON files like this…

{
    "company_name": "0x4447",
    "address": {
        "street": "42 Life",
        "state": "TX",
        "code": 123456,
        "country": "United States",
        "contact": {
            "email": "hello@0x4447.email",
            "phone_nr": "+1 (555) 555-6666"
        }
    },
    "employs": {
        "ceo": {
            "individual": {
                "first_name": "David",
                "last_name": "Gatti"
            },
            "assistant": {
                "first_name": "Very",
                "last_name": "Helpful"
            }
        }
    }
}

…with rules structured this way:

{
    company_name: {
        type: 'string'
    },
    address: {
        street: {
            type: 'string'
        },
        state: {
            includes: ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']
        },
        code: {
            type: 'number'
        },
        country: {
            includes: ['United States', 'Canada']
        },
        contact: {
            email: {
                type: 'string'
            },
            phone_nr: {
                type: 'string'
            }
        }
    },
    employs: {
        ceo: {
            individual: {
                first_name: {
                    type: 'string'
                },
                last_name: {
                    type: 'string'
                }
            },
            assistant: {
                first_name: {
                    type: 'string'
                },
                last_name: {
                    type: 'string'
                }
            }
        }
    }
}

We also wanted to make the source code as simple to understand as possible. If you check the index.js file, you'll see what we mean. Use it, modify it, learn from it.

How to Install

] npm install @0x4447/pretzel

How to Test

] npm run test

How to Require

let pretzel = require('pretzel');

How to Use

let pretzel = require('pretzel');

//
//    RULES
//
let rules = {
    name: {
        type: "string"
    }
};

//
//    Good DATA
//
let good_data = {
    name: '0x4447'
};

//
//    Bad DATA
//
let bad_data = {
    name: 123321
};

//
//    1.  Execute validation.
//
let good = pretzel(good_data, rules);
let bad = pretzel(bad_data, rules);

//
//    2.  Check the validation result.
//
console.info(good);
console.info(bad);

Rules

Below is a list of all the rules the module supports at this time.

Bigger than

{
    biggerThan: 5
}

Smaller than

{
    smallerThan: 5
}

Comparison

{
    comparison: 1
}

Includes

{
    includes: [9, '0x4447']
}

Regular Expression

{
    regexp: '[aAzZ.-]'
}

Types

{
    type: "string || number || object || array"
}

The End

If you enjoyed this project, please consider giving it a ????. And check out our 0x4447 GitHub account, where we have additional resources that you might find useful or interesting.

This project is brought to you by 0x4447 LLC, a software company specializing in build custom solutions on top of AWS. Find out more by following this link: https://0x4447.com or, say hello@0x4447.email.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文