Sha256: 6baca8f20be9243c990283ddc0dda1e1de15dc970e837599e2cc0bb8b67fa8ac

Contents?: true

Size: 1.44 KB

Versions: 27

Compression:

Stored size: 1.44 KB

Contents

module Foobara
  module TypeDeclarations
    module Handlers
      class ExtendRegisteredModelTypeDeclaration < ExtendRegisteredTypeDeclaration
        class MutableValidator < TypeDeclarations::TypeDeclarationValidator
          class InvalidMutableValueGivenError < Value::DataError
            class << self
              def context_type_declaration
                {
                  invalid_key: :symbol,
                  valid_attribute_names: [:symbol],
                  mutable: [:symbol]
                }
              end
            end
          end

          def applicable?(value)
            value.key?(:mutable)
          end

          def validation_errors(strict_type_declaration)
            mutable = strict_type_declaration[:mutable]
            return if [true, false].include?(mutable)

            model_type = type_for_declaration(strict_type_declaration[:type])
            valid_attribute_names = model_type.element_types.element_types.keys

            mutable.map do |key|
              unless valid_attribute_names.include?(key)
                build_error(
                  message: "#{key} is not a valid attribute, expected one of #{valid_attribute_names}",
                  context: {
                    invalid_key: key,
                    valid_attribute_names:,
                    mutable:
                  }
                )
              end
            end.compact
          end
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
foobara-0.0.7 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb
foobara-0.0.6 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb
foobara-0.0.5 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb
foobara-0.0.4 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb
foobara-0.0.3 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb
foobara-0.0.2 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb
foobara-0.0.1 projects/model/src/extensions/type_declarations/handlers/extend_registered_model_type_declaration/mutable_validator.rb