Class: JsonSchematize::Field

Inherits:
Object
  • Object
show all
Includes:
FieldTransformations, FieldValidators
Defined in:
lib/json_schematize/field.rb

Constant Summary collapse

EXPECTED_DIG_TYPE =
[DIG_SYMBOL = :symbol, DEFAULT_DIG = DIG_NONE =:none, DIG_STRING = :string]

Constants included from FieldTransformations

JsonSchematize::FieldTransformations::DEFAULT_CONVERTERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FieldValidators

#validate_converter!, #validate_converter_hash!, #validate_converter_nil!, #validate_converter_proc!, #validate_dig!, #validate_dig_type!, #validate_name!, #validate_required!, #validate_type!, #validate_types!, #validate_validator!, #validations!

Methods included from FieldTransformations

#transform_converter_type!, #transform_dig_type!, #transformations!

Constructor Details

#initialize(name:, types:, dig:, dig_type:, validator:, type:, required:, converter:, empty_value:, array_of_types: false) ⇒ Field

Returns a new instance of Field.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/json_schematize/field.rb', line 13

def initialize(name:, types:, dig:, dig_type:, validator:, type:, required:, converter:, empty_value:, array_of_types: false)
  @name = name
  @types = types
  @type = type
  @dig = dig.nil? ? [name] : dig
  @dig_type = dig_type || DEFAULT_DIG
  @required = required
  @validator = validator
  @acceptable_types = []
  @converter = converter
  @empty_value = empty_value
  @array_of_types = array_of_types
end

Instance Attribute Details

#acceptable_typesObject (readonly)

Returns the value of attribute acceptable_types.



9
10
11
# File 'lib/json_schematize/field.rb', line 9

def acceptable_types
  @acceptable_types
end

#array_of_typesObject (readonly)

Returns the value of attribute array_of_types.



9
10
11
# File 'lib/json_schematize/field.rb', line 9

def array_of_types
  @array_of_types
end

#converterObject (readonly)

Returns the value of attribute converter.



9
10
11
# File 'lib/json_schematize/field.rb', line 9

def converter
  @converter
end

#digObject (readonly)

Returns the value of attribute dig.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def dig
  @dig
end

#dig_typeObject (readonly)

Returns the value of attribute dig_type.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def dig_type
  @dig_type
end

#empty_valueObject (readonly)

Returns the value of attribute empty_value.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def empty_value
  @empty_value
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



9
10
11
# File 'lib/json_schematize/field.rb', line 9

def required
  @required
end

#symbolObject (readonly)

Returns the value of attribute symbol.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def symbol
  @symbol
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def type
  @type
end

#typesObject (readonly)

Returns the value of attribute types.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def types
  @types
end

#validatorObject (readonly)

Returns the value of attribute validator.



8
9
10
# File 'lib/json_schematize/field.rb', line 8

def validator
  @validator
end

Instance Method Details

#acceptable_value?(transformed_value:, raise_on_error:) ⇒ Boolean

Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/json_schematize/field.rb', line 40

def acceptable_value?(transformed_value:, raise_on_error:)
  if array_of_types
    if transformed_value.is_a?(empty_value) && required == false
      boolean = true
    else
      boolean = transformed_value.all? { |val| validate_acceptable_types(val: val) }
    end
  else
    boolean = validate_acceptable_types(val: transformed_value)
  end

  if raise_on_error && (boolean==false)
    raise JsonSchematize::InvalidFieldByType, ":#{name} is an invalid option based on acceptable klass types [#{@acceptable_types}]#{ " -- array_of_types enabled" if array_of_types }"
  end

  boolean
end

#acceptable_value_by_validator?(transformed_value:, raw_value:, raise_on_error:) ⇒ Boolean

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/json_schematize/field.rb', line 58

def acceptable_value_by_validator?(transformed_value:, raw_value:, raise_on_error:)
  if array_of_types
    if transformed_value.is_a?(empty_value) && required == false
      boolean = true
    else
      boolean = transformed_value.all? { |val| validator.call(transformed_value, raw_value) }
    end
  else
    boolean = validator.call(transformed_value, raw_value)
  end

  boolean = validator.call(transformed_value, raw_value)
  if raise_on_error && (boolean==false)
    raise JsonSchematize::InvalidFieldByValidator, ":#{name} is an invalid option based on validator :proc option; #{validator}#{ " -- array_of_types enabled" if array_of_types }"
  end

  boolean
end

#setup!Object



27
28
29
30
31
32
# File 'lib/json_schematize/field.rb', line 27

def setup!
  # validations must be done before transformations
  validations!
  transformations!
  @acceptable_types << ((empty_value.class == Class) ? empty_value : empty_value.class)
end

#value_from_field(params) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/json_schematize/field.rb', line 77

def value_from_field(params)
  begin
    value = params.dig(*dig)
  rescue TypeError => e
    msg = "Unable to dig #{dig} for field :#{name}. Returning nil"
    ::Kernel.warn(msg)
    return nil
  end

  { raw_value: value, transformed_value: value_transform(value: value)}
end

#value_transform(value:) ⇒ Object



34
35
36
37
38
# File 'lib/json_schematize/field.rb', line 34

def value_transform(value:)
  return iterate_array_of_types(value: value) if array_of_types

  raw_converter_call(value: value)
end