-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3fe70af
commit 1c67081
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module Errapi | ||
VERSION = '0.1.0' | ||
end | ||
|
||
Dir[File.join File.dirname(__FILE__), File.basename(__FILE__, '.*'), '*.rb'].each{ |lib| require lib } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Errapi | ||
|
||
class ValidationContext | ||
attr_reader :errors | ||
|
||
def initialize | ||
@errors = [] | ||
end | ||
|
||
def add message, options = {} | ||
@errors << ValidationError.new(message, options) | ||
end | ||
|
||
def error? criteria = {} | ||
return !@errors.empty? if criteria.empty? | ||
|
||
@errors.any? do |err| | ||
string_matches?(err, criteria, :message) && | ||
value_matches?(err, criteria, :code) && | ||
string_matches?(err, criteria, :type) && | ||
string_matches?(err, criteria, :location) | ||
end | ||
end | ||
|
||
private | ||
|
||
def error_matches? err, criteria, attr, &block | ||
!criteria.key?(attr) || block.call(err.send(attr), criteria[attr]) | ||
end | ||
|
||
def value_matches? err, criteria, attr | ||
error_matches?(err, criteria, attr){ |err_value,criterion| err_value == criterion } | ||
end | ||
|
||
def string_matches? err, criteria, attr | ||
error_matches?(err, criteria, attr){ |err_value,criterion| criterion.kind_of?(Regexp) ? !err_value.match(criterion).nil? : err_value == criterion } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Errapi | ||
|
||
class ValidationError | ||
attr_accessor :message, :code, :type, :location | ||
|
||
def initialize message, options = {} | ||
@message = message | ||
@code = options[:code] | ||
@type = options[:type] | ||
@location = options[:location] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'helper' | ||
|
||
RSpec.describe Errapi::ValidationContext do | ||
|
||
it "should have an empty array of errors" do | ||
expect(subject.errors).to eq([]) | ||
end | ||
|
||
it "should indicate that there are no errors" do | ||
expect(subject.error?).to be(false) | ||
end | ||
|
||
describe "with errors" do | ||
before :each do | ||
subject.add 'foo' | ||
subject.add 'bar' | ||
subject.add 'baz' | ||
end | ||
|
||
it "should match an error by message" do | ||
expect(subject.error?(message: 'foo')).to be(true) | ||
expect(subject.error?(message: 'bar')).to be(true) | ||
expect(subject.error?(message: 'baz')).to be(true) | ||
expect(subject.error?(message: 'qux')).to be(false) | ||
end | ||
|
||
it "should match an error by message with a regexp" do | ||
expect(subject.error?(message: /foo/)).to be(true) | ||
expect(subject.error?(message: /ba/)).to be(true) | ||
expect(subject.error?(message: /q/)).to be(false) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'helper' | ||
|
||
RSpec.describe Errapi::ValidationError do | ||
let(:options){ {} } | ||
subject{ described_class.new 'foo', options } | ||
|
||
it "should have a message" do | ||
expect_error message: 'foo' | ||
end | ||
|
||
describe "with options" do | ||
let(:options){ { code: 100, type: 'json', location: '/pointer' } } | ||
|
||
it "should have a code, type and location" do | ||
expect_error options.merge(message: 'foo') | ||
end | ||
end | ||
|
||
def expect_error options = {} | ||
[ :message, :code, :type, :location ].each do |attr| | ||
expect(subject.send(attr)).to eq(options.key?(attr) ? options[attr] : nil) | ||
end | ||
end | ||
end |