Skip to content

Commit

Permalink
Merge a5cfca1 into 70c098b
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew342 committed Aug 9, 2015
2 parents 70c098b + a5cfca1 commit 6857049
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ the base collections will now be called "Repositories" or "Repo's" for short. T
## 0.9.3
[0.9.3 Update Blog Post](http://blog.voltframework.com/post/121128931859/0-9-3-stuff-you-asked-for)
[Upgrade Guide](https://github.com/voltrb/volt/blob/master/docs/UPGRADE_GUIDE.md)

### Added
- Added validations block for conditional validation runs
- you can now set the NO_FORKING=true ENV to prevent using the forking server in development.
Expand Down
1 change: 1 addition & 0 deletions lib/volt/models/validations/validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'volt/models/validators/format_validator'
require 'volt/models/validators/email_validator'
require 'volt/models/validators/length_validator'
require 'volt/models/validators/list_validator'
require 'volt/models/validators/numericality_validator'
require 'volt/models/validators/phone_number_validator'
require 'volt/models/validators/presence_validator'
Expand Down
25 changes: 25 additions & 0 deletions lib/volt/models/validators/list_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Volt
class ListValidator
def self.validate(model, field_name, args)
errors = {}
value = model.get(field_name)

if args.is_a?(Array)
list = args
message = nil
elsif args.is_a?(Hash)
list = args[:list]
message = args[:message]
fail 'A list to match against must be specified' unless list.is_a?(Array)
else
fail 'The arguments to list validator must be an array or a hash'
end

unless list.include?(value)
errors[field_name] = [message || "must be one of #{list.join(', ')}"]
end

errors
end
end
end
57 changes: 57 additions & 0 deletions spec/models/validators/list_validator_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'

describe Volt::ListValidator do
subject { Volt::ListValidator.validate(*use_params) }
let(:use_params) { [model, field_name, options] }

let(:model) { Volt::Model.new name: name }
let(:field_name) { :name }
let(:name) { 'John' }

describe '.validate' do
describe 'when options is an array' do
let(:options) { %w(John Susie Mary) }

describe 'when name is "John"' do
let(:name) { 'John' }
it { expect(subject).to eq({}) }
end

describe 'when name is "Bill"' do
let(:name) { 'Bill' }
it do
expect(subject).to eq(name: ['must be one of John, Susie, Mary'])
end
end
end

describe 'when options is a Hash' do
let(:options) do
{ list: %w(John Susie Mary), message: 'Choose one from the list.' }
end

describe 'when name is "John"' do
let(:name) { 'John' }
it { expect(subject).to eq({}) }
end

describe 'when name is "Bill"' do
let(:name) { 'Bill' }
it do
expect(subject).to eq(name: ['Choose one from the list.'])
end
end
end

describe 'when options not a Fixnum or a Hash' do
let(:options) { 'string' }

it 'raises an exception' do
expect { subject }.to raise_error(
RuntimeError,
'The arguments to list validator must be an array or a hash'
)
end
end
end
end

0 comments on commit 6857049

Please sign in to comment.