class GoldenFleece::ValidatorContext
Attributes
attribute[R]
errors[R]
parent_path[R]
persisted_json[R]
record[R]
schemas[R]
validatable_keys[R]
Public Class Methods
new(record, attribute, persisted_json, schemas, parent_path)
click to toggle source
# File lib/golden_fleece/validations/validator_context.rb, line 12 def initialize(record, attribute, persisted_json, schemas, parent_path) @persisted_json = persisted_json @schemas = schemas @parent_path = parent_path persisted_keys = if persisted_json && persisted_json.keys persisted_json.keys.map(&:to_sym) else [] end schemas_keys = schemas ? schemas.keys : [] @validatable_keys = (persisted_keys + schemas_keys).uniq @record = record @attribute = attribute @errors = [] end
Public Instance Methods
validate()
click to toggle source
# File lib/golden_fleece/validations/validator_context.rb, line 28 def validate validatable_keys.each do |key| path = build_json_path(parent_path, key) if validate_key key, path schema = schemas[key] value = schema.value.compute(record) validate_type(value, schema.types, path) validate_format(value, schema.format, path) # If the key's value is a nested JSON object, recurse down errors << ValidatorContext.new(record, attribute, (persisted_json ? persisted_json[key.to_s] : nil), schemas[key], path).validate if value.is_a?(Hash) end end errors.flatten end
Private Instance Methods
validate_format(value, valid_format, path)
click to toggle source
# File lib/golden_fleece/validations/validator_context.rb, line 66 def validate_format(value, valid_format, path) if valid_format begin valid_format.validate record, value rescue Exception => e errors << "Invalid format at #{error_suffix(attribute, path)}: #{e.message}" end end end
validate_key(key, path)
click to toggle source
# File lib/golden_fleece/validations/validator_context.rb, line 51 def validate_key(key, path) if schemas.keys.include? key true else errors << "Invalid key #{error_suffix(attribute, path)}" false end end
validate_type(value, valid_types, path)
click to toggle source
# File lib/golden_fleece/validations/validator_context.rb, line 60 def validate_type(value, valid_types, path) unless valid_types.any? { |valid_type| valid_type.matches? value } errors << "Invalid type at #{error_suffix(attribute, path)}, must be one of #{valid_types}" end end