Sha256: 28b72357dc16cff35c0f8e014d6ddc6968833abf99974ba161c4ccdafb4f43ab
Contents?: true
Size: 1.05 KB
Versions: 8
Compression:
Stored size: 1.05 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.add(attribute, options[:message] || DEFAULT_INVALID_YAML_MESSAGE) end protected # @param value [Object] # @return [String, nil] def stringfy_value(value) unless value.respond_to?(:to_s) record.errors.add(attribute, options[:message] || DEFAULT_TO_S_UNAVAILABLE_MESSAGE) return nil end r = value.to_s return r if value.is_a?(::String) record.errors.add(attribute, options[:message] || DEFAULT_NOT_A_STRING_MESSAGE) nil end end end
Version data entries
8 entries across 8 versions & 1 rubygems