获取 Joi 模式中所有可选的字段
Get all fields in the Joi schema that are optjonal
如何获取在 Joi 对象中设置为可选的字段名称数组或列表?
例如:
export const schema = Joi.object<SomeType>({
field1: Joi.string().required(),
field2: Joi.string().guid(),
field3: Joi.string().allow('').optional(),
field4: Joi.string().allow('').optional(),
});
我应该能够检索到 field3、field4。
您可以使用 describe
这样做:
Returns an object that represents the internal configuration of the
schema. Useful for debugging and exposing a schema's configuration to
other systems, like valid values in a user interface.
我们开始吧:
for (const [key, value] of Object.entries(queueSchemaBase.describe().keys)) {
if (value.flags?.presence === 'optional') {
console.log(`${key} is optional`);
}
}
输出:
field3 is optional
field4 is optional
如何获取在 Joi 对象中设置为可选的字段名称数组或列表?
例如:
export const schema = Joi.object<SomeType>({
field1: Joi.string().required(),
field2: Joi.string().guid(),
field3: Joi.string().allow('').optional(),
field4: Joi.string().allow('').optional(),
});
我应该能够检索到 field3、field4。
您可以使用 describe
这样做:
Returns an object that represents the internal configuration of the schema. Useful for debugging and exposing a schema's configuration to other systems, like valid values in a user interface.
我们开始吧:
for (const [key, value] of Object.entries(queueSchemaBase.describe().keys)) {
if (value.flags?.presence === 'optional') {
console.log(`${key} is optional`);
}
}
输出:
field3 is optional
field4 is optional