Module: JsonSchematize::FieldValidators

Included in:
Field
Defined in:
lib/json_schematize/field_validators.rb

Instance Method Summary collapse

Instance Method Details

#validate_converter!Object



16
17
18
19
20
21
22
23
24
# File 'lib/json_schematize/field_validators.rb', line 16

def validate_converter!
  return if validate_converter_nil!

  return if validate_converter_proc!

  return if validate_converter_hash!

 raise JsonSchematize::FieldError, ":converter passed unexpected type. Expected [Hash, Proc, nil]. Given [#{@converter.class}]"
end

#validate_converter_hash!Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/json_schematize/field_validators.rb', line 38

def validate_converter_hash!
  return false unless converter.is_a?(Hash)

  if @converter.keys.map(&:name).sort != @acceptable_types.map(&:name).sort
    raise JsonSchematize::FieldError, ":converter given a hash. Keys of hash do not match klass types of accepted types. Given [#{converter.keys}]. Expected [#{@acceptable_types}]"
  end

  return true if @converter.values.all? { |klass| klass.is_a?(Proc) }

  raise JsonSchematize::FieldError, ":converter given a hash. Values of proc must all be of type Proc"
end

#validate_converter_nil!Object



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

def validate_converter_nil!
  return false unless @converter.nil?

  return true if @acceptable_types.length == 1

  raise JsonSchematize::FieldError, ":converter expected to be populated with multiple accepted types [#{@acceptable_types}]"
end

#validate_converter_proc!Object



34
35
36
# File 'lib/json_schematize/field_validators.rb', line 34

def validate_converter_proc!
  @converter.is_a?(Proc)
end

#validate_dig!Object



63
64
65
# File 'lib/json_schematize/field_validators.rb', line 63

def validate_dig!
  raise JsonSchematize::FieldError, ":dig expected to be an Array" unless @dig.is_a?(Array)
end

#validate_dig_type!Object



54
55
56
57
# File 'lib/json_schematize/field_validators.rb', line 54

def validate_dig_type!
  values = JsonSchematize::Field::EXPECTED_DIG_TYPE
  raise JsonSchematize::FieldError, ":dig_type expected to be an #{values}" unless values.include?(@dig_type)
end

#validate_name!Object



67
68
69
# File 'lib/json_schematize/field_validators.rb', line 67

def validate_name!
  raise JsonSchematize::FieldError, ":name expected to be symbol" unless @name.is_a?(Symbol)
end

#validate_required!Object



50
51
52
# File 'lib/json_schematize/field_validators.rb', line 50

def validate_required!
  raise JsonSchematize::FieldError, ":required expected to be an boolean" unless [true, false].include?(@required)
end

#validate_type!(t:, message: ":type expected to be a Class object") ⇒ Object



71
72
73
74
75
76
77
# File 'lib/json_schematize/field_validators.rb', line 71

def validate_type!(t:, message: ":type expected to be a Class object")
  raise JsonSchematize::FieldError, message unless t.class == Class

  return if @acceptable_types.include?(t)

  @acceptable_types << t
end

#validate_types!Object



79
80
81
82
83
84
# File 'lib/json_schematize/field_validators.rb', line 79

def validate_types!
  raise JsonSchematize::FieldError, ":types expected to be an array" unless @types.is_a?(Array)
  @types.each do |t|
     validate_type!(t: t, message: ":types expected to be an array with class Objects")
  end
end

#validate_validator!Object



59
60
61
# File 'lib/json_schematize/field_validators.rb', line 59

def validate_validator!
  raise JsonSchematize::FieldError, ":validator expected to be an proc" unless @validator.is_a?(Proc)
end

#validations!Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/json_schematize/field_validators.rb', line 5

def validations!
  validate_type!(t: @type)
  validate_types!
  validate_name!
  validate_validator!
  validate_required!
  validate_dig_type!
  validate_dig!
  validate_converter!
end