Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a key validator with Joi objects #1382

Closed
pierreis opened this issue Dec 18, 2017 · 4 comments
Closed

Add a key validator with Joi objects #1382

pierreis opened this issue Dec 18, 2017 · 4 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@pierreis
Copy link

pierreis commented Dec 18, 2017

Currently, Joi only allows validating the unknown keys of an object using a regexp. While a good solution, it does adapt to more complex use-cases that would be better served by a Joi object.

For example, for email or uuid-like keys, I need to put a regex. This is sad as native validation already exists in Joi for these use-cases.

I would propose accepting as first argument a Joi string validator, while keeping the current behavior for compatibility.


The result would be: Joi.object().pattern(Joi.string().email(), Joi.any())

@Marsup Marsup added the request label Dec 20, 2017
@Marsup
Copy link
Collaborator

Marsup commented Dec 20, 2017

I've been thinking about doing this for a long while now. While less efficient than a regexp, it would be more legible.

Although I would say it doesn't necessarily need to be a string, like in many other cases in joi, anything that validates without error could be considered a match.

Care to do a PR ?

@pierreis
Copy link
Author

I completely agree with that. I’ll check for a PR!

@DavidTPate
Copy link
Contributor

Although I would say it doesn't necessarily need to be a string

Yeah, within an object you could have a key that is a number for example, there's also the question of things like Map and Set with how the key could be something like an object or array instead of just a primitive.

@Marsup
Copy link
Collaborator

Marsup commented Jan 4, 2018

This is already a 1st step, we'll deal with new ES6 types in another time, I'm not even sure it should inherit or be part of object.

@Marsup Marsup added this to the 13.4.0 milestone Aug 3, 2018
@Marsup Marsup self-assigned this Aug 3, 2018
@Marsup Marsup closed this as completed Aug 3, 2018
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants