Skip to content

Commit

Permalink
Merge pull request #2652 from cloudfoundry/decorate-regex
Browse files Browse the repository at this point in the history
Create wrappers for regexes instead of singleton methods
  • Loading branch information
sethboyles authored Jan 31, 2022
2 parents 5e136e8 + 17ad492 commit 58eaf84
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions lib/vcap/rest_api/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,62 @@
module VCAP
module RestAPI
class Message < JsonMessage
# The schema validator used by class `JsonMessage` calls the `inspect`
# method on the regexp object to get a description of the regex. We tweak
# the regexp object so that the `inspect` method generates a readable
# description for us through `VCAP::RestAPI::Message#schema_doc` method.
def self.readable_regexp(regexp, description, default_error_message)
regexp.define_singleton_method(:inspect) do
description
class UrlDecorator < SimpleDelegator
def inspect
'String /URL_REGEX/'
end

alias_method :to_s, :inspect
def default_error_message
'must be a valid URL'
end
end

regexp.define_singleton_method(:to_s) do
inspect
class HttpsUrlDecorator < SimpleDelegator
def inspect
'String /HTTPS_URL_REGEX/'
end

regexp.define_singleton_method(:default_error_message) do
default_error_message
alias_method :to_s, :inspect
def default_error_message
'must be a valid HTTPS URL'
end
end

class EmailDecorator < SimpleDelegator
def inspect
'String /EMAIL_REGEX/'
end

regexp
alias_method :to_s, :inspect
def default_error_message
'must be a valid email'
end
end

class GitUrlDecorator < SimpleDelegator
def inspect
'String /GIT_URL_REGEX/'
end

alias_method :to_s, :inspect

def default_error_message
'must be a valid git URL'
end
end
# The schema validator used by class `JsonMessage` calls the `inspect`
# method on the regexp object to get a description of the regex. We tweak
# the regexp object so that the `inspect` method generates a readable
# description for us through `VCAP::RestAPI::Message#schema_doc` method.
def self.schema_doc(schema)
schema.deparse
end

URL = readable_regexp(URI::DEFAULT_PARSER.make_regexp(%w(http https)),
'String /URL_REGEX/',
'must be a valid URL')
HTTPS_URL = readable_regexp(URI::DEFAULT_PARSER.make_regexp('https'),
'String /HTTPS_URL_REGEX/',
'must be a valid HTTPS URL')
EMAIL = readable_regexp(RFC822::EMAIL_REGEXP_WHOLE,
'String /EMAIL_REGEX/',
'must be a valid email')
GIT_URL = readable_regexp(URI::DEFAULT_PARSER.make_regexp(%w(http https git)),
'String /GIT_URL_REGEX/',
'must be a valid git URL')
URL = UrlDecorator.new(URI::DEFAULT_PARSER.make_regexp(%w(http https)))
HTTPS_URL = HttpsUrlDecorator.new(URI::DEFAULT_PARSER.make_regexp('https'))
EMAIL = EmailDecorator.new(RFC822::EMAIL_REGEXP_WHOLE)
GIT_URL = GitUrlDecorator.new(URI::DEFAULT_PARSER.make_regexp(%w(http https git)))

# The block will be evaluated in the context of the schema validator used
# by class `JsonMessage` viz. `Membrane`.
Expand Down

0 comments on commit 58eaf84

Please sign in to comment.