我正在使用Node Joi
验证.我是节点中的新用户我要验证env
只接受2个单词"是"或"否"我需要在以下代码中进行哪些更改
schema = Joi.object().keys({
app_id: Joi.string().required(),
env: Joi.string().required()
});
Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法来验证数组是否包含使用joi所需的值。
在网上找到这些问题 - #1 , #2,但没有一个有明确的答案。
我尝试了几种东西,但它们似乎不起作用,例如:
joi.array().items(joi.string().allow('required-string').required())
,
joi.array().items(joi.string().label('required-string').required())
,
joi.array().items(joi.string().valid('required-string'))
这就是我想要实现的目标:
公认:
['required-string'], ['required-string', 'other'], ['other','required-string'], ['other',...,'required-string',....,'more-other']
Run Code Online (Sandbox Code Playgroud)
拒绝:
[], ['other'], [null], etc..
Run Code Online (Sandbox Code Playgroud) 我正在使用带有TypeScript的joi和@ types/joi.Joi有一个extend方法,它允许通过返回一个新实例来扩展joi,而无需修改原始的joi库.我用它创建了一个扩展实例.
为了这个扩展实例创建的定义,我试图Module Augmentation
描述这里使用下面的代码:
declare module 'joi' {
// Add a new Schema type which has noChildren() method.
interface CustomSchema extends ObjectSchema {
noChildren(): this;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,正如预期的那样,这会通过扩充来修改原始定义.我想要的是为扩展实例创建定义,它继承原始内容而不修改它.
还扩展Joi
了如下:
import * as Joi from 'joi';
const JoiExtended = Joi.extend({...some implementation...})
// How to export?
// export * from 'Joi' ---> In this case, original non-extended Joi is exported
// export default JoiExtended ---> Imported `Joi` reports: Cannot find …
Run Code Online (Sandbox Code Playgroud) 我正在使用 hapi js 和 couchbase 开发 API。我正在使用 log4js 来记录错误。
{
// Add a user level
method: 'POST',
path: '/api/v1/userlevel',
config: {
handler: (request, reply) => {
const userlevel = new Userlevel(request.payload);
userlevel.save((err) =>{
if(err) {
return reply({
status: 400,
message: err.message
}).code(400);
// logger.error(err);
}
// logger.debug(reply);
return reply(userlevel).code(201);
});
},
validate: {
payload: {
group_id: Joi.string(),
name: Joi.string(),
status: Joi.string(),
index_id: Joi.number(),
device_id: Joi.string(),
created_at: Joi.string(),
updated_at: Joi.string(),
sys_version: Joi.string()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用无效数据向此端点发送 POST 请求时,它显示错误
POST 请求 …
let obj = Joi.object().keys({
"id": Joi.string().required(),
"array": Joi.array().items(obj).required()//array contains multiple
});
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在 JOI 中定义递归数组验证 obj.array 包含 n 个 obj
我有以下 JSON 结构:
{
key1: "value1",
key2: "value2",
transactions: [
{
receiverId: '12341',
senderId: '51634',
someOtherKey: 'value'
},
{
receiverId: '97561',
senderId: '46510',
someOtherKey: 'value'
}
]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试编写一些 Joi 代码来验证事务数组中的每个对象都是唯一的,即receiverId 和senderId 的组合只出现一次。交易数组中可以有可变数量的元素,但总是至少有 1 个。有什么想法吗?
我正在开发一个应该允许多个参数的 api,但对于其中三个我只想允许其中一个。每个键的值更容易,但我想知道 Joi 是否也允许它,或者我应该在我的服务器中添加额外的验证逻辑。
简而言之,对于 keys a
,b
或者c
我想失败具有三个以上之一的任何请求,因此:
http://myapi.com/?a=value
是一个有效的请求。
http://myapi.com/?b=value&c=value2
是无效的。
谢谢!
我遇到了快速验证器使用两个键验证对象的问题。我的方法如下。
check('contact.code')
.trim()
.isNumeric()
.withMessage('Country code must be numeric.')
.bail()
.isLength({min: 1, max: 4})
.withMessage('Invalid country code.')
.bail(),
check('contact.number')
.trim()
.isNumeric()
.withMessage('Phone number must be numeric.')
.bail()
.isLength({max: 10, min: 10})
.withMessage('Phone number must be 10 digits long.')
.bail(),
Run Code Online (Sandbox Code Playgroud)
在req.body中,我将我的联系人发送为,
contact: {"code": "91", "number":"9087654321"}
但我收到错误:
{
"errors": [
{
"value": "",
"msg": "Country code must be numeric.",
"param": "contact.code",
"location": "body"
},
{
"value": "",
"msg": "Phone number must be numeric.",
"param": "contact.number",
"location": "body"
}
]
} …
Run Code Online (Sandbox Code Playgroud) 验证日期字段是否大于 x 天。
现在我有这个片段来检查日期是否大于现在。
planned_date: Joi.date().greater('now').required()
Run Code Online (Sandbox Code Playgroud)
但我想验证一下,现在至少planned_date
比现在多了2 天。这可以与 结合使用,但无法使其工作。moment.js
我的架构是:
const scenerioSchema = Joi.object({
drawingNode: Joi.object({
moduleRackOutputs: Joi.array()
.items(
Joi.object({
moduleId: Joi.string().required()
})
)
.unique((a, b) => a.moduleId !== b.moduleId)
})
})
Run Code Online (Sandbox Code Playgroud)
我的数据是:
const mockScenario1 = {
drawingNode: {
moduleRackOutputs: [
{
moduleId: 'module1'
},
{
moduleId: 'module2'
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
当我验证时:
const validationResponse = scenerioSchema.validate(mockScenario1)
Run Code Online (Sandbox Code Playgroud)
我得到:
{
validationResponse: {
value: { drawingNode: [Object] },
error: [Error [ValidationError]: "drawingNode.moduleRackOutputs[1]" contains a duplicate value] {
_original: [Object],
details: [Array]
}
}
}
Run Code Online (Sandbox Code Playgroud)
moduleId
但 (a) 这不是真的 - 这些项目不是重复的,并且 (b) …