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

Support for ES6 Types #938

Closed
esatterwhite opened this issue Jul 1, 2016 · 18 comments
Closed

Support for ES6 Types #938

esatterwhite opened this issue Jul 1, 2016 · 18 comments
Assignees
Labels
feature New functionality or improvement

Comments

@esatterwhite
Copy link

There is no way to validate against Map, WeakMap, Set, WeakSet or Symbol. Right now the only work around I see is to define them as joi.any() in a schema.

@hueniverse
Copy link
Contributor

Joi is not really a JS types validator. It is for input validation...

@esatterwhite
Copy link
Author

esatterwhite commented Jul 2, 2016

Yes, I understand that. Maybe I worded the issue poorly. But the problem remains that there is no way to validate input that is of or objects that contain those types.

{
    a: 1,
    b: Symbol('b'),
    c: new Set([1,2,3])
}

The only way to deal with this would be to not validate some of the keys / use object().unknown(), or to use joi.any() for b and c and do some manual validation of them.

@bricss
Copy link
Contributor

bricss commented Jul 7, 2016

@esatterwhite did you ever seen the plain JSON with that kind of types Map, WeakMap, Set, WeakSet or Symbol?

@Marsup Marsup added the request label Jul 8, 2016
@esatterwhite
Copy link
Author

esatterwhite commented Jul 8, 2016

@bricss if by plain, you are mean what JSON.parse and stringify support, then no. But that is not the problem. The problem is that it is not possible to validate objects of or containing those types with joi

@AdriVanHoudt
Copy link
Contributor

to come back to what @hueniverse said the description of Joi does state ... and validator for JavaScript objects

@esatterwhite
Copy link
Author

Not if the objects contain sets, maps, or symbols.

@esatterwhite
Copy link
Author

Plainly - ES6 introduces new primitives that joi should understand just as well as arrays, objects or strings

@Marsup
Copy link
Collaborator

Marsup commented Jul 8, 2016

I agree that it's core-ish, I don't want to ship it with normal joi builds, but there should be an extension for it, not necessarily an official one, but if it gets adoption it would make sense to have it in hapi org.

@AdriVanHoudt
Copy link
Contributor

My comment was pro integrating the new types btw ;)
@Marsup why not ship it with normal builds?

@Marsup
Copy link
Collaborator

Marsup commented Jul 8, 2016

Because it would make Joi fatter than it already is, and it's usually not needed, most people validate POJOs.

@Marsup
Copy link
Collaborator

Marsup commented Jul 8, 2016

Another way would be to have require('joi') and require('joi/es2015').

@Clement-TS
Copy link

Clement-TS commented Jul 12, 2016

And what about promises? Actually we have to set a callback, and joi is not thenable right?
I also feel joi should include new JS features.

@Marsup
Copy link
Collaborator

Marsup commented Jul 12, 2016

This specific detail isn't related to this issue and has already been answered in #636 (comment). I don't think there's anything to check in promises either.

@Marsup
Copy link
Collaborator

Marsup commented Jul 12, 2016

Is anyone interested in implementing the last strategy ?

@AdriVanHoudt
Copy link
Contributor

can't we just do this with .extend() now?

@hueniverse
Copy link
Contributor

Closing due to lack of interest.

@hueniverse hueniverse self-assigned this Jun 16, 2019
@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@kirillgroshkov
Copy link

Would be nice to have it:)

@candu
Copy link

candu commented Nov 25, 2019

Agree that it's useful, and also that it's probably best handled with an extension. I'll provide a sample use case as well, in case that helps motivate this - the TL;DR here is that, as mentioned elsewhere, Joi is used for validating both input and output.

I'm building a REST API with Hapi. To limit N + 1 query problems, several endpoints perform multi-lookups by some ID / field. One natural way to return those results is as a Map: that way, I can guarantee that I'm not duplicating results.

Of course, ES6 Map doesn't exist in JSON, so I configure routes.json.replacer to apply Array.from here:

replacer(_, value) {
  if (value instanceof Map || value instanceof Set) {
    return Array.from(value);
  }
  return value;
},

I can then either do new Map(response) in the frontend, or even build a REST API client layer that uses the reviver argument to JSON.parse to do that automatically.

Now, this isn't the only solution to my problem. I could instead use Array.from() within the REST API endpoint handler and validate it as an array instead:

response: {
  schema: Joi.array().items(
    Joi.array().ordered(keySchema, valueSchema),
  ),
},

That's OK, but it does mean that I'm weakening the response type guarantee from my REST API.

(Of course, if I really care that my REST API returns a Map and not the result of Array.from(myMap), one might reasonably tell me I'm better off using a statically-typed server-side language.)

A hypothetical equivalent map() validator might look like:

response: {
  schema: Joi.map().entries(keySchema, valueSchema),
},

@lock lock bot locked as resolved and limited conversation to collaborators Jun 24, 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

8 participants