add own URI validation rule that allows nil

This commit is contained in:
Benjamin Neff 2015-07-27 03:47:56 +02:00
parent d7a5e71ce6
commit c1e700d560
12 changed files with 142 additions and 3 deletions

View file

@ -1,6 +1,5 @@
require "validation"
require "validation/rule/regular_expression"
require "validation/rule/uri"
# +valid+ gem namespace
module Validation
@ -20,6 +19,7 @@ require "diaspora_federation/validators/rules/guid"
require "diaspora_federation/validators/rules/not_nil"
require "diaspora_federation/validators/rules/public_key"
require "diaspora_federation/validators/rules/tag_count"
require "diaspora_federation/validators/rules/uri"
module DiasporaFederation
# Validators to perform basic sanity-checks on {DiasporaFederation::Entities federation entities}.

View file

@ -8,7 +8,7 @@ module DiasporaFederation
rule :diaspora_id, :diaspora_id
rule :url, :URI
rule :url, %i(not_nil URI)
rule :profile, :not_nil

View file

@ -0,0 +1,41 @@
module Validation
module Rule
# URI validation rule
#
# This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/uri.rb
#
# It allows +nil+, so maybe add an additional {Rule::NotNil} rule.
class URI
# @param [Array<Symbol>] parts the parts that are required
def initialize(parts=%i(scheme host))
@required_parts = parts
end
# The error key for this rule
# @return [Symbol] error key
def error_key
:URI
end
# This rule has a +required_elements+ param
# @return [Hash] params
def params
{required_elements: @required_parts}
end
# Determines if value is a valid URI
def valid_value?(uri_string)
return true if uri_string.nil?
uri = URI(uri_string)
@required_parts.each do |part|
return false if uri.public_send(part).nil? || uri.public_send(part).empty?
end
true
rescue ::URI::InvalidURIError
false
end
end
end
end

View file

@ -6,6 +6,10 @@ describe Validation::Rule::Birthday do
}.to raise_error ArgumentError
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:birthday)
end
context "validation" do
it "validates a date object" do
validator = Validation::Validator.new(OpenStruct.new(birthday: Date.new))

View file

@ -6,6 +6,10 @@ describe Validation::Rule::Boolean do
}.to raise_error ArgumentError
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:boolean)
end
context "validation" do
context "strings" do
it "validates boolean-esque strings" do

View file

@ -6,6 +6,10 @@ describe Validation::Rule::DiasporaId do
}.to raise_error ArgumentError
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:diaspora_id)
end
context "validation" do
it "validates a normal diaspora id" do
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@example.com"))

View file

@ -6,6 +6,10 @@ describe Validation::Rule::Guid do
}.to raise_error ArgumentError
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:guid)
end
context "validation" do
it "validates a string at least 16 chars long, consisting of [0-9a-f] (diaspora)" do
validator = Validation::Validator.new(OpenStruct.new(guid: "abcdef0123456789"))

View file

@ -6,6 +6,10 @@ describe Validation::Rule::NotNil do
}.to raise_error ArgumentError
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:not_nil)
end
context "validation" do
it "validates a string " do
validator = Validation::Validator.new(OpenStruct.new(not_nil: "abcd"))

View file

@ -1,4 +1,4 @@
describe Validation::Rule::Guid do
describe Validation::Rule::PublicKey do
it "will not accept parameters" do
validator = Validation::Validator.new({})
expect {
@ -6,6 +6,10 @@ describe Validation::Rule::Guid do
}.to raise_error ArgumentError
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:public_key)
end
context "validation" do
["PUBLIC KEY", "RSA PUBLIC KEY"].each do |key_type|
context key_type do

View file

@ -15,6 +15,10 @@ describe Validation::Rule::TagCount do
end
end
it "has an error key" do
expect(described_class.new(maximum: 5).error_key).to eq(:tag_count)
end
context "validation" do
let(:tag_str) { "#i #love #tags" }

View file

@ -0,0 +1,61 @@
describe Validation::Rule::URI do
it "has default params" do
expect(described_class.new.params).to eq(required_elements: %i(scheme host))
end
it "has an error key" do
expect(described_class.new.error_key).to eq(:URI)
end
context "validation" do
it "validates a valid uri" do
validator = Validation::Validator.new(OpenStruct.new(uri: "http://example.com"))
validator.rule(:uri, :URI)
expect(validator).to be_valid
expect(validator.errors).to be_empty
end
it "validates nil" do
validator = Validation::Validator.new(OpenStruct.new(uri: nil))
validator.rule(:uri, :URI)
expect(validator).to be_valid
expect(validator.errors).to be_empty
end
it "fails when given an invalid uri" do
validator = Validation::Validator.new(OpenStruct.new(uri: "foo:/%urim"))
validator.rule(:uri, :URI)
expect(validator).not_to be_valid
expect(validator.errors).to include(:uri)
end
context "part validation" do
it "fails to validate when given a uri without a host" do
validator = Validation::Validator.new(OpenStruct.new(uri: "http:foo@"))
validator.rule(:uri, :URI)
expect(validator).not_to be_valid
expect(validator.errors).to include(:uri)
end
it "fails to validate when given a uri without a scheme" do
validator = Validation::Validator.new(OpenStruct.new(uri: "example.com"))
validator.rule(:uri, :URI)
expect(validator).not_to be_valid
expect(validator.errors).to include(:uri)
end
it "fails to validate when given a uri without a path" do
validator = Validation::Validator.new(OpenStruct.new(uri: "http://example.com"))
validator.rule(:uri, URI: %i(host path))
expect(validator).not_to be_valid
expect(validator.errors).to include(:uri)
end
end
end
end

View file

@ -125,6 +125,15 @@ shared_examples "a name validator" do
end
shared_examples "a url validator without path" do
it "must not be nil or empty" do
[nil, ""].each do |val|
validator = described_class.new(entity_stub(entity, property, val))
expect(validator).not_to be_valid
expect(validator.errors).to include(property)
end
end
it "fails for url with special chars" do
validator = described_class.new(entity_stub(entity, property, "https://asdf$%.com"))