Sha256: c847e7943f14c220a16c459c836536a5e2ace6fb49f817e45cda336c63bac2d8

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

module ActiveModel
  module Validations
    class BasicMultilingualPlaneValidator < ActiveModel::EachValidator
      OUTSIDE_BMP = /[^\u{0}-\u{FFFF}]/

      def validate_each(record, attribute, value)
        return if value.nil?
        if value.to_s.index(OUTSIDE_BMP)
          errors_options = options.except(:characters_outside_basic_multilingual_plane)
          default_message = options[:characters_outside_basic_multilingual_plane]
          errors_options[:message] ||= default_message if default_message
          record.errors.add(attribute, :characters_outside_basic_multilingual_plane, errors_options)
        end
      end
    end

    module HelperMethods
      def validates_basic_multilingual_plane_of(*attr_names)
        validates_with ActiveModel::Validations::BasicMultilingualPlaneValidator, _merge_attributes(attr_names)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-databasevalidations-0.1.1 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.1.0 lib/active_model/validations/basic_multilingual_plane.rb