I want to validate a specific string using JOI. The values for the variable can be updated and resides in the Data Base.
My current implementation is as follows:
someVariable: Joi.string().valid(['value1', 'value2']).default("someDefault")
The .valid function takes array of string, but my requirement is to give it a function which checks the corresponding values from the DB. I want some thing like the following:
someVariable: Joi.string().valid(getOptions).default("someDefault")
async function getOptions(){
let options = await someVariableOptions.findAll({});
let allowedValues = options.map( (currOption)=> currOption.values);
return allowedValues;
}
How this can be achieved using JOI?
The valid versions of this string is your database is that? I don't understand that part so well. :sweat_smile:
If is that, you can achieve this behavior with this actual code, only changing the
Joi.string().valid(getOptions).default("someDefault");
for something like this:
const validOptions = await getOptions();
// Considering that the getOptions function is getOptions(): String[] or at least getOptions(): Joi.objet[]
// Somewhere else in the code ....
{
someVariable: Joi.string().valid(validOptions).default("someDefault");
}
Hi @Horaddrim , thanks a lot for your input.
['value1', 'value2'] would reside in the database, and these values, can be added, deleted or updated.
So for-example, ['value1', 'value2'] could later be ['value1', 'value2', 'value3'] or ['valueNew1', 'value2', 'value3'] . Therefore, I need to check the latest values from the DB every time.
Hope, it clarifies the question. :)
The solution, that you have presented, will give me ['value1', 'value2'] & if those values change in the DB, I would have no knowledge about that.
Therefore, need to query DB every time the Validation is required.
Thanks for the feedback! I think now a days there is no support for subscription-like validation, and maybe don't even enter in the Joi scope, maybe @hueniverse or @Marsup can help me, because I really don't know if it's a Joi responsibility to handle a socket between database servers, to update this validation value.
Maybe, and I let my self open to help you if you wanted, we can do a plugin, or WS based Joi validation, because it's an extension and runs out from the scope of the Joi project.
:smile: But we can do something if you want it!
Don't be afraid because it's easier than it looks like! hahaha!
Yeah, it seems like you need an asynchronous validation, which Joi currently does not support.
It is technically on the roadmap, but none of us have had the time to devote to it. :\
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.