admin管理员组

文章数量:1399297

I have a simple object with few fields that I would like to validate. I wish to allow or a specific validation schema or that all of the properties have empty values

I created the following two schemas:

 const nonEmptyUserInfoValidationSchema = Joi.object({
    url: Joi.string().uri().required(),
    username: Joi.string().required().min(usernameMinLength),
    password: Joi.string().required().min(passwordMinLength),
});

const emptyUserInfoValidationSchema = Joi.object({
    url: Joi.string().empty().required(),
    username: Joi.string().empty().required(),
    password: Joi.string().empty().required(),
});

I wish to create schema that validates if emptyUserInfoValidationSchema or nonEmptyUserInfoValidationSchema is applied but I can't figure out how to do it, any suggestions?

allowed:

{url:";, username:"some username", password:"some password"}
{url:"", username:"", password:""}

not allowed:

{url:";,  username:"", password:""}

I have a simple object with few fields that I would like to validate. I wish to allow or a specific validation schema or that all of the properties have empty values

I created the following two schemas:

 const nonEmptyUserInfoValidationSchema = Joi.object({
    url: Joi.string().uri().required(),
    username: Joi.string().required().min(usernameMinLength),
    password: Joi.string().required().min(passwordMinLength),
});

const emptyUserInfoValidationSchema = Joi.object({
    url: Joi.string().empty().required(),
    username: Joi.string().empty().required(),
    password: Joi.string().empty().required(),
});

I wish to create schema that validates if emptyUserInfoValidationSchema or nonEmptyUserInfoValidationSchema is applied but I can't figure out how to do it, any suggestions?

allowed:

{url:"http://some.url", username:"some username", password:"some password"}
{url:"", username:"", password:""}

not allowed:

{url:"http://some.url",  username:"", password:""}
Share Improve this question edited Jan 4, 2022 at 19:17 jsejcksn 34k5 gold badges49 silver badges75 bronze badges asked Jan 4, 2022 at 18:55 Or YaacovOr Yaacov 3,8905 gold badges28 silver badges55 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Well I finally found what I was looking for joi.alternatives:

export const ansibleInfoValidationSchema = Joi.alternatives(emptyUserInfoValidationSchema , nonEmptyUserInfoValidationSchema );

seems to do the trick

I hope this helps you can use the when condition in Joi

var schema = Joi.object({
  url : Joi.any().when('username',{
    is: Joi.empty(),
    then: Joi.empty().required(),
    otherwise: Joi.string().required()
  }),
  username: Joi.when('password',{
    is: Joi.empty(),
    then: Joi.empty(),
    otherwise: Joi.string().required().min(usernameMinLength)
  })
});

here's a link to when docs Joi When Condtion

You use the .keys().and() to specify peers. This means that none of the fields specified in the .and() will exist without each other. Hence should be used as so:

const schema = Joi.object({
  url: Joi.string().uri(),
  username: Joi.string().min(usernameMinLength),
  password: Joi.string().min(passwordMinLength)
}).keys().and('url', 'username', 'password');

本文标签: javascriptJoi validate one of two schemasStack Overflow