Sha256: 88a6d61de7b174f9d27800c707448afe9644697d6405af1ad879724b11b5a7dc

Contents?: true

Size: 1.94 KB

Versions: 11

Compression:

Stored size: 1.94 KB

Contents

require 'mail'

module Pupa
  module Refinements
    # Validates "email" and "uri" formats. Using Ruby's refinements doesn't seem
    # to work, possibly because `refine` can't be used with `prepend`.
    module FormatAttribute
      # @see http://my.rails-royce.org/2010/07/21/email-validation-in-ruby-on-rails-without-regexp/
      def validate(current_schema, data, fragments, processor, validator, options = {})
        case current_schema.schema['format']
        when 'email'
          if String === data
            address = Mail::Address.new(data)
            unless (address.address == data && address.domain && address.__send__(:tree).domain.dot_atom_text.elements.size > 1 rescue false)
              error_message = "The property '#{build_fragment(fragments)}' must be a valid email address (#{data})"
              validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
            end
          else
            error_message = "The property '#{build_fragment(fragments)}' must be a string (#{data})"
            validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
          end
        when 'uri'
          if String === data
            re = URI::DEFAULT_PARSER.regexp[:ABS_URI]
            unless re.match(data)
              error_message = "The property '#{build_fragment(fragments)}' must be a valid URI (#{data})"
              validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
            end
          else
            error_message = "The property '#{build_fragment(fragments)}' must be string (#{data})"
            validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors])
          end
        else
          super
        end
      end
    end
  end
end

class JSON::Schema::FormatAttribute
  class << self
    prepend Pupa::Refinements::FormatAttribute
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
pupa-0.1.8 lib/pupa/refinements/json-schema.rb
pupa-0.1.7 lib/pupa/refinements/json-schema.rb
pupa-0.1.6 lib/pupa/refinements/json-schema.rb
pupa-0.1.5 lib/pupa/refinements/json-schema.rb
pupa-0.1.4 lib/pupa/refinements/json-schema.rb
pupa-0.1.3 lib/pupa/refinements/json-schema.rb
pupa-0.1.2 lib/pupa/refinements/json-schema.rb
pupa-0.1.1 lib/pupa/refinements/json-schema.rb
pupa-0.1.0 lib/pupa/refinements/json-schema.rb
pupa-0.0.13 lib/pupa/refinements/json-schema.rb
pupa-0.0.12 lib/pupa/refinements/json-schema.rb