DmitryTsepelev / store_model

Work with JSON-backed attributes as ActiveRecord-ish models
MIT License
1.04k stars 85 forks source link

Allow an option to disallow "UnknownAttributes" behavior #136

Open armahillo opened 1 year ago

armahillo commented 1 year ago

Normally, the "UnknownAttributes" behavior is great and I find it very useful.

Sometimes, though, I want it to be a bit noisier when a field gets passed along to UnknownAttributes.

Could we get something like:

  attribute :color, :string, disallow_unknown_attributes:true

or

  attribute :color, :string, allow_unknown_attributes:false

If the former, it could be done as a validator (invalid the model if the field is a StoreModel descendant and unknown_attributes isn't empty; if it's the latter, then don't rescue "UnknownAttributesError".

DmitryTsepelev commented 1 year ago

Hey @armahillo! I'd be happy to review such PR 🙂Remember that attribute is a Rails method, so we likely gonna have a different API, e.g.

include StoreModel::Model
attribute :color, :string

disallow_unknown_attributes true

or even

include StoreModel::Model[disallow_unknown_attributes: true]

attribute :color, :string
MatheusRich commented 1 month ago

This PR could help.