lib/compel/builder/string.rb in compel-0.3.6 vs lib/compel/builder/string.rb in compel-0.3.7

- old
+ new

@@ -1,17 +1,35 @@ module Compel module Builder class String < Schema + # Taken from ruby_regex gem by @eparreno + # https://github.com/eparreno/ruby_regex + URL_REGEX = /(\A\z)|(\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?\z)/ix + + # Taken from Michael Hartl's 'The Ruby on Rails Tutorial' + # https://www.railstutorial.org/book + EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i + include CommonValue def initialize super(Coercion::String) end def format(regex) options[:format] = Coercion.coerce!(regex, Coercion::Regexp) + self + end + + def url + options[:format] = URL_REGEX + self + end + + def email + options[:format] = EMAIL_REGEX self end end