data_validator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx-19mode
7
+
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in data_validator.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rspec'
8
+ gem 'rspec-core'
9
+ gem 'rake'
10
+ end
11
+
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012- HARUYAMA Makoto - http://github.com/SpringMT
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # DataValidator [![Build Status](https://travis-ci.org/SpringMT/data_validator.png)](https://travis-ci.org/SpringMT/data_validator)
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ gem 'data_validator'
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install data_validator
16
+
17
+ ## Usage
18
+
19
+ require 'data_validator'
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
28
+
29
+ ## Copyright
30
+
31
+ Copyright (c) 2012- HARUAYMA Makoto (SpringMT)
32
+
33
+ ## License
34
+ MIT (see MIT-LICENSE)
35
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => :test
4
+
5
+ task :test do
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:test) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+ end
12
+
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'data_validator/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'data_validator'
7
+ s.version = DataValidator::VERSION
8
+ s.authors = ['Spring_MT']
9
+ s.email = ['today.is.sky.blue.sky@gmail.com']
10
+ s.homepage = 'https://github.com/SpringMT/data_validator'
11
+ s.summary = %q{DataValidator is validation anywhere}
12
+
13
+ s.rubyforge_project = 'data_validator'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'active_support', '~> 3.0.0'
21
+ s.add_dependency 'i18n', '~> 0.6.1'
22
+
23
+ s.description = <<description
24
+ DataValidator has almost active_recorde validation methods.
25
+ description
26
+
27
+ end
@@ -0,0 +1,3 @@
1
+ module DataValidator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'data_validator/version'
4
+ require 'active_support/core_ext'
5
+ require 'validations/base'
6
+
7
+ module DataValidator
8
+ class Validator
9
+ attr_accessor :errors
10
+
11
+ def initialize(params, rules)
12
+ @params = params
13
+ @rules = rules
14
+ @errors = {}
15
+ end
16
+
17
+ def valid?
18
+ @rules.each_pair do |key, rule|
19
+ is_allow_nil = rule.delete :allow_nil
20
+ is_allow_blank = rule.delete :allow_blank
21
+ next if @params[key].nil? && is_allow_nil
22
+ next if @params[key].blank? && is_allow_blank
23
+
24
+ rule.each_pair do |validator, options|
25
+ klass = "#{validator.capitalize}Validator"
26
+ constant = Object
27
+ constant = constant.const_get "DataValidator"
28
+ validation = constant.const_get(klass).new(key, @params[key], options, errors)
29
+ validation.check_validity!
30
+ validation.validate
31
+ end
32
+ end
33
+
34
+ if error?
35
+ return false
36
+ else
37
+ return true
38
+ end
39
+ end
40
+
41
+ def error?
42
+ errors.present?
43
+ end
44
+
45
+ end
46
+ end
47
+
48
+ Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
49
+ filename = File.basename(path)
50
+ require "validations/#{filename}"
51
+ end
52
+
53
+
@@ -0,0 +1,25 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class AcceptanceValidator < BaseValidator
5
+ def initialize(name, value, options, errors)
6
+ raise ArgumentError, "options must define" unless options
7
+ case options
8
+ when Hash
9
+ options_base = options
10
+ else
11
+ options_base = {}
12
+ end
13
+ super(name, value, options_base.reverse_merge(:allow_nil => true, :accept => "1"), errors)
14
+ end
15
+
16
+ def validate
17
+ unless value == options[:accept]
18
+ error_add :accepted
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+
25
+
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'i18n'
4
+
5
+ module DataValidator
6
+ class BaseValidator
7
+ attr_accessor :name, :value, :options, :errors
8
+
9
+ def initialize(name, value, options, errors)
10
+ raise ArgumentError, "Options must define" if options.blank?
11
+
12
+ case options
13
+ when Hash
14
+ @options = options
15
+ else
16
+ @options = {}
17
+ end
18
+
19
+ @name = name
20
+ @value = value
21
+ @errors = errors
22
+ end
23
+
24
+ def check_validity!
25
+ end
26
+
27
+ def validate
28
+ raise ArgumentError, "Validate method is necessary"
29
+ end
30
+
31
+ def error_add(error_message_key, message_args = {})
32
+ if errors.key? name
33
+ errors[name] << I18n.t("errors.messages.#{error_message_key.to_s}", message_args)
34
+ else
35
+ errors[name] = [I18n.t("errors.messages.#{error_message_key.to_s}", message_args)]
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class ConfirmationValidator < BaseValidator
5
+ def validate
6
+ if value != options[:criterion]
7
+ error_add :confirmation
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class FormatValidator < BaseValidator
5
+ def check_validity!
6
+ unless options.include?(:with) ^ options.include?(:without) # ^ == xor, or "exclusive or"
7
+ raise ArgumentError, "Either :with or :without must be supplied (but not both)"
8
+ end
9
+ check_options_validity(options, :with)
10
+ check_options_validity(options, :without)
11
+ end
12
+
13
+ def validate
14
+ if regexp = options[:with]
15
+ error_add :invalid if value.to_s !~ regexp
16
+ elsif regexp = options[:without]
17
+ error_add :invalid if value.to_s =~ regexp
18
+ end
19
+ end
20
+
21
+ private
22
+ def check_options_validity(options, name)
23
+ option = options[name]
24
+ if option && !option.is_a?(Regexp) && !option.respond_to?(:call)
25
+ raise ArgumentError, "A regular expression or a proc or lambda must be supplied as :#{name}"
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class InclusionValidator < BaseValidator
5
+ ERROR_MESSAGE = "An object with the method #include? or a proc or lambda is required, " <<
6
+ "and must be supplied as the :in option of the configuration hash"
7
+
8
+ def check_validity!
9
+ unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
10
+ raise ArgumentError, ERROR_MESSAGE
11
+ end
12
+ end
13
+
14
+ def validate
15
+ delimiter = options[:in]
16
+ unless delimiter.send(inclusion_method(delimiter), value)
17
+ error_add :inclusion, value: value
18
+ end
19
+ end
20
+
21
+ private
22
+ # In Ruby 1.9 <tt>Range#include?</tt> on non-numeric ranges checks all possible values in the
23
+ # range for equality, so it may be slow for large ranges. The new <tt>Range#cover?</tt>
24
+ # uses the previous logic of comparing a value with the range endpoints.
25
+ def inclusion_method(enumerable)
26
+ enumerable.is_a?(Range) ? :cover? : :include?
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class LengthValidator < BaseValidator
5
+ MESSAGES = { :is => :wrong_length, :minimum => :too_short, :maximum => :too_long }.freeze
6
+ CHECKS = { :is => :==, :minimum => :>=, :maximum => :<= }.freeze
7
+
8
+ def check_validity!
9
+ if range = (options.delete(:in) || options.delete(:within))
10
+ raise ArgumentError, ":in and :within must be a Range" unless range.is_a?(Range)
11
+ options[:minimum], options[:maximum] = range.begin, range.end
12
+ options[:maximum] -= 1 if range.exclude_end?
13
+ end
14
+
15
+ keys = CHECKS.keys & options.keys
16
+ if keys.empty?
17
+ raise ArgumentError, 'Range unspecified. Specify the :in, :within, :maximum, :minimum, or :is option.'
18
+ end
19
+
20
+ keys.each do |key|
21
+ option_value = options[key]
22
+ unless option_value.is_a?(Integer) && option_value >= 0
23
+ raise ArgumentError, ":#{key} must be a nonnegative Integer"
24
+ end
25
+ end
26
+ end
27
+
28
+ def validate
29
+ tokenized_value = tokenize value
30
+ value_length = tokenized_value.respond_to?(:length) ? tokenized_value.length : tokenized_value.to_s.length
31
+
32
+ CHECKS.each do |key, validity_check|
33
+ next unless check_value = options[key]
34
+ next if value_length.send(validity_check, check_value)
35
+ error_add MESSAGES[key], count: check_value
36
+ end
37
+ end
38
+
39
+ private
40
+ def tokenize(value)
41
+ if value.kind_of?(String)
42
+ if options[:tokenizer]
43
+ options[:tokenizer].call(value)
44
+ elsif !value.encoding_aware?
45
+ value.mb_chars
46
+ end
47
+ end || value
48
+ end
49
+
50
+ end
51
+ end
52
+
53
+
@@ -0,0 +1,65 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class NumericalityValidator < BaseValidator
5
+ CHECKS = { :greater_than => :>, :greater_than_or_equal_to => :>=,
6
+ :equal_to => :==, :less_than => :<, :less_than_or_equal_to => :<=,
7
+ :odd => :odd?, :even => :even? }.freeze
8
+
9
+ def check_validity!
10
+ keys = CHECKS.keys - [:odd, :even]
11
+ options.slice(*keys).each do |option, option_value|
12
+ next if option_value.is_a?(Numeric) || option_value.is_a?(Proc)
13
+ raise ArgumentError, ":#{option} must be a number, a symbol or a proc"
14
+ end
15
+ end
16
+
17
+ def validate
18
+ unless checked_value = parse_raw_value_as_a_number(value)
19
+ error_add :not_a_number
20
+ return
21
+ end
22
+
23
+ if options[:only_integer]
24
+ unless checked_value = parse_raw_value_as_an_integer(value)
25
+ error_add :not_an_integer
26
+ return
27
+ end
28
+ end
29
+
30
+ options.slice(*CHECKS.keys).each do |option, option_value|
31
+ case option
32
+ when :odd, :even
33
+ unless checked_value.to_i.send(CHECKS[option])
34
+ error_add option
35
+ end
36
+ else
37
+ option_value = option_value.call(record) if option_value.is_a?(Proc)
38
+ unless checked_value.send(CHECKS[option], option_value)
39
+ error_add option, count: option_value
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ protected
46
+ def parse_raw_value_as_a_number(value)
47
+ case value
48
+ when /\A0[xX]/
49
+ nil
50
+ else
51
+ begin
52
+ Kernel.Float(value)
53
+ rescue ArgumentError, TypeError
54
+ nil
55
+ end
56
+ end
57
+ end
58
+
59
+ def parse_raw_value_as_an_integer(value)
60
+ value.to_i if value.to_s =~ /\A[+-]?\d+\Z/
61
+ end
62
+
63
+ end
64
+ end
65
+
@@ -0,0 +1,12 @@
1
+ # encoding: UTF-8
2
+
3
+ module DataValidator
4
+ class PresenceValidator < BaseValidator
5
+ def validate
6
+ if options && (value.nil? || value.blank?)
7
+ error_add :blank
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.dirname(__FILE__) + '/spec_helper'
5
+
6
+ module DataValidator
7
+ class DummyValidator
8
+ attr_accessor :name, :value, :options, :errors
9
+ def initialize(name, value, options, errors)
10
+ @name = name; @value = value; @options = options; @errors = errors;
11
+ end
12
+ def check_validity!
13
+ end
14
+ def validate
15
+ unless options
16
+ errors[name] = 'dummy error'
17
+ end
18
+ options
19
+ end
20
+ end
21
+ end
22
+
23
+ describe DataValidator::Validator do
24
+ describe :valid? do
25
+ context 'allow_nil with nil params' do
26
+ subject { DataValidator::Validator.new({name: nil}, {name: {dummy: true, allow_nil: true}}).valid? }
27
+ it { should be_true }
28
+ end
29
+ context 'allow_nil with blank params' do
30
+ subject { DataValidator::Validator.new({name: ""}, {name: {dummy: false, allow_nil: true}}).valid? }
31
+ it {should be_false}
32
+ end
33
+ context 'allow_blank with blank params' do
34
+ subject { DataValidator::Validator.new({name: ""}, {name: {dummy: true, allow_blank: true}}).valid? }
35
+ it { should be_true }
36
+ end
37
+ context 'undefied validatior' do
38
+ it { expect { DataValidator::Validator.new({name: 'dummy'}, {name: {test: true}}).valid? }.to raise_error(NameError) }
39
+ end
40
+ context 'validation true' do
41
+ subject { DataValidator::Validator.new({name: 'dummy'}, {name: {dummy: true}}).valid? }
42
+ it { should be_true }
43
+ end
44
+ context 'validation false' do
45
+ let(:obj) { DataValidator::Validator.new({name: 'dummy'}, {name: {dummy: false}}) }
46
+ it { obj.valid?.should be_false }
47
+ it do
48
+ obj.valid?
49
+ obj.errors[:name].should be_eql 'dummy error'
50
+ end
51
+ end
52
+
53
+ end
54
+ end
55
+
56
+
@@ -0,0 +1,24 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ inclusion: "is not included in the list"
5
+ exclusion: "is reserved"
6
+ invalid: "is invalid"
7
+ confirmation: "doesn't match confirmation"
8
+ accepted: "must be accepted"
9
+ empty: "can't be empty"
10
+ blank: "can't be blank"
11
+ too_long: "is too long (maximum is %{count} characters)"
12
+ too_short: "is too short (minimum is %{count} characters)"
13
+ wrong_length: "is the wrong length (should be %{count} characters)"
14
+ taken: "has already been taken"
15
+ not_a_number: "is not a number"
16
+ not_an_integer: "is not an integer"
17
+ greater_than: "must be greater than %{count}"
18
+ greater_than_or_equal_to: "must be greater than or equal to %{count}"
19
+ equal_to: "must be equal to %{count}"
20
+ less_than: "must be less than %{count}"
21
+ less_than_or_equal_to: "must be less than or equal to %{count}"
22
+ odd: "must be odd"
23
+ even: "must be even"
24
+
@@ -0,0 +1,16 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ Bundler.setup(:default, :test)
6
+ Bundler.require(:default, :test)
7
+
8
+ require 'rspec'
9
+
10
+ $TESTING=true
11
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
12
+ require 'data_validator'
13
+
14
+ I18n.load_path += Dir[File.join(File.dirname(__FILE__), 'locale', '*.{rb,yml}')]
15
+
16
+
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::AcceptanceValidator do
7
+ context 'validation true' do
8
+ subject do
9
+ DataValidator::Validator.new(
10
+ {terms_of_service: '1'},
11
+ {terms_of_service: {acceptance: true}}
12
+ ).valid?
13
+ end
14
+ it { should be_true }
15
+ end
16
+ context 'validation true with different accept' do
17
+ subject do
18
+ DataValidator::Validator.new(
19
+ {terms_of_service: 'yes'},
20
+ {terms_of_service: {acceptance: {accept: 'yes'}}}
21
+ ).valid?
22
+ end
23
+ it { should be_true }
24
+ end
25
+ context 'validation false' do
26
+ before do
27
+ @obj = DataValidator::Validator.new(
28
+ {terms_of_service: '0'},
29
+ {terms_of_service: {acceptance: true}}
30
+ )
31
+ end
32
+ it { @obj.valid?.should be_false }
33
+ it do
34
+ @obj.valid?
35
+ @obj.errors.should eql({terms_of_service: ["must be accepted"]})
36
+ end
37
+ end
38
+ end
39
+
40
+
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::BaseValidator do
7
+ describe :initialize do
8
+ context 'options is blank' do
9
+ it { expect{ DataValidator::BaseValidator.new(:name, 'value', '', {}) }.to raise_error(ArgumentError) }
10
+ end
11
+ context 'options is true' do
12
+ subject { DataValidator::BaseValidator.new(:name, 'value', true, {}) }
13
+ it { subject.options.should be_eql({}) }
14
+ end
15
+ context 'options is hash' do
16
+ subject { DataValidator::BaseValidator.new(:name, 'value', {test: 'test'}, {}) }
17
+ it { subject.options.should be_eql({test: 'test'}) }
18
+ end
19
+ end
20
+
21
+ describe :validate do
22
+ context 'validate method is override' do
23
+ it { expect{ DataValidator::BaseValidator.new(:name, 'value', true, {}).validate }.to raise_error(ArgumentError) }
24
+ end
25
+ end
26
+
27
+ describe :error_add do
28
+ before { @obj = DataValidator::BaseValidator.new(:name, 'value', true, {}) }
29
+ context 'add an error first' do
30
+ it do
31
+ @obj.error_add(:blank)
32
+ @obj.errors.should eql({name: ["can't be blank"]})
33
+ end
34
+ end
35
+ context 'add an errors secondly' do
36
+ it do
37
+ @obj.error_add(:blank)
38
+ @obj.error_add(:invalid)
39
+ @obj.errors.should eql({name: ["can't be blank", "is invalid"]})
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::AcceptanceValidator do
7
+ context 'validation true' do
8
+ subject do
9
+ params = {password: "password", password_confirmation: "password"}
10
+ DataValidator::Validator.new(
11
+ params,
12
+ {
13
+ password: {confirmation: {criterion: params[:password_confirmation]}},
14
+ password_confirmation: {presence: true}
15
+ }
16
+ ).valid?
17
+ end
18
+ it { should be_true }
19
+ end
20
+ context 'validation false' do
21
+ before do
22
+ params = {password: "password", password_confirmation: "hoge"}
23
+ @obj = DataValidator::Validator.new(
24
+ params,
25
+ {
26
+ password: {confirmation: {criterion: params[:password_confirmation]}},
27
+ password_confirmation: {presence: true}
28
+ }
29
+ )
30
+ end
31
+ it { @obj.valid?.should be_false }
32
+ it do
33
+ @obj.valid?
34
+ @obj.errors.should eql({password: ["doesn't match confirmation"]})
35
+ end
36
+ end
37
+ end
38
+
39
+
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::FormatValidator do
7
+ context 'validation true' do
8
+ context 'with' do
9
+ subject do
10
+ DataValidator::Validator.new(
11
+ {name: "abc"},
12
+ {name: {format: {with: /\A[a-zA-Z]+\z/}}}
13
+ ).valid?
14
+ end
15
+ it { should be_true }
16
+ end
17
+ context 'without' do
18
+ subject do
19
+ DataValidator::Validator.new(
20
+ {name: "123"},
21
+ {name: {format: {without: /\A[a-zA-Z]+\z/}}}
22
+ ).valid?
23
+ end
24
+ it { should be_true }
25
+ end
26
+ end
27
+
28
+ context 'validation false' do
29
+ context 'with' do
30
+ before do
31
+ @obj = DataValidator::Validator.new(
32
+ {name: "123"},
33
+ {name: {format: {with: /\A[a-zA-Z]+\z/}}}
34
+ )
35
+ end
36
+ it { @obj.valid?.should be_false }
37
+ it do
38
+ @obj.valid?
39
+ @obj.errors.should eql({name: ["is invalid"]})
40
+ end
41
+ end
42
+ context 'without' do
43
+ before do
44
+ @obj = DataValidator::Validator.new(
45
+ {name: "abc"},
46
+ {name: {format: {without: /\A[a-zA-Z]+\z/}}}
47
+ )
48
+ end
49
+ it { @obj.valid?.should be_false }
50
+ it do
51
+ @obj.valid?
52
+ @obj.errors.should eql({name: ["is invalid"]})
53
+ end
54
+ end
55
+ end
56
+
57
+ context 'invalid optiosn' do
58
+ context 'with and without both' do
59
+ it do
60
+ expect do
61
+ DataValidator::Validator.new(
62
+ {name: "123"},
63
+ {name: {format: {with: /\d+/, without: /\A[a-zA-Z]+\z/}}}
64
+ ).valid?
65
+ end.to raise_error(ArgumentError)
66
+ end
67
+ end
68
+ context 'a regular expression nor proc' do
69
+ it do
70
+ expect do
71
+ DataValidator::Validator.new(
72
+ {name: "123"},
73
+ {name: {format: {with: "test"}}}
74
+ ).valid?
75
+ end.to raise_error(ArgumentError)
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::InclusionValidator do
7
+ context 'validation true' do
8
+ context 'in' do
9
+ subject do
10
+ DataValidator::Validator.new(
11
+ {name: 'name'},
12
+ {name: {inclusion: {in: ['name', 'bar']}}}
13
+ ).valid?
14
+ end
15
+ it { should be_true }
16
+ end
17
+ end
18
+
19
+ context 'validation false' do
20
+ before do
21
+ @obj = DataValidator::Validator.new(
22
+ {name: 'name'},
23
+ {name: {inclusion: {in: ['foo', 'bar']}}}
24
+ )
25
+ end
26
+ it {@obj.valid?.should be_false}
27
+ it do
28
+ @obj.valid?
29
+ @obj.errors.should eql({name: ["is not included in the list"]})
30
+ end
31
+ end
32
+
33
+ context 'invalid options' do
34
+ it do
35
+ expect do
36
+ DataValidator::Validator.new(
37
+ {name: "123"},
38
+ {name: {inclusion: {in: 123}}}
39
+ ).valid?
40
+ end.to raise_error(ArgumentError)
41
+ end
42
+ end
43
+ end
44
+
45
+
@@ -0,0 +1,122 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::LengthValidator do
7
+ context 'validation true' do
8
+ context 'is' do
9
+ subject do
10
+ DataValidator::Validator.new(
11
+ {name: 'name'},
12
+ {name: {length: {is: 4}}}
13
+ ).valid?
14
+ end
15
+ it { should be_true }
16
+ end
17
+ context 'is with token' do
18
+ subject do
19
+ DataValidator::Validator.new(
20
+ {name: 'i am test name'},
21
+ {name: {length: {is: 4, tokenizer: lambda { |str| str.scan(/\w+/) }}}}
22
+ ).valid?
23
+ end
24
+ it { should be_true }
25
+ end
26
+ context 'maximum and minimum' do
27
+ subject do
28
+ DataValidator::Validator.new(
29
+ {name: 'name'},
30
+ {name: {length: {maximum: 100, minimum: 2}}}
31
+ ).valid?
32
+ end
33
+ it { should be_true }
34
+ end
35
+ context 'in' do
36
+ subject do
37
+ DataValidator::Validator.new(
38
+ {name: 'name'},
39
+ {name: {length: {in: 3..7}}}
40
+ ).valid?
41
+ end
42
+ it { should be_true }
43
+ end
44
+ end
45
+
46
+ context 'validation false' do
47
+ context 'is' do
48
+ before do
49
+ @obj = DataValidator::Validator.new(
50
+ {name: 'name'},
51
+ {name: {length: {is: 5}}}
52
+ )
53
+ end
54
+ it { @obj.valid?.should be_false }
55
+ it do
56
+ @obj.valid?
57
+ @obj.errors.should eql({name: ["is the wrong length (should be 5 characters)"]})
58
+ end
59
+ end
60
+ context 'maximum' do
61
+ before do
62
+ @obj = DataValidator::Validator.new(
63
+ {name: 'name'},
64
+ {name: {length: {maximum: 2}}}
65
+ )
66
+ end
67
+ it { @obj.valid?.should be_false }
68
+ it do
69
+ @obj.valid?
70
+ @obj.errors.should eql({name: ["is too long (maximum is 2 characters)"]})
71
+ end
72
+ end
73
+ context 'minimum' do
74
+ before do
75
+ @obj = DataValidator::Validator.new(
76
+ {name: 'name'},
77
+ {name: {length: {minimum: 5}}}
78
+ )
79
+ end
80
+ it { @obj.valid?.should be_false }
81
+ it do
82
+ @obj.valid?
83
+ @obj.errors.should eql({name: ["is too short (minimum is 5 characters)"]})
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'invalid options' do
89
+ context 'not Range with in:' do
90
+ it do
91
+ expect do
92
+ DataValidator::Validator.new(
93
+ {name: 'name'},
94
+ {name: {length: {in: 123}}}
95
+ ).valid?
96
+ end.to raise_error(ArgumentError)
97
+ end
98
+ end
99
+ context 'unexist options' do
100
+ it do
101
+ expect do
102
+ DataValidator::Validator.new(
103
+ {name: 'name'},
104
+ {name: {length: {test: 123}}}
105
+ ).valid?
106
+ end.to raise_error(ArgumentError)
107
+ end
108
+ end
109
+ context 'negative Integer' do
110
+ it do
111
+ expect do
112
+ DataValidator::Validator.new(
113
+ {name: 'name'},
114
+ {name: {length: {minimum: -1}}}
115
+ ).valid?
116
+ end.to raise_error(ArgumentError)
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+
@@ -0,0 +1,140 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::LengthValidator do
7
+ shared_examples_for :validtion_true do |params, rule|
8
+ subject do
9
+ DataValidator::Validator.new(
10
+ params,
11
+ rule
12
+ ).valid?
13
+ end
14
+ it { should be_true }
15
+ end
16
+ shared_examples_for :validtion_false do |params, rule, name, message|
17
+ before do
18
+ @obj = DataValidator::Validator.new(
19
+ params,
20
+ rule
21
+ )
22
+ end
23
+ it { @obj.valid?.should be_false }
24
+ it do
25
+ @obj.valid?
26
+ @obj.errors.should eql({name => [message]})
27
+ end
28
+ end
29
+
30
+ context 'validtion true' do
31
+ context 'only numeric check' do
32
+ it_behaves_like :validtion_true, {num: "001"}, {num: {numericality: true}}
33
+ end
34
+ context 'greater_than' do
35
+ it_behaves_like :validtion_true, {num: "100.1"}, {num: {numericality: {greater_than: 100}}}
36
+ end
37
+ context 'greater_than_or_equal_to' do
38
+ it_behaves_like :validtion_true, {num: "100"}, {num: {numericality: {greater_than_or_equal_to: 100}}}
39
+ end
40
+ context 'equal_to' do
41
+ it_behaves_like :validtion_true, {num: "100"}, {num: {numericality: {equal_to: 100}}}
42
+ end
43
+ context 'less_than' do
44
+ it_behaves_like :validtion_true, {num: "99.9"}, {num: {numericality: {less_than: 100}}}
45
+ end
46
+ context 'less_than_or_equal_to' do
47
+ it_behaves_like :validtion_true, {num: "100"}, {num: {numericality: {less_than_or_equal_to: 100}}}
48
+ end
49
+ context 'odd' do
50
+ it_behaves_like :validtion_true, {num: "101"}, {num: {numericality: {odd: true}}}
51
+ end
52
+ context 'even' do
53
+ it_behaves_like :validtion_true, {num: "100"}, {num: {numericality: {even: true}}}
54
+ end
55
+ context 'only_integer' do
56
+ it_behaves_like :validtion_true, {num: "-1"}, {num: {numericality: {only_integer: true}}}
57
+ end
58
+ end
59
+
60
+ context 'validtion true' do
61
+ context 'only numeric check' do
62
+ it_behaves_like :validtion_false,
63
+ {num: "test"},
64
+ {num: {numericality: true}},
65
+ :num,
66
+ "is not a number"
67
+ end
68
+ context 'greater_than' do
69
+ it_behaves_like :validtion_false,
70
+ {num: "100"},
71
+ {num: {numericality: {greater_than: 100}}},
72
+ :num,
73
+ "must be greater than 100"
74
+ end
75
+ context 'greater_than_or_equal_to' do
76
+ it_behaves_like :validtion_false,
77
+ {num: "99.9"},
78
+ {num: {numericality: {greater_than_or_equal_to: 100}}},
79
+ :num,
80
+ "must be greater than or equal to 100"
81
+ end
82
+ context 'equal_to' do
83
+ it_behaves_like :validtion_false,
84
+ {num: "99"},
85
+ {num: {numericality: {equal_to: 100}}},
86
+ :num,
87
+ "must be equal to 100"
88
+ end
89
+ context 'less_than' do
90
+ it_behaves_like :validtion_false,
91
+ {num: "100"},
92
+ {num: {numericality: {less_than: 100}}},
93
+ :num,
94
+ "must be less than 100"
95
+ end
96
+ context 'less_than_or_equal_to' do
97
+ it_behaves_like :validtion_false,
98
+ {num: "100.1"},
99
+ {num: {numericality: {less_than_or_equal_to: 100}}},
100
+ :num,
101
+ "must be less than or equal to 100"
102
+ end
103
+ context 'odd' do
104
+ it_behaves_like :validtion_false,
105
+ {num: "100"},
106
+ {num: {numericality: {odd: true}}},
107
+ :num,
108
+ "must be odd"
109
+ end
110
+ context 'even' do
111
+ it_behaves_like :validtion_false,
112
+ {num: "101"},
113
+ {num: {numericality: {even: true}}},
114
+ :num,
115
+ "must be even"
116
+ end
117
+ context 'only_integer' do
118
+ it_behaves_like :validtion_false,
119
+ {num: "1.001"},
120
+ {num: {numericality: {only_integer: true}}},
121
+ :num,
122
+ "is not an integer"
123
+ end
124
+ end
125
+
126
+ context 'invalid options' do
127
+ context 'option_value is not Numeric' do
128
+ it do
129
+ expect do
130
+ DataValidator::Validator.new(
131
+ {num: "100"},
132
+ {num: {numericality: {greater_than: "aaaa"}}},
133
+ ).valid?
134
+ end.to raise_error(ArgumentError)
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ require File.join(File.dirname(__FILE__), '..' ,'spec_helper')
5
+
6
+ describe DataValidator::PresenceValidator do
7
+ context 'validtion true' do
8
+ subject do
9
+ DataValidator::Validator.new(
10
+ {name: 'name'},
11
+ {name: {presence: true}}
12
+ ).valid?
13
+ end
14
+ it { should be_true }
15
+ end
16
+ context 'validation false' do
17
+ before { @obj = DataValidator::Validator.new({name: ''}, {name: {presence: true}}) }
18
+ it { @obj.valid?.should be_false }
19
+ it do
20
+ @obj.valid?
21
+ @obj.errors.should eql({name: ["can't be blank"]})
22
+ end
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spring_MT
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: active_support
16
+ requirement: &70098909584760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70098909584760
25
+ - !ruby/object:Gem::Dependency
26
+ name: i18n
27
+ requirement: &70098909582200 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70098909582200
36
+ description: ! ' DataValidator has almost active_recorde validation methods.
37
+
38
+ '
39
+ email:
40
+ - today.is.sky.blue.sky@gmail.com
41
+ executables: []
42
+ extensions: []
43
+ extra_rdoc_files: []
44
+ files:
45
+ - .gitignore
46
+ - .travis.yml
47
+ - Gemfile
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - data_validator.gemspec
52
+ - lib/data_validator.rb
53
+ - lib/data_validator/version.rb
54
+ - lib/validations/acceptance.rb
55
+ - lib/validations/base.rb
56
+ - lib/validations/confirmation.rb
57
+ - lib/validations/format.rb
58
+ - lib/validations/inclusion.rb
59
+ - lib/validations/length.rb
60
+ - lib/validations/numericality.rb
61
+ - lib/validations/presence.rb
62
+ - spec/data_validator_spec.rb
63
+ - spec/locale/en.yml
64
+ - spec/spec_helper.rb
65
+ - spec/validations/acceptance_spec.rb
66
+ - spec/validations/base_spec.rb
67
+ - spec/validations/confirmation_spec.rb
68
+ - spec/validations/format_spec.rb
69
+ - spec/validations/inclusion_spec.rb
70
+ - spec/validations/length_validation_spec.rb
71
+ - spec/validations/numericality_spec.rb
72
+ - spec/validations/presence_validation_spec.rb
73
+ homepage: https://github.com/SpringMT/data_validator
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project: data_validator
93
+ rubygems_version: 1.8.11
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: DataValidator is validation anywhere
97
+ test_files:
98
+ - spec/data_validator_spec.rb
99
+ - spec/locale/en.yml
100
+ - spec/spec_helper.rb
101
+ - spec/validations/acceptance_spec.rb
102
+ - spec/validations/base_spec.rb
103
+ - spec/validations/confirmation_spec.rb
104
+ - spec/validations/format_spec.rb
105
+ - spec/validations/inclusion_spec.rb
106
+ - spec/validations/length_validation_spec.rb
107
+ - spec/validations/numericality_spec.rb
108
+ - spec/validations/presence_validation_spec.rb
109
+ has_rdoc: