DmitryTsepelev / store_model

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

Validate any Hash #114

Closed andresespinosapc closed 2 years ago

andresespinosapc commented 2 years ago

Is it possible to use a StoreModel to validate any Hash or json, without it being in a model?

My use case is that I have an API that returns a list of shipment methods, the user selects one of them and then a request is made to create a record with that shipment method as a jsonb column. I want to validate that the structure is right in the API that returns the shipment methods in the first place.

DmitryTsepelev commented 2 years ago

Hi @andresespinosapc! store_model uses built–in ActiveRecord validation mechanisms, which happen before saving record to the database. Since you don't want to save your JSON and only validate it you might want to consider a different approach: you can describe the schema you expect and check if a given JSON is valid using https://github.com/voxpupuli/json-schema