Skip to content

Validators

Matt Muller edited this page Apr 10, 2024 · 4 revisions

Validators are classes that provide a method for validating Ruby input types against the Smithy model. Validators have a 1:1 mapping to Smithy input structure shapes.

Each input structure shape will have a code generated validator. Hearth::Validator is used to validate input when given the value, the Ruby types to check against, and context for any error messaging.

module SampleService
  # @api private
  module Validators

    class HighScoreParams
      def self.validate!(input, context:)
        Hearth::Validator.validate_types!(input, Types::HighScoreParams, context: context)
        Hearth::Validator.validate_types!(input[:game], ::String, context: "#{context}[:game]")
        Hearth::Validator.validate_types!(input[:score], ::Integer, context: "#{context}[:score]")
      end
    end

  end
end

If validate_input is false in Config, the validate! method in the Validators will not be called for any operation. The validate_input option is true by default.