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

Extended by:
ActiveSupport::Concern
Included in:
Objects::Base
Defined in:
lib/jsonapionify/structure/helpers/validations.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#allowed_type_mapObject



263
264
265
266
267
268
269
270
271
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 263

def allowed_type_map
  self.class.allowed_type_map.dup.tap do |hash|
    wildcard_types = hash.delete('*')
    keys.each do |k|
      hash[k] ||= {}
      hash[k].deep_merge! wildcard_types
    end if wildcard_types.present?
  end
end

#permitted_key?(key) ⇒ Boolean

Instance Methods

Returns:



231
232
233
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 231

def permitted_key?(key)
  !allow_only_permitted || permitted_keys.map(&:to_sym).include?(key.to_sym)
end

#permitted_keysObject



259
260
261
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 259

def permitted_keys
  expand_keys(*self.class.permitted_keys)
end

#permitted_type_for?(key) ⇒ Boolean

Returns:



245
246
247
248
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 245

def permitted_type_for?(key)
  types = permitted_types_for(key)
  types.empty? || permitted_types_for(key).any? { |type| self[key].is_a? type }
end

#permitted_types_for(key) ⇒ Object



250
251
252
253
254
255
256
257
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 250

def permitted_types_for(key)
  options_and_types = allowed_type_map[key] || {}
  options_and_types.each_with_object([]) do |(options, types), permitted_types|
    JSONAPIonify::Continuation.new(**options).check(self) do
      permitted_types.concat types
    end
  end.uniq
end

#required_key?(key) ⇒ Boolean

Returns:



235
236
237
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 235

def required_key?(key)
  required_keys.include? key
end

#required_keysObject



239
240
241
242
243
# File 'lib/jsonapionify/structure/helpers/validations.rb', line 239

def required_keys
  self.class.required_keys.select do |_, options|
    JSONAPIonify::Continuation.new(**options).check(self) { true }
  end.keys
end