Sha256: 9ce9b5027d5022f0c600933207af80b033e8fa6f635c347b8264ce3861870d11

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'luna_park/validators/dry'
require 'cyclone_lariat/errors'

module CycloneLariat
  module Messages
    module V2
      class Validator < LunaPark::Validators::Dry
        UUID_MATCHER = /^\h{8}-\h{4}-(\h{4})-\h{4}-\h{12}$/.freeze
        ISO8601_MATCHER = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?$/.freeze

        validation_schema do
          params do
            required(:uuid).value(format?: UUID_MATCHER)
            required(:publisher).filled(:string)
            required(:type).filled(:string)
            required(:version).filled(:integer).value(eql?: 2)
            required(:data).value(:hash?)
            optional(:request_id).filled(:string)
            required(:sent_at).value(format?: ISO8601_MATCHER)
            required(:subject).hash do
              required(:type).filled(:string)
              required(:uuid).value(format?: UUID_MATCHER)
            end
            required(:object).hash do
              required(:type).filled(:string)
              required(:uuid).value(format?: UUID_MATCHER)
            end
          end
        end

        def check!
          raise Errors::InvalidMessage.new(message: params, validation_errors: errors) unless success?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cyclone_lariat-1.0.0 lib/cyclone_lariat/messages/v2/validator.rb