Hapi/Joi Validation: How to process text to a specific format

You can try the following:

const schema = Joi.object({
    isCapitalCity: Joi.boolean().default(true),
    cityName: Joi.string().valid('MyCity', 'YourCity').insensitive().required()
})

And while validating with schema, you can use the convert:true option as:

schema.validate({"cityName": "myCity"}, {"convert": true})

Alternatively, you can directly provide additional preferences to convert the Joi validated object in the schema:

const schema = Joi.object({
    isCapitalCity: Joi.boolean().default(true),
    cityName: Joi.string().valid('MyCity', 'YourCity').insensitive().prefs({convert:true}).required()
})

Here is a link to working example: https://repl.it/repls/HarmfulEvenPhases

Leave a Comment