Module: JSONAPIonify::Structure::Helpers::Validations::ClassMethods

Defined in:
lib/jsonapionify/structure/helpers/validations.rb

Instance Method Summary collapse

Instance Method Details

#may_contain!(*keys) ⇒ Object

Fails if keys other than these exist



118
119
120
121
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 118

def may_contain!(*keys)
  self.allow_only_permitted = true
  self.permitted_keys       = [*permitted_keys, *keys].uniq
end

#may_not_exist!(key, without: nil, **options) ⇒ Object

Fails if this key doesn’t exist and the ‘given` does.



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 21

def may_not_exist!(key, without: nil, **options)
  return may_not_exist_without! key, without if without
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      invalid_keys = self.keys.map(&:to_sym) & keys.map(&:to_sym)
      if invalid_keys.present?
        invalid_keys.each do |k|
          errors.add(k, 'is not permitted')
        end
      end
    end
  end
end

#may_not_exist_without!(key, without, **options) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 35

def may_not_exist_without!(key, without, **options)
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      if has_key?(key) && !has_key?(without)
        errors.add(key, "may not exist without: `#{without}`")
      end
    end
  end
end

#must_contain!(*keys, **options) ⇒ Object

Fails if these keys dont exist



110
111
112
113
114
115
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 110

def must_contain!(*keys, **options)
  self.permitted_keys = [*self.permitted_keys, *keys]
  keys.each do |key|
    required_keys[key] = options
  end
end

#must_contain_one_of!(*keys, **options, &block) ⇒ Object

Fails if one of these keys does not exist



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 96

def must_contain_one_of!(*keys, **options, &block)
  self.permitted_keys = [*permitted_keys, *keys].uniq
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      keys       += self.keys.select(&block) if block_given?
      valid_keys = keys.map(&:to_sym) & self.keys.map(&:to_sym)
      unless valid_keys.present?
        errors.add('*', "must contain one of: #{keys_to_sentence(*self.keys)}")
      end
    end
  end
end

#must_not_coexist!(*keys, **options) ⇒ Object

Fails is more than one of the keys exists.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 124

def must_not_coexist!(*keys, **options)
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      keys             = expand_keys(*keys)
      conflicting_keys = keys & self.keys
      if conflicting_keys.length > 1
        conflicting_keys.each do |key|
          conflicts_with = conflicting_keys - [key]
          errors.add key, "conflicts with #{keys_to_sentence(*conflicts_with)}"
        end
      end
    end
  end
end

#must_not_contain!(*keys, deep: false, **options, &block) ⇒ Object

Fails if these keys exist



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 61

def must_not_contain!(*keys, deep: false, **options, &block)
  return must_not_contain_deep!(*keys, **options) if deep === true
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      keys         += self.keys.select(&block) if block_given?
      invalid_keys = self.keys.map(&:to_sym) & keys.map(&:to_sym)
      if invalid_keys.present?
        invalid_keys.each do |key|
          errors.add(key, 'is not permitted')
        end
      end
    end
  end
end

#must_not_contain_deep!(*keys, **options, &block) ⇒ Object

Fails if these keys exist deeply



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 77

def must_not_contain_deep!(*keys, **options, &block)
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      all_invalid_keys   = []
      keys               += self.keys.select(&block) if block_given?
      is_invalid         = proc do |hash|
        invalid_keys     = hash.keys.map(&:to_sym) & keys.map(&:to_sym)
        all_invalid_keys += invalid_keys
        children         = hash.values.select { |v| v.respond_to?(:keys) && v.respond_to?(:values) }
        invalid_keys.present? | children.map { |c| is_invalid.call c }.reduce(:|)
      end
      if is_invalid.call(self)
        errors.add('*', "cannot contain keys #{keys_to_sentence(*all_invalid_keys.uniq)}")
      end
    end
  end
end

#should_not_contain!(*keys, **options, &block) ⇒ Object

Warn but do not fail if keys present



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 46

def should_not_contain!(*keys, **options, &block)
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      keys         += self.keys.select(&block) if block_given?
      invalid_keys = self.keys.map(&:to_sym) & keys.map(&:to_sym)
      if invalid_keys.present?
        invalid_keys.each do |key|
          warnings.add(key, 'is not permitted')
        end
      end
    end
  end
end

#type_of!(key, must_be:, allow_nil: false, **options) ⇒ Object

Validates key is type



173
174
175
176
177
178
179
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 173

def type_of!(key, must_be:, allow_nil: false, **options)
  allowed_type_map[key] ||= {}
  types                 = Array.wrap(must_be)
  types << NilClass if allow_nil
  allowed_type_map[key][options] ||= []
  allowed_type_map[key][options] += types
end

#validate!(key, with: nil, message: 'is not valid', **options, &block) ⇒ Object

Validates key using a provided method or block



140
141
142
143
144
145
146
147
148
149
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 140

def validate!(key, with: nil, message: 'is not valid', **options, &block)
  before_validation do
    if has_key? key
      JSONAPIonify::Continuation.new(**options).check(self, key, self[key]) do
        real_block = get_block_from_options(with, &block)
        errors.add key, message unless real_block.call(self, key, self[key])
      end
    end
  end
end

#validate_each!(with: nil, message: 'not valid', **options, &block) ⇒ Object

Validates the object using a provided method or block



161
162
163
164
165
166
167
168
169
170
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 161

def validate_each!(with: nil, message: 'not valid', **options, &block)
  before_validation do
    real_block = get_block_from_options(with, &block)
    keys.each do |key|
      JSONAPIonify::Continuation.new(**options).check(self, key, self[key]) do
        errors.add key, message unless real_block.call(self, key, self[key])
      end
    end
  end
end

#validate_object!(with: nil, message: 'is not valid', **options, &block) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 151

def validate_object!(with: nil, message: 'is not valid', **options, &block)
  before_validation do
    JSONAPIonify::Continuation.new(**options).check(self) do
      real_block = get_block_from_options(with, &block)
      errors.add '*', message unless real_block.call(self)
    end
  end
end

#validation_error(message) ⇒ Object

Raise the validation errors



13
14
15
16
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 13

def validation_error(message)
  message = "#{name}: #{message}"
  ValidationError.new(message)
end