joi

JavaScript对象的对象模式描述语言和验证器。(Object schema description language and validator for JavaScript objects.)

Github星跟蹤圖

JavaScript对象的对象模式描述语言和验证器。

主要维护者: Nicolas Morel

简介

假设你运行Facebook,并且你希望访问者在真实姓名的网站上注册,而不是在名字字段中输入 l337_p@nda 。您如何定义可输入内容的限制并根据设定的规则验证它?

这是joi,joi允许您为JavaScript对象(存储信息的对象)创建蓝图模式以确保验证关键信息。

API

请参阅详细的 API参考资料

示例

 const Joi = require('joi');

const schema = Joi.object().keys({
    username: Joi.string().alphanum().min(3).max(30).required(),
    password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
    access_token: [Joi.string(), Joi.number()],
    birthyear: Joi.number().integer().min(1900).max(2013),
    email: Joi.string().email({ minDomainAtoms: 2 })
}).with('username', 'birthyear').without('password', 'access_token');

// Return result.
const result = Joi.validate({ username: 'abc', birthyear: 1994 }, schema);
// result.error === null -> valid

// You can also pass a callback which will be called synchronously with the validation result.
Joi.validate({ username: 'abc', birthyear: 1994 }, schema, function (err, value) { });  // err === null -> valid

以上模式定义了以下约束:

  • 用户名
    • 必填字符串
    • 只能包含字母数字字符
    • 至少3个字符,但不超过30个
    • 必须附有 birthyear
  • 密码
    • 可选字符串
    • 必须满足自定义正则表达式
    • 不能与 access_token 一起出现
  • access_token
    • 可选的无约束字符串或数字
  • birthyear
    • 1900年至2013年之间的整数
  • 电子邮件
    • 有效的电子邮件地址字符串

用法

用法是一个两步骤的过程。首先,使用提供的类型和约束来构造模式:

const schema = {
    a: Joi.string()
};

请注意, joi 模式对象是不可变的,这意味着添加的每个附加规则(例如 .min(5))将返回一个 新的模式对象。

然后,该值将根据模式进行验证:

const {error, value} = Joi.validate({ a: 'a string' }, schema);

// or

Joi.validate({ a: 'a string' }, schema, function (error, value) { });

如果输入有效,那么错误将是 null ,否则它将是一个Error对象。

模式可以是一个普通的JavaScript对象,其中每个键被分配一个 joi 类型,或者直接可以是一个 joi 类型:

const schema = Joi.string().min(10);

如果模式是 joi 类型,则可以直接在该类型上调用 schema.validate(value,callback)。当传递一个非类型的模式对象时, 该模块将其内部转换为等效的object()类型

const schema = Joi.object().keys({
    a: Joi.string()
});

验证架构时:

  • 默认情况下,值(或对象中的键)是可选的。
    Joi.validate(undefined, Joi.string()); // validates fine    
    要禁止这种行为,您可以将模式设置为 required(),或者将 presence 设置为“required”代码>选项:
    Joi.validate(undefined, Joi.string().required());
    // or
    Joi.validate(undefined, Joi.string(), /* options */ { presence: "required" }); 
      
  • 字符串是由defau编码的utf-8
  • 规则以添加方式定义,并在白名单和黑名单检查后按顺序进行评估。

浏览器

Joi不直接支持浏览器,但您可以使用 joi-browser 获得Joi的ES5版本在浏览器中,或者作为你自己构建的灵感来源。

概覽

名稱與所有者hapijs/joi
主編程語言JavaScript
編程語言JavaScript (語言數: 2)
平台BSD, Cross-platform, Linux, Mac, Solaris, Unix-like, Windows
許可證Other
發布數266
最新版本名稱v17.12.3 (發布於 2024-04-03 11:41:27)
第一版名稱v0.0.4 (發布於 2012-10-01 16:17:06)
創建於2012-09-16 16:38:06
推送於2024-04-19 06:27:29
最后一次提交2024-04-03 11:27:29
星數20.6k
關注者數176
派生數1.5k
提交數2.5k
已啟用問題?
問題數2340
打開的問題數153
拉請求數459
打開的拉請求數11
關閉的拉請求數213
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?

@hapi/joi

The most powerful schema description language and data validator for JavaScript.

joi is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks). If you are using a different web framework and find this module useful, check out hapi – they work even better together.

Visit the hapi.dev Developer Portal for tutorials, documentation, and support

Useful resources

去到頂部