Sha256: a82ce853fed759c781242230740261b4d9eead8b260d9e7beed0ea6ebae23c02

Contents?: true

Size: 764 Bytes

Versions: 6

Compression:

Stored size: 764 Bytes

Contents

# typed: strict

module Typed
  module Validations
    class FieldTypeValidator
      extend T::Sig

      include FieldValidator

      sig { override.params(field: Field, value: Value).returns(ValidationResult) }
      def validate(field:, value:)
        if field.type == value.class
          Success.new(ValidatedValue.new(name: field.name, value: value))
        elsif field.required? && value.nil?
          Failure.new(RequiredFieldError.new(field_name: field.name))
        elsif field.optional? && value.nil?
          Success.new(ValidatedValue.new(name: field.name, value: value))
        else
          Failure.new(TypeMismatchError.new(field_name: field.name, field_type: field.type, given_type: value.class))
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sorbet-schema-0.3.0 lib/typed/validations/field_type_validator.rb
sorbet-schema-0.2.2 lib/typed/validations/field_type_validator.rb
sorbet-schema-0.2.1 lib/typed/validations/field_type_validator.rb
sorbet-schema-0.2.0 lib/typed/validations/field_type_validator.rb
sorbet-schema-0.1.1 lib/typed/validations/field_type_validator.rb
sorbet-schema-0.1.0 lib/typed/validations/field_type_validator.rb