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

Joi - Ignore inputs if both blank otherwise require both #2866

Closed
Ganjena opened this issue Oct 19, 2022 · 1 comment
Closed

Joi - Ignore inputs if both blank otherwise require both #2866

Ganjena opened this issue Oct 19, 2022 · 1 comment
Assignees
Labels
support Questions, discussions, and general support

Comments

@Ganjena
Copy link

Ganjena commented Oct 19, 2022

Support plan

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

Context

  • node version: 1.72.2
  • module version:
  • environment (e.g. node, browser, native): vsc
  • used with (e.g. hapi application, another framework, standalone, ...): node-express
  • any other relevant information:

How can we help?

I would like to have two user inputs. I would like both to be optional. BUT if EITHER field is used they then both need to be required.

Example:

Should the user not want to input thier name, firstName and lastName it can be null or "" and still pass validation. But if the user inputs EITHER first or last name then BOTH will be required.

My Schema atm:

    const customerSchema = Joi.object({
        name: Joi.object({
            firstName: Joi.string(),
            lastName: Joi.string()
        }).and("firstName", "lastName")
})

With the current Schema the user does need to completed both fields to pass validation BUT they are unable to leave both blank should they choose to.

    const customerSchema = Joi.object({
        name: Joi.object({
            firstName: Joi.string().allow(null, ``),
            lastName: Joi.string().allow(null, ``)
        }).and("firstName", "lastName")
})

With this schema both can now be null or empty but the .and() condition doesnt apply.

The last thing i tried was variation's of .when() but keep coming up with dependancy errors...

    const customerSchema = Joi.object({
        name: Joi.object({
            firstName: Joi.string().when(`lastName`, {is: Joi.exsist(), then: Joi.required(), otherwise: Joi.allow(null,``)}),
            lastName: Joi.string().when(`firstName`, {is: Joi.exsist(), then: Joi.required(), otherwise: Joi.allow(null,``)})
        }).and("firstName", "lastName")
})

Is this possible?

@Marsup
Copy link
Collaborator

Marsup commented Oct 19, 2022

For now it's not, I need to review #2762, watch that PR for activity.

@Marsup Marsup closed this as completed Oct 19, 2022
@Marsup Marsup self-assigned this Oct 19, 2022
@Marsup Marsup added feature New functionality or improvement support Questions, discussions, and general support and removed feature New functionality or improvement labels Oct 19, 2022
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

2 participants