Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

418
Views
How to make `Joi` fill missing key/value pairs with some defaults?

I didn't succeed in finding it on the web,
but I can't imagine this is not possible through joi.

Considering this very simple schema below...

Joi.object({
 field_1: Joi.string().required(),
 field_2: Joi.string().required(),
})

...and considering this provided object:
(note that field_2 key and value are missing)

{field_1: "a string"}

I'd like joi to generate this complete object:

{field_1: "a string",
field_2: "a default value"}

How to do so?
Thanks

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

You can define your own validation function for a key.
If value is empty return "any string".
For example:

const method = (value) => {
    if (value === '')
        return "default string"
    return value
}

Joi.object({
    field_1: Joi.string().custom(method, 'custom validation'),
    field_2: Joi.string().required(),
})

For more information: https://joi.dev/api/?v=17.6.0

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!