Logo: Relish

  1. Sign in

Project: Uri-format-validator

Scheme component constraints

Background
Given
a file named "test_example_model.rb" with:
require "active_model"
require "uri_format_validator"
require_relative "./example_model"

object = ExampleModel.new

object.uri = "http://example.com"
puts object.valid?

object.uri = "https://example.com"
puts object.valid?

object.uri = "telnet://example.com"
puts object.valid?

object.uri = "ssh://example.com"
puts object.valid?

object.uri = "nonexisting://example.com"
puts object.valid?
Scenarios
By default, accept every valid URI
And
a file named "example_model.rb" with:
class ExampleModel
  include ActiveModel::Validations

  attr_accessor :uri
  validates :uri, uri: true
end
When
I run ruby test_example_model.rb
Then
it should pass with exactly:
true
true
true
true
true
When sheme option is a string, it specifies the only allowed scheme
And
a file named "example_model.rb" with:
class ExampleModel
  include ActiveModel::Validations

  attr_accessor :uri
  validates :uri, uri: { scheme: "http" }
end
When
I run ruby test_example_model.rb
Then
it should pass with exactly:
true
false
false
false
false
When sheme option is a regular expression, it specifies the pattern for allowed schemes
And
a file named "example_model.rb" with:
class ExampleModel
  include ActiveModel::Validations

  attr_accessor :uri
  validates :uri, uri: { scheme: /^https?$/ }
end
When
I run ruby test_example_model.rb
Then
it should pass with exactly:
true
true
false
false
false
When sheme option is an array, it specifies a set of allowed schemes
And
a file named "example_model.rb" with:
class ExampleModel
  include ActiveModel::Validations

  attr_accessor :uri
  validates :uri, uri: { scheme: ["telnet", /^https?$/] }
end
When
I run ruby test_example_model.rb
Then
it should pass with exactly:
true
true
true
false
false

Last published almost 3 years ago by Ribose.