Joi: How to make keys case insensitive

Created on 13 Jan 2017  路  8Comments  路  Source: sideway/joi

Context

  • node version: 4
  • joi version:10.1.0
  • environment (node, browser):node
  • used with (hapi, standalone, ...):standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

Describe your issue here, include schemas and inputs you are validating if needed.

var model  = Joi.object().keys({
    "Audi": Joi.string().required()
});

Joi.validate({"AUDI":"somevalue"},model) should pass but it fails

Which result you had ?

What did you expect ?

feature

Most helpful comment

It would be great if there was an option in ValidationOptions: e.g. keysAreCaseInsensitive = true|false

All 8 comments

It's currently only possible through the use of pattern. I'd argue it's a very uncommon construct and I'd rather wait to see other people with the same need to act on it.

I need this too due to clients sending different cases for header keys: X-Vendor-Ext, X-vendor-ext, x-vendor-ext

Node.js is lower-casing all the headers, I don't think this is a valid use case.

Even then, utilizing a case-insensitive Regex pattern would work or utilizing the string().lowercase() method.

I've definitely found the current interface for dynamic key values to be lacking in the ease-of-use area, but in terms of customization you can do many things with the current implementation. That said, I've not figured out a great way to handle this which could fit within how JS objects work.

The best case that I can think of currently is that having a sibling method to object().pattern() which takes a Joi schema for a String type as the first parameter and tries to match the key there. That said, this would be expensive performance wise (compared to a RegEx) and wouldn't offer anything really different except a an API to construct the key instead of just utilizing RegEx.

It would be great if there was an option in ValidationOptions: e.g. keysAreCaseInsensitive = true|false

Be my guest, PRs are open :)

Low interest and existing workaround. Not worth another config option. If others feel strongly about having this option, we can reconsider.

@hueniverse could you point to the existing workaround?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

n-sviridenko picture n-sviridenko  路  3Comments

Taxi4you picture Taxi4you  路  3Comments

alekbarszczewski picture alekbarszczewski  路  3Comments

PaunPrashant picture PaunPrashant  路  3Comments

kailashyogeshwar85 picture kailashyogeshwar85  路  4Comments