joi icon indicating copy to clipboard operation
joi copied to clipboard

Object pattern ignore key uppercase and lowercase

Open creativearmenia opened this issue 7 years ago • 2 comments

Context

  • node version: 11.10.1
  • joi version: 14.3.1
  • environment (node):
  • used with ( standalone, ...):

I am trying to have object with unknown keys in uppercase with number values

const schema = joi.object().keys({
        name: joi.string().uppercase(),
        nested_keys: joi.object().pattern(joi.string().uppercase(), joi.number())
    });

I am passing this object

{
    name: "test",
    nested_keys:{
       "USD" : 2,
       "eur": 3
   }
}

Which result you had ?

{
    name: "TEST",
    nested_keys:{
       "USD" : 2,
       "eur": 3
   }
}

What did you expect ?

{
    name: "TEST",
    nested_keys:{
       "USD" : 2,
       "EUR": 3
   }
}

As joi documentation note it should be the string will be forced to uppercase it works for 'name' field but not for nested keys

creativearmenia avatar Mar 06 '19 14:03 creativearmenia

So the difference here is that the name field is able to do the value conversion with the schema during validation, but object().pattern() does not modify the keys. The pattern (regex or schema) provided to .pattern() simply does a check to see if the key matches the expected pattern.

For key modification, I would typically point you toward object().rename(), but that's also not going to work here. Renaming doesn't accept a modification function, only a replacement string.

I think your best bet is going to be a Joi extension, similar to rename(), but using a transform function on the keys to call .toUpperCase() on them.

WesTyler avatar Mar 06 '19 15:03 WesTyler

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.

lock[bot] avatar Jan 09 '20 09:01 lock[bot]