pebble / joi-objectid

A MongoDB ObjectId validator for Joi
MIT License
43 stars 22 forks source link

Type casting to ObjectID? #2

Open craigspaeth opened 9 years ago

craigspaeth commented 9 years ago

Joi does implicit type casting for things like Date. I find myself wanting this plugin to type cast a 24 char alphanumeric string into a require('mongodb').ObjectID. I'm not sure if it makes sense to include mongodb-native as a dependency of this project to do that, but I thought I would throw the idea out there.

If that sounds like something you're interested in I would be happy to submit a pull request for it.

aheckmann commented 9 years ago

maybe we could just include the bson module?

devinus commented 9 years ago

I would really like this.

olistic commented 9 years ago

I've just created a pull request in joi which would cover exactly what you are asking. The PR wasn't merged because of the added BSON dependency. However, you might find a way to reutilise the code in this plugin.

https://github.com/hapijs/joi/pull/563/commits

aheckmann commented 9 years ago

is it not possible to put that logic in this module? my thinking is that Hapi shouldn't need to care about validating everyone's custom types itself and instead should provide the APIs which we can hook into. separation of concerns

olistic commented 9 years ago

I think that it is not possible as things are right now. I totally agree with you in that an API which we can hook into would be an awesome enhancement to joi.

morfj commented 9 years ago

I didn't start using Joi for its pretty syntax (there are validators with more compact/prettier syntaxes out there) but because it also provided typecasting/parsing of types to create proper DTOs from the input. As it stands, this plugin is rather half-done IMHO. It needs to support type-casting!

aheckmann commented 9 years ago

SGTM. anyone want to open a quick PR with tests?

olistic commented 9 years ago

I'm in. Will start working on this.

jlai commented 9 years ago

FYI, joi is working on a way to officially support plugins/extensibility (actively being discussed): https://github.com/hapijs/joi/issues/577