Class: GraphQL::Schema::Validator::AllowBlankValidator

Inherits:
GraphQL::Schema::Validator show all
Defined in:
lib/graphql/schema/validator/allow_blank_validator.rb

Overview

Use this to specifically reject values that respond to .blank? and respond truthy for that method.

Examples:

Require a non-empty string for an argument

argument :name, String, required: true, validate: { allow_blank: false }

Constant Summary

Constants included from FindInheritedValue::EmptyObjects

FindInheritedValue::EmptyObjects::EMPTY_ARRAY, FindInheritedValue::EmptyObjects::EMPTY_HASH

Instance Attribute Summary

Attributes inherited from GraphQL::Schema::Validator

#validated

Instance Method Summary collapse

Methods inherited from GraphQL::Schema::Validator

from_config, install, #partial_format, #permitted_empty_value?, uninstall, validate!

Constructor Details

#initialize(allow_blank_positional, allow_blank: nil, message: "%{validated} can't be blank", **default_options) ⇒ AllowBlankValidator

Returns a new instance of AllowBlankValidator.



11
12
13
14
15
# File 'lib/graphql/schema/validator/allow_blank_validator.rb', line 11

def initialize(allow_blank_positional, allow_blank: nil, message: "%{validated} can't be blank", **default_options)
  @message = message
  super(**default_options)
  @allow_blank = allow_blank.nil? ? allow_blank_positional : allow_blank
end

Instance Method Details

#validate(_object, _context, value) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/graphql/schema/validator/allow_blank_validator.rb', line 17

def validate(_object, _context, value)
  if value.respond_to?(:blank?) && value.blank?
    if (value.nil? && @allow_null) || @allow_blank
      # pass
    else
      @message
    end
  end
end