Combine uppercase with valid
Is it possible to use string.uppercase() prior to running through the any.valid() method?
What difference would it make ?
I would like Joi.string().valid('AA','BB') to be able to accept the following values: ['aa','aA', Aa', ...] without having to expand the array to cover all string cases.
So you want Joi.string().insensitive() which already exists then.
So that works for the validation piece, but I lose the functionality of the resulting value being uppercase after validation takes place. Any way to have both?
@hueniverse do you mind helping me understand that https://github.com/hapijs/joi/commit/0b4b45aa6548e2444d9bb976e07653f2af1e4e97 ? Is it purely optimization ?
@pon until I find a solution, a hack that works is :
Joi.string().insensitive().uppercase().concat(Joi.string().valid(['AA', 'BB', ...]))
@Marsup - running the code below still returns the non-uppercased value:
var Joi = require('joi');
var schema = {
key: Joi.string().insensitive().uppercase().concat(Joi.string().valid(['AA', 'BB']))
};
Joi.validate({ key: 'aa' }, schema, function (err, value) {
// value = { key: 'aa' }
});
My bad, thought it was working for a moment.
No problem - I have a non-Joi workaround for now
Best I can think of is this :
var schema = {
key: Joi.string().uppercase().regex(/^AA|BB$/)
};
I came across this as well, and would like to see a solution. Especially for my "lout" documentation I would prefer the combination with valid over regex.
I'm still thinking about how to lessen the performance impact of such modification.
I was also expecting Joi.string().uppercase().valid('A', 'B') to solve this use case.
As far as 0b4b45a is concerned, after reading the documentation I get the impression that using .min(2).valid(2, 3, 4) is really the problem as they contradict each other. So to truly solve this I guess joi would have to distinguish between modification rules (trim(), lowercase() etc) and validation rules. And then the assertion "Cannot define rules when valid values specified" would only be applied to the latter.