Skip to content

Use JSONSchema validation errors in EventStore data error

What does this MR do and why?

Prior to this change, Gitlab::EventStore::Event would validate event schema but not reveal any information about the validation errors themselves. The underlying library actually reports all of this information if we use a the #validate method, and we can then use the JSONSchema::Errors.pretty to get human readable output for the error message.

In this change, we replace the actual schema with just the human readable errors. This makes things easier to debug and means that each distinct type of invalid event gets its own error message. This should be safe to output all the time. It only reports the error's type and location in the event; it does not include any event data.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

How to set up and validate locally

A few examples you can try out in a GDK rails console:

# See that happy path works as expected
Packages::PackageCreatedEvent.new(data: {project_id: 1, name: '', id: 1, package_type: 'generic'})

# Missing all required keys
Packages::PackageCreatedEvent.new(data: {})
# Gitlab::EventStore::InvalidEvent: Data for event Packages::PackageCreatedEvent does not match the defined schema: ["root is missing required keys: project_id, id, name, package_type"]
# from /Users/hfyngvason/src/gitlab-development-kit/gitlab/lib/gitlab/event_store/event.rb:68:in `validate_data!'

# Example with invalid package_type
Packages::PackageCreatedEvent.new(data: {project_id: 1, name: '', id: 1, package_type: 'abcd'})
# Gitlab::EventStore::InvalidEvent: Data for event Packages::PackageCreatedEvent does not match the defined schema: ["property '/package_type' is not one of: [\"maven\", \"npm\", \"conan\", \"nuget\", \"pypi\", \"composer\", \"generic\", \"golang\", \"debian\", \"rubygems\", \"helm\", \"terraform_module\", \"rpm\", \"ml_model\"]"]
# from /Users/hfyngvason/src/gitlab-development-kit/gitlab/lib/gitlab/event_store/event.rb:68:in `validate_data!'
Edited by Hordur Freyr Yngvason

Merge request reports