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 Symbol() support #1420

Closed
kanongil opened this issue Feb 6, 2018 · 3 comments
Closed

Add Symbol() support #1420

kanongil opened this issue Feb 6, 2018 · 3 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@kanongil
Copy link
Contributor

kanongil commented Feb 6, 2018

In extension of #938, I'm looking into adding support for the symbol type to Joi to allow something like this:

const enums = {
    first: Symbol('first'),
    second: Symbol('second')
};
const schema = Joi.symbol().map(enums);

schema.validate('first'); // value = Symbol(first)

This will enable eg. an options schema to declare enums using symbols instead of strings.

I could probably create it as an extension, but given that it is a native type (typeof Symbol() === 'symbol'), I'd say that it needs to be a part of the standard package.

How does this sound?

@Marsup Marsup added the request label Aug 3, 2018
@Marsup
Copy link
Collaborator

Marsup commented Aug 3, 2018

It could be part of joi indeed, I'm just puzzled by map. It's a coercion from string to symbol, I think it should use Symbol.for() for that.

@kanongil
Copy link
Contributor Author

kanongil commented Aug 3, 2018

The enums define a mapping from a string to a Symbol. In this case I have named them similarly, but it doesn't have to be that way.

You could probably also add an option for an implicit map that uses Symbol.for(), along with a pattern.

The primary use-case from my POV, is to parse a configuration. Eg. instead of having to do a string comparison to figure the option, I can test against a Symbol.

const schema = Joi.string().valid('option1', 'option2');
const config = Joi.attempt(inputConfig, schema);

if (config === 'option1') {
    
}

vs

const option1 = Symbol();
const option2 = Symbol();

const schema = Joi.symbol().map({ option1, option2 });
const config = Joi.attempt(inputConfig, schema);

if (config === option1) {
    
}

@Marsup
Copy link
Collaborator

Marsup commented Aug 7, 2018

Care to make a PR ? Do you think your map should have a case sensitive option ?

@hueniverse hueniverse added this to the 13.7.0 milestone Nov 11, 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

3 participants