Class: GraphQL::Schema::Validator::AllowNullValidator

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

Overview

Use this to specifically reject or permit nil values (given as null from GraphQL).

Examples:

require a non-null value for an argument if it is provided

argument :name, String, required: false, validates: { allow_null: false }

Constant Summary collapse

MESSAGE =
"%{validated} can't be null"

Constants included from EmptyObjects

EmptyObjects::EMPTY_ARRAY, 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_null_positional, allow_null: nil, message: MESSAGE, **default_options) ⇒ AllowNullValidator

Returns a new instance of AllowNullValidator.



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

def initialize(allow_null_positional, allow_null: nil, message: MESSAGE, **default_options)
  @message = message
  super(**default_options)
  @allow_null = allow_null.nil? ? allow_null_positional : allow_null
end

Instance Method Details

#validate(_object, _context, value) ⇒ Object



18
19
20
21
22
# File 'lib/graphql/schema/validator/allow_null_validator.rb', line 18

def validate(_object, _context, value)
  if value.nil? && !@allow_null
    @message
  end
end