I am using a schema validator to validate and clean up a JSON schema before it is set. It performs the validation and spits out a clean version (if valid) all in one step, so it would be redundant to use it in both the validate and set() properties. There's no need to do it twice.
Does throwing an error from set() guarantee that the model will not be saved to the database?
Here's the column from the model for example:
metadata: {
type: DataTypes.JSONB,
set(obj) {
const { error, value } = schema.validate(obj)
if (error) throw error
this.setDataValue('metadata', value)
},
},