Sha256: ce0d7da5a032ff403057276da43d876dfb4776dd8fa20dc70a99c2ae652f8265
Contents?: true
Size: 1.08 KB
Versions: 8
Compression:
Stored size: 1.08 KB
Contents
# frozen_string_literal: true require 'eac_ruby_utils/yaml' module EacRailsUtils class YamlValidator < ActiveModel::EachValidator DEFAULT_INVALID_YAML_MESSAGE = 'is not a valid YAML' DEFAULT_TO_S_UNAVAILABLE_MESSAGE = 'should respond to .to_s' DEFAULT_NOT_A_STRING_MESSAGE = '.to_s do not returned a String' def validate_each(record, attribute, value) return if value.blank? string_value = stringfy_value(value) return if string_value.nil? return if ::EacRubyUtils::Yaml.yaml?(string_value) record.errors[attribute] << (options[:message] || DEFAULT_INVALID_YAML_MESSAGE) end protected # @param value [Object] # @return [String, nil] def stringfy_value(value) # rubocop:disable Metrics/AbcSize unless value.respond_to?(:to_s) record.errors[attribute] << (options[:message] || DEFAULT_TO_S_UNAVAILABLE_MESSAGE) return nil end r = value.to_s return r if value.is_a?(::String) record.errors[attribute] << (options[:message] || DEFAULT_NOT_A_STRING_MESSAGE) nil end end end
Version data entries
8 entries across 8 versions & 1 rubygems