Skip to content

Commit

Permalink
Split context into context and state.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Oulevay committed Nov 25, 2014
1 parent 18a5052 commit 23237f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 46 deletions.
30 changes: 4 additions & 26 deletions lib/errapi/validation_context.rb
Original file line number Diff line number Diff line change
@@ -1,37 +1,15 @@
module Errapi

class ValidationContext
attr_reader :errors
attr_reader :state

def initialize
@errors = []
def initialize state
@state = state
end

def add_error options = {}, &block
@errors << ValidationError.new(options, &block)
@state.add_error options, &block
self
end

def validate value, options = {}
if yield value, options
true
else
add_error options[:error]
false
end
end

def error? criteria = {}
return !@errors.empty? if criteria.empty?
@errors.any?{ |err| err.matches? criteria }
end

def valid?
!error?
end

def clear
@errors.clear
end
end
end
37 changes: 37 additions & 0 deletions lib/errapi/validation_state.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Errapi

class ValidationState
attr_reader :errors

def initialize
@errors = []
end

def add_error options = {}, &block
@errors << ValidationError.new(options, &block)
self
end

def validate value, options = {}
if yield value, options
true
else
add_error options[:error]
false
end
end

def error? criteria = {}
return !@errors.empty? if criteria.empty?
@errors.any?{ |err| err.matches? criteria }
end

def valid?
!error?
end

def clear
@errors.clear
end
end
end
41 changes: 21 additions & 20 deletions spec/example_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@

RSpec.describe 'errapi' do

let(:context){ Errapi::ValidationContext.new }
let(:state){ Errapi::ValidationState.new }
let(:context){ Errapi::ValidationContext.new state }

it "should collect and find errors" do

context.add_error message: 'foo'
context.add_error message: 'bar', code: 'auth.failed'
context.add_error{ |err| err.set message: 'baz', code: 'json.invalid' }
state.add_error message: 'foo'
state.add_error message: 'bar', code: 'auth.failed'
state.add_error{ |err| err.set message: 'baz', code: 'json.invalid' }

%w(foo bar baz).each do |message|
expect(context.error?(message: message)).to be(true)
expect(state.error?(message: message)).to be(true)
end

[ /fo/, /ba/ ].each do |regexp|
expect(context.error?(message: regexp)).to be(true)
expect(state.error?(message: regexp)).to be(true)
end

expect(context.error?(message: 'qux')).to be(false)
expect(context.error?(message: /qux/)).to be(false)
expect(state.error?(message: 'qux')).to be(false)
expect(state.error?(message: /qux/)).to be(false)

%w(auth.failed json.invalid).each do |code|
expect(context.error?(code: code)).to be(true)
expect(state.error?(code: code)).to be(true)
end

[ /^auth\./, /invalid/ ].each do |regexp|
expect(context.error?(code: regexp)).to be(true)
expect(state.error?(code: regexp)).to be(true)
end

expect(context.error?(code: 'broken')).to be(false)
expect(context.error?(code: /broke/)).to be(false)
expect(state.error?(code: 'broken')).to be(false)
expect(state.error?(code: /broke/)).to be(false)
end

it "should provide a model extension to validate objects" do
Expand All @@ -47,14 +48,14 @@

o = klass.new
o.validate context
expect(context.error?).to be(true)
expect(context.error?(message: /cannot be null or empty/)).to be(true)
expect(context.errors).to have(1).item
expect(state.error?).to be(true)
expect(state.error?(message: /cannot be null or empty/)).to be(true)
expect(state.errors).to have(1).item

context.clear
state.clear
o.name = 'foo'
o.validate context
expect(context.error?).to be(false)
expect(state.error?).to be(false)
end

it "should validate parsed JSON" do
Expand Down Expand Up @@ -82,8 +83,8 @@

validations.validate h, context

expect(context.error?).to be(true)
expect(context.error?(message: /cannot be null or empty/)).to be(true)
expect(context.errors).to have(3).items
expect(state.error?).to be(true)
expect(state.error?(message: /cannot be null or empty/)).to be(true)
expect(state.errors).to have(3).items
end
end

0 comments on commit 23237f4

Please sign in to comment.