[英]How to extend a Joi schema?
I have a joi schema called user我有一个名为 user 的 joi 架构
const user = {
firstName: Joi.string()
.min(2)
.max(50)
.required()
.label('First Name'),
lastName: Joi.string()
.min(3)
.max(50)
.required()
.label('Last Name'),
email: Joi.string()
.allow('')
.email({ minDomainAtoms: 2 })
.max(100)
.label('Email Address'),
}
I had another one called owner我还有一个叫owner
const ownerSchema = {
firstName: Joi.string()
.min(2)
.max(50)
.required()
.label('First Name'),
lastName: Joi.string()
.min(3)
.max(50)
.required()
.label('Last Name'),
email: Joi.string()
.allow('')
.email({ minDomainAtoms: 2 })
.max(100)
.label('Email Address'),
number: Joi.string()
.regex(/[0-9]/)
.length(10)
.required()
.label('Phone Number'),
dateOfBirth: Joi.date(),
kycDetails: Joi.array()
.items(schemaKyc)
.required(),
bankDetails: Joi.array()
.items(schemaBank)
.required(),
licenceDetails: Joi.array()
.items(schemaLicence)
.required(),
insuranceDetails: Joi.array()
.items(schemaInsurance)
.required()
};
As you can see the both have three fields in common I want to be able to use the user schema in owner and whenever I make changes to the user I want it to reflect in the owner as well.正如您所看到的,两者都有三个共同的字段,我希望能够在所有者中使用用户架构,并且每当我对用户进行更改时,我希望它也反映在所有者中。
You can use object.keys([schema])
, which您可以使用
object.keys([schema])
,其中
Sets or extends the allowed object keys where:
设置或扩展允许的对象键,其中:
schema
- optional object where each key is assigned a joi type object.schema
- 可选对象,其中每个键都分配了一个joi类型对象。 Ifschema
is{}
no keys allowed.如果
schema
为{}
不允许使用任何键。 Ifschema
isnull
orundefined
, any key allowed.如果
schema
为null
或undefined
,则允许使用任何键。 Ifschema
is an object with keys, the keys are added to any previously defined keys (but narrows the selection if all keys previously allowed).如果
schema
是一个带有键的对象,键将添加到任何先前定义的键中(但如果先前允许所有键,则会缩小选择范围)。 Defaults to 'undefined' which allows any child key.默认为“未定义”,允许任何子键。
Example:例子:
const base = Joi.object().keys({
a: Joi.number(),
b: Joi.string()
});
// Validate keys a, b and c.
const extended = base.keys({
c: Joi.boolean()
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.