简体   繁体   English

如何使用 Joi 验证嵌套 object 的键应与外部对象匹配的另一个键的值为数组的键?

[英]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.我有想要验证的 object。

// 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 also.键中存在的所有keys也应details显示。

I tried this using Joi.ref()我尝试使用Joi.ref()

var schema = Joi.object({
    details: Joi.object().keys(Object.assign({}, ...Object.entries({...Joi.ref('keys')}).map(([a,b]) => ({ [b]: Joi.string() })))),
    keys: Joi.array()
})

But this is not working because Joi.ref('keys') will get resolved at validation time.但这不起作用,因为Joi.ref('keys')将在验证时得到解决。

How can I validate this object using Joi ?如何使用Joi验证此 object ?

Using object.pattern and array.length使用object.patternarray.length

var schema = Joi.object({
  details: Joi.object().pattern(Joi.in('keys'), Joi.string()),
  keys: Joi.array().length(Joi.ref('details', {
      adjust: (value) => Object.keys(value).length
    }))
});

stackblitz 堆栈闪电战

You can validate the array(if you want) then make a dynamic schema and validate that.您可以验证数组(如果需要),然后制作动态模式并进行验证。

const arrSchema = Joi.object({
    keys: Joi.array()
});

then,然后,

const newSchema = Joi.object({
    details: Joi.object().keys(data.keys.reduce((p, k) => {
        p[k] = Joi.string().required();
        return p;
    },{})),
    keys: Joi.array()
})

This should probably do it.这应该可以做到。

You have to set allowUnknown: true in validate() option.您必须在validate()选项中设置allowUnknown: true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Joi 验证 object 键的值是否应该出现在另一个 object 中? - How to validate that object key's value should present in another object using Joi? 使用 Joi 验证 object 的两个键是否应该具有相同的值? - Validate that two keys of object should have same value using Joi? JavaScript - 如何遍历对象数组以创建一个新对象,其键是原始对象的初始键/值对的值 - JavaScript — how to iterate through an array of objects to create a new object whose key is the value of the original object's initial key/value pair JavaScript:创建 function,它返回一个 object,其键使用回调与值数组中的元素匹配 - JavaScript: Create function that returns an object whose keys match the elements in the array of values using callbacks 如何访问其键是另一个 JSON 对象的值的 JSON 对象的值? - How to access value of a JSON object whose key is value of another JSON object? 如何使用 Joi 对三个键进行约束来验证 object? - How to validate object with constraint on three keys using Joi? 以不可变的方式更新其值为对象数组的对象 - Update Object whose value is an array of objects in an immutable way 如何使用JOI验证来验证嵌套json对象的某些字段 - How to validate certain fields of a nested json object using JOI validation Joi-根据对象数组中的值验证属性 - Joi - validate property against value in array of objects 如何使用 JOI 验证请求正文中的对象数组 - How to validate an array of objects in a request body using JOI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM