admin管理员组

文章数量:1334361

I have the following Joi schema for a User:

const userRules = Joi.object({
    name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(),
    email: Joi.string().email().required(),
    password: Joi.string().min(8).max(40).required()
});

But for authentication purposes, I only want to validate email and password, ignoring name. Is it possible to do this without having to create a different schema?

I have the following Joi schema for a User:

const userRules = Joi.object({
    name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(),
    email: Joi.string().email().required(),
    password: Joi.string().min(8).max(40).required()
});

But for authentication purposes, I only want to validate email and password, ignoring name. Is it possible to do this without having to create a different schema?

Share Improve this question asked Sep 3, 2020 at 14:35 Allan JuanAllan Juan 2,5743 gold badges26 silver badges54 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

This will ignore name:

const userRules = Joi.object({
    email: Joi.string().email().required(),
    password: Joi.string().min(8).max(40).required()
})
.options({allowUnknown: true});

You can use Joi.fork() to create a derived schema from a base one.

const userRules = Joi.object({
    name: Joi.string().pattern(new RegExp('^[A-Za-zÁÉÍÓÚáéíóúãõÃÕâêôÂÊÔ ]+$')).required(),
    email: Joi.string().email().required(),
    password: Joi.string().min(8).max(40).required()
});
const authSchema = userRules.fork(['name'], (schema) => schema.optional())

Now, in authSchema, the name attribute is optional and can be ignored.

本文标签: javascriptHow to ignore one of the fields from a Joi schemaStack Overflow