Class: GraphQL::Schema::Validator::LengthValidator

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

Overview

Use this to enforce a .length restriction on incoming values. It works for both Strings and Lists.

Examples:

Allow no more than 10 IDs


argument :ids, [ID], required: true, validates: { length: { maximum: 10 } }

Require three selections


argument :ice_cream_preferences, [ICE_CREAM_FLAVOR], required: true, validates: { length: { is: 3 } }

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(maximum: nil, too_long: "%{validated} is too long (maximum is %{count})", minimum: nil, too_short: "%{validated} is too short (minimum is %{count})", is: nil, within: nil, wrong_length: "%{validated} is the wrong length (should be %{count})", message: nil, **default_options) ⇒ LengthValidator

Returns a new instance of LengthValidator.

Parameters:

  • maximum (Integer) (defaults to: nil)
  • too_long (String) (defaults to: "%{validated} is too long (maximum is %{count})")

    Used when maximum is exceeded or value is greater than within

  • minimum (Integer) (defaults to: nil)
  • too_short (String) (defaults to: "%{validated} is too short (minimum is %{count})")

    Used with value is less than minimum or less than within

  • is (Integer) (defaults to: nil)

    Exact length requirement

  • wrong_length (String) (defaults to: "%{validated} is the wrong length (should be %{count})")

    Used when value doesn’t match is

  • within (Range) (defaults to: nil)

    An allowed range (becomes minimum: and maximum: under the hood)

  • message (String) (defaults to: nil)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/graphql/schema/validator/length_validator.rb', line 25

def initialize(
  maximum: nil, too_long: "%{validated} is too long (maximum is %{count})",
  minimum: nil, too_short: "%{validated} is too short (minimum is %{count})",
  is: nil, within: nil, wrong_length: "%{validated} is the wrong length (should be %{count})",
  message: nil,
  **default_options
)
  if within && (minimum || maximum)
    raise ArgumentError, "`length: { ... }` may include `within:` _or_ `minimum:`/`maximum:`, but not both"
  end
  # Under the hood, `within` is decomposed into `minimum` and `maximum`
  @maximum = maximum || (within && within.max)
  @too_long = message || too_long
  @minimum = minimum || (within && within.min)
  @too_short = message || too_short
  @is = is
  @wrong_length = message || wrong_length
  super(**default_options)
end

Instance Method Details

#validate(_object, _context, value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/graphql/schema/validator/length_validator.rb', line 45

def validate(_object, _context, value)
  return if permitted_empty_value?(value) # pass in this case
  length = value.nil? ? 0 : value.length
  if @maximum && length > @maximum
    partial_format(@too_long, { count: @maximum })
  elsif @minimum && length < @minimum
    partial_format(@too_short, { count: @minimum })
  elsif @is && length != @is
    partial_format(@wrong_length, { count: @is })
  end
end