neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

Consider using Joi for schema validation #277

Open marshall007 opened 9 years ago

marshall007 commented 9 years ago

I believe the existing schema API is already based heavily on Joi. I think it would make a lot of sense to just move to it completely.

  1. Reduce complexity of thinky.
  2. Joi can be browserify'd, so schemas could be shared between client and server (this would be huge!).
  3. Many applications require validating things other than database models. It would be nice to use a single library for both cases.
tlvenn commented 9 years ago

+1 for this as well.

neumino commented 9 years ago

Yes, the current syntax is based on Joi. I don't remember who showed it to me, but I found the syntax way nicer that what we used to have.

I don't remember exactly all the reasons, but the main one I think was that ReQL values are not checked on save but on retrieval. Typicall r.now() will pass the validation for save.

neumino commented 9 years ago

That being said, I'm not an expert on Joi and may have missed an option to skip validation in special cases (or they may have added it since last time?).

robertjpayne commented 9 years ago

@neumino does Joi need to skip all validation or just validation on certain fields?

neumino commented 9 years ago

It needs to skip validation sometimes (for ReQL terms like r.now()), or have a special handling for binaries/dates when they are in the ReQL raw format.

grantcarthew commented 9 years ago

+1

marshall007 commented 9 years ago

I think the solution to validating on retrieval would be for thinky to inspect the Joi schema you pass in to create the Model and replace (for example) instances of Joi.date() (i.e. schema._type === 'date') with:

Joi.alternatives().try(
  Joi.date(),
  Joi.object().keys({
    '$reql_type$': 'TIME'
  }).and('epoch_time', 'timezone')
)

I'm still unsure how we'd get Joi to not complain about validating r.now() before save, though.

marshall007 commented 9 years ago

On second thought, it might be as simple as adding another alternative Joi.object().type(r._Term)?

sjmueller commented 9 years ago

+1

cur3n4 commented 8 years ago

+1

no-more commented 8 years ago

That would really be great!

sandeepjain commented 8 years ago

Any chance if this is implemented? Any fork implementing this?

at0g commented 8 years ago

👍 Would love to see this

schettino commented 8 years ago

+1

MarkHerhold commented 8 years ago

@schettino @at0g please stop spamming this thread. Use the reaction buttons above

no-more commented 8 years ago

Does anyone know if there a tool to export thinky schema to joi schema ? I understand thinky have some requirements that joi don't have due to the database (for date), but it would be great to have a tool, even if it does not handle every options, that could export to joi which can then be used for routes validation.

cedbale commented 7 years ago

+1