Validate alert's `payload` using JSON schema
Problem
The issue #222826 (closed) provides a list of JSONB columns which do NOT have a JSON schema to validate their content. See also https://docs.gitlab.com/ee/development/migration_style_guide.html#storing-json-in-database.
The content of AlertManagement::Alert#payload is currently not validated using a JsonSchemaValidator.
Alert's payload can have different shapes and is not static. We are currently supporting Custom payload for Generic alerts but also Prometheus alerts. With Custom Integrations (previously known as "Integration Builder") we will support even more highly customized formats and mappings definitions.
Proposed Solution
Validate the alert payload dynamically depending on its source (Generic alert, Prometheus, a custom mapping definition):
- Generic alerts - see static payload definition
- Prometheus alerts - see static payload definition
- Custom Integration - (hand-wavy) generate a JSON schema dynamically from the custom mapping definitions
Prerequisites
Currently, JsonSchemaValidator only supports static schema files located in app/validators/json_schemas/<file>.json. Example:
class Alert
validates :data, json_schema: { filename: "file" }
end
Because of the different payloads' shapes we cannot use a single file. However, we could allow JsonSchemaValidator to retrieve the schema Hash from a method instead:
class Alert
validates :data, json_schema: { hash: :json_schema_hash }
private
def json_schema_hash
case
when generic? then generic_schema_hash
when prometheus? then prometheus_schema_hash
when custom_integration? then generate_custom_integration_schema_hash
else
...
end
end
end
Results/Outcome
After discussion between Peter and Kamil it was suggested NOT to support JSON validation for dynamic custom payloads (via "Custom Integrations").
Please see #227777 (comment 385950428) and #227777 (comment 386652194) more context.