Ajv:如何根据其他属性有条件地使属性为空
Posted
技术标签:
【中文标题】Ajv:如何根据其他属性有条件地使属性为空【英文标题】:Ajv: how to make a property nullable conditionally depending on other property 【发布时间】:2022-01-15 01:26:18 【问题描述】:我有一个可以处于 4 种状态之一的对象:
"A": "something", "B": ["C": "D"]
"A": "", "B": ["C": "D"]
"A": "something", "B": null
"A": "", "B": null
在我的 Ajv 架构验证中,我只想将属性 "B"
nullable: true
设为属性 "A"
不是为空字符串。
我怎样才能做到这一点?
【问题讨论】:
【参考方案1】:我希望下面的帮助函数对你有所帮助。您可以根据需要更改分配值
function mappNullable(obj, nonNullable, nullableProperty)
if(obj[nonNullable] != '')
obj[nullableProperty] = nullable: true;
return;
const t1 = "A": "something", "B": ["C": "D"];
const t2 = "A": "", "B": ["C": "D"];
const t3 = "A": "something", "B": null;
const t4 = "A": "", "B": null;
mappNullable(t1, "A", "B");
mappNullable(t2, "A", "B");
mappNullable(t3, "A", "B");
mappNullable(t4, "A", "B");
console.log(t1); // "A": "something", "B": nullable: true;
console.log(t2); // "A": "", "B": ["C": "D"];
console.log(t3); // "A": "something", "B": nullable: true;
console.log(t4); // "A": "", "B": null;
【讨论】:
以上是关于Ajv:如何根据其他属性有条件地使属性为空的主要内容,如果未能解决你的问题,请参考以下文章