Class: Grape::Validations::ValidationsSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/grape/validations/validations_spec.rb

Overview

Frozen value object holding everything ParamsScope#validates needs to know about a single requires/optional declaration. Built once from the raw validations hash supplied by the DSL; the raw hash is never mutated.

Splits the raw entries into three logical buckets:

  • Spec-consumed keys (type/types/coerce*, presence/message, default/fail_fast, doc keys) — exposed via named accessors and never handed to validator dispatch.

  • Shared opts (allow_blank, fail_fast) — read by every validator at construction time via #shared_opts.

  • Validator entries (everything else, e.g. regexp, length, values, allow_blank, custom validators) — exposed via #validator_entries for the dispatch loop.

Same key can land in more than one bucket (e.g. allow_blank is both a shared opt and a validator entry; length is both a doc source and a validator entry).

Constant Summary collapse

SPEC_CONSUMED_KEYS =

Keys consumed by the spec itself; must NOT be dispatched as validators by the caller. Documentation-only keys are filtered through a separate set so that dual-purpose keys (length, default, values, except_values) aren’t accidentally swallowed.

%i[
  type types coerce coerce_with coerce_message
  presence message
  fail_fast
  desc description documentation
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ ValidationsSpec

Returns a new instance of ValidationsSpec.

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/grape/validations/validations_spec.rb', line 42

def initialize(raw)
  raise ArgumentError, ':type may not be supplied with :types' if raw.key?(:type) && raw.key?(:types)

  @raw = raw
  @coerce_type, @coerce_message, @coerce_method = parse_coerce(raw)
  @values = resolve_value(raw[:values])
  @except_values = resolve_value(raw[:except_values])
  @default = raw[:default]
  @presence_options = raw[:presence]
  @allow_blank = resolve_value(raw[:allow_blank])
  @fail_fast = raw[:fail_fast] || false

  @coerce_type = guess_coerce_type(@coerce_type, @values, @except_values)
  @shared_opts = { allow_blank: @allow_blank, fail_fast: @fail_fast }.freeze
  @validator_entries = build_validator_entries(raw)

  validate!

  freeze
end

Instance Attribute Details

#allow_blankObject (readonly)

Returns the value of attribute allow_blank.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def allow_blank
  @allow_blank
end

#coerce_messageObject (readonly)

Returns the value of attribute coerce_message.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def coerce_message
  @coerce_message
end

#coerce_methodObject (readonly)

Returns the value of attribute coerce_method.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def coerce_method
  @coerce_method
end

#coerce_typeObject (readonly)

Returns the value of attribute coerce_type.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def coerce_type
  @coerce_type
end

#defaultObject (readonly)

Returns the value of attribute default.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def default
  @default
end

#except_valuesObject (readonly)

Returns the value of attribute except_values.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def except_values
  @except_values
end

#fail_fastObject (readonly)

Returns the value of attribute fail_fast.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def fail_fast
  @fail_fast
end

#presence_optionsObject (readonly)

Returns the value of attribute presence_options.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def presence_options
  @presence_options
end

#rawObject (readonly)

Returns the value of attribute raw.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def raw
  @raw
end

#shared_optsObject (readonly)

Returns the value of attribute shared_opts.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def shared_opts
  @shared_opts
end

#validator_entriesObject (readonly)

Returns the value of attribute validator_entries.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def validator_entries
  @validator_entries
end

#valuesObject (readonly)

Returns the value of attribute values.



36
37
38
# File 'lib/grape/validations/validations_spec.rb', line 36

def values
  @values
end

Class Method Details

.from(validations) ⇒ Object



38
39
40
# File 'lib/grape/validations/validations_spec.rb', line 38

def self.from(validations)
  new(validations)
end

Instance Method Details

#coerce_optionsObject



67
68
69
# File 'lib/grape/validations/validations_spec.rb', line 67

def coerce_options
  CoerceOptions.new(type: @coerce_type, coerce_method: @coerce_method, message: @coerce_message)
end

#required?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/grape/validations/validations_spec.rb', line 63

def required?
  !@presence_options.nil? && @presence_options != false
end