-
Notifications
You must be signed in to change notification settings - Fork 0
Validators
Ez-on-rails delivers validators you can use in your active record models. The following sections describe those validators.
If you use json or jsonb fields in your records, you can validate them against schemas.
This can be useful to of course have have valid data, but can also be useful as developer to keep track about what you save in your json fields.
You can call the validator as follows:
class Article < EzOnRails::ApplicatioRecord
validates :metadata, json_schema: true
end
In this case the validator will look for a schema in the file app/models/json_schemas/article/metadata.json. This can have eg. the following content:
{
"type": "object",
"properties": {
"commentable": {
"type": "boolean"
},
"number_of_views": {
"type": "integer"
},
"parent_id": {
"type": ["integer", "null"]
}
},
"required": ["commentable"]
}
If you want to use another schema you can pass the path to the schema as paremeter to the validator:
class MyRecord < Article < EzOnRails::ApplicatioRecord
validates :metadata, json_schema: { schema: Rails.root.join('app', 'articles_metadata_schema.json') }
end
TIP: If you want to save the json by your views, you also have to permit them in the controller. We recommend to also only permit the allowed values there.