Using Joi, require one of two fields to be non empty

Code below worked for me. I used alternatives because .or is really testing for the existence of keys and what you really wanted was an alternative where you would allow one key or the other to be empty. var console = require(“consoleit”); var Joi = require(‘joi’); var schema = Joi.alternatives().try( Joi.object().keys({ a: Joi.string().allow(”), b: Joi.string() … Read more

Joi validator conditional schema

I achieved the same in a little different manner. Posting the same here since this might be useful for someone in future. const schema = Joi.object({ type: Joi.number().required().valid(1, 2, 3), firstname: Joi.alternatives().conditional(‘type’, { is: 1, then: Joi.string().required() }), lastname: Joi.alternatives().conditional(‘type’, { is: 1, then: Joi.string().required() }), salary: Joi.alternatives().conditional(‘type’, { is: 2, then: Joi.number().required() }), pension: … Read more

Joi object validation: How to validate values with unknown key names?

Try this. It’ll basically accept any key within an object campaign and the value must validate against Joi.date().iso() campaign: Joi.object().pattern(/^/, Joi.date().iso()) This however will match any key. You can restrict this by padding out the regex a little. e.g. only word characters between 2 and 25 chars campaign: Joi.object().pattern(/\w{2,25}/, Joi.date().iso()) UPDATE Regarding the example in … Read more

Nodejs – Joi Check if string is in a given list

You are looking for the valid and invalid functions. v16: https://hapi.dev/module/joi/api/?v=16.1.8#anyvalidvalues—aliases-equal v17: https://hapi.dev/module/joi/api/?v=17.1.1#anyvalidvalues—aliases-equal As of Joi v16 valid and invalid no longer accepts arrays, they take a variable number of arguments. Your code becomes var schema = Joi.object().keys({ firstname: Joi.string().valid(…[‘a’,’b’]), lastname: Joi.string().invalid(…[‘c’,’d’]), }); Can also just pass in as .valid(‘a’, ‘b’) if not getting the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)