lib/pupa.rb in pupa-0.2.2 vs lib/pupa.rb in pupa-0.2.3

- old
+ new

@@ -6,10 +6,12 @@ require 'active_support/core_ext/hash/except' require 'active_support/core_ext/hash/slice' require 'active_support/core_ext/object/blank' require 'active_support/inflector' +require 'mail' + require 'pupa/models/concerns/indifferent_access' require 'pupa/models/concerns/contactable' require 'pupa/models/concerns/identifiable' require 'pupa/models/concerns/linkable' require 'pupa/models/concerns/nameable' @@ -63,5 +65,22 @@ end # @see http://api.rubyonrails.org/classes/String.html#method-i-blank-3F alias_method :blank?, :empty? end + +# @see https://github.com/ruby-json-schema/json-schema/tree/master/lib/json-schema/attributes/formats +JSON::Validator.register_format_validator('email', lambda{|data| + return unless data.is_a?(String) + address = Mail::Address.new(data) + unless address.address == data && address.domain && address.domain.split('.').size > 1 + raise JSON::Schema::CustomFormatError.new("must be a valid email address (#{data})") + end +}) + +JSON::Validator.register_format_validator('uri', lambda{|data| + return unless data.is_a?(String) + re = URI::DEFAULT_PARSER.regexp[:ABS_URI] + unless re.match(data) + raise JSON::Schema::CustomFormatError.new("must be a valid email URI (#{data})") + end +}) \ No newline at end of file