hapipal / schwifty

A model layer for hapi integrating Objection ORM
MIT License
73 stars 10 forks source link

Compatibility with koajs #57

Closed BlueAccords closed 6 years ago

BlueAccords commented 6 years ago

Just wondering but is this compatible with koa.js as well https://github.com/koajs?
If so, does it require any specific changes or can I just extend like in the given example for my objection.js models?

devinivy commented 6 years ago

Yes, the Model absolutely is compatible with koa! It doesn't do anything hapi-specific: it just provides joi validation on top of objection models and sets jsonAttributes based upon your joi schema 👍

BlueAccords commented 6 years ago

alright thanks! So I just have to setup my joi schema inside of static get joiSchema() { for each model for the validation I want and I should be good to go?

devinivy commented 6 years ago

Yep, the model class just needs tableName and joiSchema static properties, so this works equally well,

const User = module.exports = class User extends Schwifty.Model {};

User.tableName = 'Users';

User.joiSchema = Joi.object({
    id: Joi.number(),
    name: Joi.string()
});
BlueAccords commented 6 years ago

Mmmk, Also in the joi docs the syntax uses

const schema = Joi.object().keys({
  ...
})

Is there a difference between Joi.object().keys({ ... }) vs Joi.object({ ... })?

devinivy commented 6 years ago

There's a good section about this in the joi docs (https://github.com/hapijs/joi/blob/master/API.md#objectkeysschema). Some decent places to ask questions about joi usage are hapijs/discuss, the hapi slack, or hapijs/hapi on gitter.

BlueAccords commented 6 years ago

Alright i'll look through it, thanks for the help!