DmitryTsepelev / store_model

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

Manageable raising on invalid enum value #164

Closed alexeevit closed 9 months ago

alexeevit commented 9 months ago

Hey, just recently I was working on an API where I had to add a validation on enum attributes. Unfortunately, there were no other validation mechanisms before passing the parameters to the model. Therefore, the store_model instance raised an error every time a wrong value was passed to the API.

I checked the ActiveRecord::Enum behavior and found, that they have a validate: boolean parameter, that turns off the raising an exception and adds a validation for the field to the model. I'm not sure if we should interfere with the model, so here is my implementation of the enum that supports a new parameter raise_on_invalid_values: boolean (default: true) just so we turn off the check.

Not sure if it's a good idea to update the type, or maybe it would be better to create another type that'd inherit from EnumType. Let me know what you think about this.