Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field validation #2998

Open
hbindiya opened this issue Nov 2, 2023 · 3 comments
Open

Field validation #2998

hbindiya opened this issue Nov 2, 2023 · 3 comments
Labels
support Questions, discussions, and general support

Comments

@hbindiya
Copy link

hbindiya commented Nov 2, 2023

Support plan

  • is this issue currently blocking your project? (yes/no): yes
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v18.14.2
  • module version: 17.3.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, ...): both

How can we help?

const NewUser = Joi.object()
  .keys({
    email: Joi.string()
      .email()
      .example("johndoe@example.com"),
    name: Joi.string().example("john doe"),
    username: Joi.when("email", {
      is: Joi.exist(),
      then: Joi.string().required(),
      otherwise: Joi.optional()
    }).example("johndoe"),
    password: Joi.when("email", {
      is: Joi.exist(),
      then: Joi.string().required(),
      otherwise: Joi.optional()
    }).example("password"),
    "first-name": Joi.string()
      .allow(null)
      .example("john"),
    "communication-email": Joi.string()
      .allow(null)
      .example("johndoe@example.com"),
    "phone-number": Joi.number()
      .allow(null)
      .example(9898989898),
    "avatar-s3-key": Joi.string()
      .allow(null)
      .example("ace/2018-12/89a3fa9c-0b79-4140-b0ef-d1a35ea0b733/dog4.jpeg"),
    "last-name": Joi.string()
      .allow(null)
      .example("doe"),
    "dont-login": Joi.boolean()
      .allow(null)
      .example(true),
    "avatar-url": Joi.string()
      .uri()
      .allow(null)
      .example(
        "https://thumbor-stg.assettype.com/ace/2018-12/89a3fa9c-0b79-4140-b0ef-d1a35ea0b733/dog4.jpeg"
      ),
    "login-phone-number": Joi.string().example("+919898989898"),
    otp: Joi.string()
      .when("login-phone-number", {
        is: Joi.exist(),
        then: Joi.required(),
        otherwise: Joi.optional()
      })
      .when("email", {
        is: Joi.exist(),
        then: Joi.required(),
        otherwise: Joi.optional()
      })
      .example("12345"),
    metadata: Joi.object()
      .example({ "favorite-fruit": "papaya" })
      .meta({ swagger: { deprecated: true } }),
    active: Joi.boolean().example(true)
  })
  .xor("otp", "username", "password")
  .or("login-phone-number", "email");
For payload:
{
    "email":"povofev476@undewp.com",
    "otp": "913676"
}
Response:
{
    "message": "\"body.username\" is required,\"body.password\" is required"
}

username and password was initially to be mandatory, but when I have otp, username+password can be optional and vice versa. it can also allow all the three fields.

@hbindiya hbindiya added the support Questions, discussions, and general support label Nov 2, 2023
@Marsup
Copy link
Collaborator

Marsup commented Nov 2, 2023

Wouldn't it work with a simple .and('username', 'password').or('username', 'otp')?

@hbindiya
Copy link
Author

hbindiya commented Nov 3, 2023

If email exists user can signup/login via otp or username+password

  • when otp exists in the request body, u+p can be optional
  • when u+p is in request body, otp can be optional
  • email+otp, email+u+p is also allowed

.and('username', 'password').or('username', 'otp') doesn't work. I tried the below code.

const NewUser = Joi.object()
  .keys({
    email: Joi.string()
      .email()
      .example("johndoe@example.com"),
    name: Joi.string().example("john doe"),
    username: Joi.when("email", {
      is: Joi.exist(),
      then: Joi.string().required(),
      otherwise: Joi.optional()
    }).example("johndoe"),
    password: Joi.when("email", {
      is: Joi.exist(),
      then: Joi.string().required(),
      otherwise: Joi.optional()
    }).example("password"),
    "login-phone-number": Joi.string().example("+919898989898"),
    otp: Joi.string()
      .when("login-phone-number", {
        is: Joi.exist(),
        then: Joi.required(),
        otherwise: Joi.optional()
      })
      .when("email", {
        is: Joi.exist(),
        then: Joi.required(),
        otherwise: Joi.optional()
      })
      .example("12345"),
    metadata: Joi.object()
      .example({ "favorite-fruit": "papaya" })
      .meta({ swagger: { deprecated: true } }),
    active: Joi.boolean().example(true)
  })
  .or("login-phone-number", "email")
  .and("username", "password")
  .or("username", "otp");

@Nargonath
Copy link
Member

I believe if you use and/or for the u + p and u + otp you shouldn't use when anymore. Have you tried removing the when?

@hbindiya hbindiya changed the title How to validate a field is OTP or username+password Field validation Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

3 participants