add own URI validation rule that allows nil
This commit is contained in:
parent
d7a5e71ce6
commit
c1e700d560
12 changed files with 142 additions and 3 deletions
|
|
@ -1,6 +1,5 @@
|
||||||
require "validation"
|
require "validation"
|
||||||
require "validation/rule/regular_expression"
|
require "validation/rule/regular_expression"
|
||||||
require "validation/rule/uri"
|
|
||||||
|
|
||||||
# +valid+ gem namespace
|
# +valid+ gem namespace
|
||||||
module Validation
|
module Validation
|
||||||
|
|
@ -20,6 +19,7 @@ require "diaspora_federation/validators/rules/guid"
|
||||||
require "diaspora_federation/validators/rules/not_nil"
|
require "diaspora_federation/validators/rules/not_nil"
|
||||||
require "diaspora_federation/validators/rules/public_key"
|
require "diaspora_federation/validators/rules/public_key"
|
||||||
require "diaspora_federation/validators/rules/tag_count"
|
require "diaspora_federation/validators/rules/tag_count"
|
||||||
|
require "diaspora_federation/validators/rules/uri"
|
||||||
|
|
||||||
module DiasporaFederation
|
module DiasporaFederation
|
||||||
# Validators to perform basic sanity-checks on {DiasporaFederation::Entities federation entities}.
|
# Validators to perform basic sanity-checks on {DiasporaFederation::Entities federation entities}.
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ module DiasporaFederation
|
||||||
|
|
||||||
rule :diaspora_id, :diaspora_id
|
rule :diaspora_id, :diaspora_id
|
||||||
|
|
||||||
rule :url, :URI
|
rule :url, %i(not_nil URI)
|
||||||
|
|
||||||
rule :profile, :not_nil
|
rule :profile, :not_nil
|
||||||
|
|
||||||
|
|
|
||||||
41
lib/diaspora_federation/validators/rules/uri.rb
Normal file
41
lib/diaspora_federation/validators/rules/uri.rb
Normal 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
|
||||||
|
|
@ -6,6 +6,10 @@ describe Validation::Rule::Birthday do
|
||||||
}.to raise_error ArgumentError
|
}.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new.error_key).to eq(:birthday)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
it "validates a date object" do
|
it "validates a date object" do
|
||||||
validator = Validation::Validator.new(OpenStruct.new(birthday: Date.new))
|
validator = Validation::Validator.new(OpenStruct.new(birthday: Date.new))
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ describe Validation::Rule::Boolean do
|
||||||
}.to raise_error ArgumentError
|
}.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new.error_key).to eq(:boolean)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
context "strings" do
|
context "strings" do
|
||||||
it "validates boolean-esque strings" do
|
it "validates boolean-esque strings" do
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ describe Validation::Rule::DiasporaId do
|
||||||
}.to raise_error ArgumentError
|
}.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new.error_key).to eq(:diaspora_id)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
it "validates a normal diaspora id" do
|
it "validates a normal diaspora id" do
|
||||||
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@example.com"))
|
validator = Validation::Validator.new(OpenStruct.new(diaspora_id: "some_user@example.com"))
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ describe Validation::Rule::Guid do
|
||||||
}.to raise_error ArgumentError
|
}.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new.error_key).to eq(:guid)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
it "validates a string at least 16 chars long, consisting of [0-9a-f] (diaspora)" 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"))
|
validator = Validation::Validator.new(OpenStruct.new(guid: "abcdef0123456789"))
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,10 @@ describe Validation::Rule::NotNil do
|
||||||
}.to raise_error ArgumentError
|
}.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new.error_key).to eq(:not_nil)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
it "validates a string " do
|
it "validates a string " do
|
||||||
validator = Validation::Validator.new(OpenStruct.new(not_nil: "abcd"))
|
validator = Validation::Validator.new(OpenStruct.new(not_nil: "abcd"))
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
describe Validation::Rule::Guid do
|
describe Validation::Rule::PublicKey do
|
||||||
it "will not accept parameters" do
|
it "will not accept parameters" do
|
||||||
validator = Validation::Validator.new({})
|
validator = Validation::Validator.new({})
|
||||||
expect {
|
expect {
|
||||||
|
|
@ -6,6 +6,10 @@ describe Validation::Rule::Guid do
|
||||||
}.to raise_error ArgumentError
|
}.to raise_error ArgumentError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new.error_key).to eq(:public_key)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
["PUBLIC KEY", "RSA PUBLIC KEY"].each do |key_type|
|
["PUBLIC KEY", "RSA PUBLIC KEY"].each do |key_type|
|
||||||
context key_type do
|
context key_type do
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,10 @@ describe Validation::Rule::TagCount do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "has an error key" do
|
||||||
|
expect(described_class.new(maximum: 5).error_key).to eq(:tag_count)
|
||||||
|
end
|
||||||
|
|
||||||
context "validation" do
|
context "validation" do
|
||||||
let(:tag_str) { "#i #love #tags" }
|
let(:tag_str) { "#i #love #tags" }
|
||||||
|
|
||||||
|
|
|
||||||
61
spec/lib/diaspora_federation/validators/rules/uri_spec.rb
Normal file
61
spec/lib/diaspora_federation/validators/rules/uri_spec.rb
Normal 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
|
||||||
|
|
@ -125,6 +125,15 @@ shared_examples "a name validator" do
|
||||||
end
|
end
|
||||||
|
|
||||||
shared_examples "a url validator without path" do
|
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
|
it "fails for url with special chars" do
|
||||||
validator = described_class.new(entity_stub(entity, property, "https://asdf$%.com"))
|
validator = described_class.new(entity_stub(entity, property, "https://asdf$%.com"))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue