天道酬勤,学无止境

joi

Joi 嵌套方案(Joi Nested schema)

问题 我正在尝试在 joi 中创建嵌套模式并且它抛出错误 [错误:对象架构不能是 joi 架构] var nestedSchema = joi.object({ b: joi.number() }); var base = joi.object({ a: joi.string(), nestedData:joi.object(nestedSchema) }); 我应该如何在 joi 中定义嵌套模式? 回答1 尽管 Francesco 的回答有效,但没有必要使用object.keys() 。 问题创建者所做的错误是将模式作为参数传递给joi.object() 。 因此,创建嵌套模式就像将模式分配给属于另一个模式的键一样简单。 const schemaA = Joi.string() const schemaB = Joi.object({ keyB1: schemaA, keyB2: Joi.number() }) const schemaC = Joi.object({ keyC1: Joi.string(), keyC2: schemaB }) Joi.validate({ keyC1: 'joi', keyC2: { keyB1: 'rocks!', keyB2: 3 } }, schemaC) 回答2 你可以使用 object.keys API var

2022-01-15 00:26:13    分类:技术分享    javascript   node.js   joi

Joi Nested schema

I am trying to create nested schema in joi and it is throwing error [Error: Object schema cannot be a joi schema] var nestedSchema = joi.object({ b: joi.number() }); var base = joi.object({ a: joi.string(), nestedData:joi.object(nestedSchema) }); How should i define nested schema in joi?

2022-01-14 23:36:37    分类:问答    javascript   node.js   joi

如何在 Joi 中添加自定义验证器功能?(How to add custom validator function in Joi?)

问题 我有 Joi 模式,想添加一个自定义验证器来验证默认 Joi 验证器无法实现的数据。 目前,我使用的是 Joi 16.1.7 版本 const method = (value, helpers) => { // for example if the username value is (something) then it will throw an error with flowing message but it throws an error inside (value) object without error message. It should throw error inside the (error) object with a proper error message if (value === "something") { return new Error("something is not allowed as username"); } // Return the value unchanged return value; }; const createProfileSchema = Joi.object().keys({ username: Joi.string() .required() .trim() .empty() .min(5) .max

2021-12-23 17:42:06    分类:技术分享    javascript   node.js   validation   express   joi

如何在 Joi 字符串验证中使用枚举值(How to use enum values with Joi String validation)

问题 我正在为我的 HTTP 请求使用 Joi 验证器。 在那里我有一个名为type的参数。 我需要确保参数的可能值是“ios”或“android”。 我怎样才能做到这一点? body : { device_key : joi.string().required(), type : joi.string().required() } 回答1 您可以使用valid . const schema = Joi.object().keys({ type: Joi.string().valid('ios', 'android'), }); const myObj = { type: 'none' }; const result = Joi.validate(myObj, schema); console.log(result); 这给出了一个错误ValidationError: child "type" fails because ["type" must be one of [ios, android]] 回答2 也许对于想要根据现有枚举/值数组检查值的任何人来说,它都会很有用。 const SomeEnumType = { TypeA: 'A', TypeB: 'B' }; 然后只需使用这个: const schema = Joi.object().keys({ type: Joi

2021-12-23 04:24:47    分类:技术分享    node.js   validation   express   joi

How to add custom validator function in Joi?

I have Joi schema and want to add a custom validator for validating data which isn't possible with default Joi validators. Currently, I'm using the version 16.1.7 of Joi const method = (value, helpers) => { // for example if the username value is (something) then it will throw an error with flowing message but it throws an error inside (value) object without error message. It should throw error inside the (error) object with a proper error message if (value === "something") { return new Error("something is not allowed as username"); } // Return the value unchanged return value; }; const

2021-12-12 11:22:56    分类:问答    javascript   node.js   validation   express   joi

如何使用Joi验证其键应与另一个值为数组的键匹配的嵌套对象?(How to validate nested object whose keys should match with outer objects another key whose value is array using Joi?)

问题 我有要验证的对象。 // valid object because all values of keys are present in details object var object = { details: { key1: 'stringValue1', key2: 'stringValue2', key3: 'stringValue3' }, keys: ['key1', 'key2', 'key3'] } // invalid object as key5 is not present in details var object = { details: { key4: 'stringValue4' }, keys: ['key4', 'key5'] } // invalid object as key5 is not present and key8 should not exist in details var object = { details: { key4: 'stringValue4', key8: 'stringValue8', }, keys: ['key4', 'key5'] } 键中存在的所有keys应该details存在。 我用 Joi.ref() 试过这个 var schema = Joi.object({ details: Joi

2021-12-05 07:27:01    分类:技术分享    javascript   json   validation   object   joi

hapi route joi 验证密码确认(hapi route joi validation of password confirmation)

问题 如何检查 password 和 password_confirmation 是否相同? var Joi = require('joi'), S = Joi.string().required().min(3).max(15); exports.create = { payload: { username: S, email: Joi.string().email(), password: S, password_confirmation: S } } 回答1 您可以将 Joi.any().valid() 与 Joi.ref() 一起使用: password: Joi.string().min(3).max(15).required(), password_confirmation: Joi.any().valid(Joi.ref('password')).required().options({ language: { any: { allowOnly: 'must match password' } } }) 回答2 如果你得到"language" is not allowed错误信息。 哦,你来对地方了。 现在,2020 年和 Joi v17.2.1,我们可以使用Joi.any().equal()或Joi.any().valid()与Joi.ref(

2021-11-29 17:36:19    分类:技术分享    node.js   hapijs   joi

How to use enum values with Joi String validation

I am using Joi validator for my HTTP requests. There I have a parameter called type. I need to make sure that the possible values for the parameter are either "ios" or "android". How can I do that? body : { device_key : joi.string().required(), type : joi.string().required() }

2021-11-29 03:27:23    分类:问答    node.js   validation   express   joi

hapi route joi validation of password confirmation

How do I check that password and password_confirmation are the same ? var Joi = require('joi'), S = Joi.string().required().min(3).max(15); exports.create = { payload: { username: S, email: Joi.string().email(), password: S, password_confirmation: S } }

2021-11-22 00:41:19    分类:问答    node.js   hapijs   joi

How to validate nested object whose keys should match with outer objects another key whose value is array using Joi?

I have object which I want to validate. // valid object because all values of keys are present in details object var object = { details: { key1: 'stringValue1', key2: 'stringValue2', key3: 'stringValue3' }, keys: ['key1', 'key2', 'key3'] } // invalid object as key5 is not present in details var object = { details: { key4: 'stringValue4' }, keys: ['key4', 'key5'] } // invalid object as key5 is not present and key8 should not exist in details var object = { details: { key4: 'stringValue4', key8: 'stringValue8', }, keys: ['key4', 'key5'] } All the keys present in keys should be present in details

2021-11-21 02:51:08    分类:问答    javascript   json   validation   object   joi