hapipal / schwifty

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

Remove Model.getJoiSchema(), add Model.joiSchemaPatch #98

Closed devinivy closed 3 years ago

devinivy commented 3 years ago

In schwifty v5 and below we currently define a model's schema on joiSchema and then are instructed to access it from getJoiSchema(). The only reason we did it that way was because static class properties didn't exist back in node v8: we had to make them all getters static get joiSchema(). And it just didn't make sense to recreate the joi schema every time Model.joiSchema is accessed through the getter, so getJoiSchema() was implemented to do this caching for us.

Now that we plan to support node v12+ we don't have to optimize for that kind of usage. You will define static joiSchema = Joi.object(/*...*/); and access Model.joiSchema directly. In order to support patch schemas formerly accessed with Model.getJoiSchema(true) we will implement a cached getter Model.joiSchemaPatch derived from Model.joiSchema.

class MyModel extends Schwifty.Model {
    static tableName = 'Dogs';
    static joiSchema = Joi.object(/* ... */);
};

MyModel.joiSchema.validate({/* ... */});
MyModel.joiSchemaPatch.validate({/* ... */});