Sha256: 16da082eebdc8187594d437eecb31649ef63bb8656ef90cb036cb7305b077b28

Contents?: true

Size: 960 Bytes

Versions: 13

Compression:

Stored size: 960 Bytes

Contents

require 'rejectu/rejectu'

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

      def validate_each(record, attribute, value)
        return if value.nil?
        return if value.to_s.encoding != Encoding::UTF_8

        unless Rejectu.valid?(value.to_s)
          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

13 entries across 13 versions & 1 rubygems

Version Path
activerecord-databasevalidations-0.5.0 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.4.0 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.3.1 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.3.0 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.6 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.5 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.4 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.3 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.2 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.1 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.2.0 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.1.3 lib/active_model/validations/basic_multilingual_plane.rb
activerecord-databasevalidations-0.1.2 lib/active_model/validations/basic_multilingual_plane.rb