data_validator 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/data_validator/version.rb +1 -1
- data/lib/data_validator.rb +2 -1
- data/lib/validations/acceptance.rb +1 -1
- data/lib/validations/base.rb +4 -4
- data/lib/validations/format.rb +1 -1
- data/lib/validations/inclusion.rb +2 -2
- data/{locale → locales}/en.yml +1 -1
- data/spec/locales/jp.yml +8 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/validations/base_spec.rb +8 -0
- metadata +5 -3
data/lib/data_validator.rb
CHANGED
@@ -45,7 +45,8 @@ module DataValidator
|
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
|
-
I18n.load_path += Dir[File.join(File.dirname(__FILE__), '..', '
|
48
|
+
I18n.load_path += Dir[File.join(File.dirname(__FILE__), '..', 'locales', '*.{rb,yml}')]
|
49
|
+
|
49
50
|
Dir[File.dirname(__FILE__) + "/validations/*.rb"].sort.each do |path|
|
50
51
|
filename = File.basename(path)
|
51
52
|
require "validations/#{filename}"
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module DataValidator
|
4
4
|
class AcceptanceValidator < BaseValidator
|
5
5
|
def initialize(name, value, options, errors)
|
6
|
-
raise ArgumentError,
|
6
|
+
raise ArgumentError, 'Options must define' unless options
|
7
7
|
case options
|
8
8
|
when Hash
|
9
9
|
options_base = options
|
data/lib/validations/base.rb
CHANGED
@@ -7,7 +7,7 @@ module DataValidator
|
|
7
7
|
attr_accessor :name, :value, :options, :errors
|
8
8
|
|
9
9
|
def initialize(name, value, options, errors)
|
10
|
-
raise ArgumentError,
|
10
|
+
raise ArgumentError, 'Options must define' if options.blank?
|
11
11
|
|
12
12
|
case options
|
13
13
|
when Hash
|
@@ -25,14 +25,14 @@ module DataValidator
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def validate
|
28
|
-
raise ArgumentError,
|
28
|
+
raise ArgumentError, 'Validate method is necessary'
|
29
29
|
end
|
30
30
|
|
31
31
|
def add_error(error_message_key, message_args = {})
|
32
|
-
error_subject_key = "datavalidator.
|
32
|
+
error_subject_key = "datavalidator.errors.subjects.#{name}"
|
33
33
|
error_subject = ''
|
34
34
|
begin
|
35
|
-
error_subject = I18n.t! "datavalidator.
|
35
|
+
error_subject = I18n.t! "datavalidator.errors.subjects.#{name}"
|
36
36
|
rescue
|
37
37
|
error_subject = name.to_s
|
38
38
|
end
|
data/lib/validations/format.rb
CHANGED
@@ -4,7 +4,7 @@ module DataValidator
|
|
4
4
|
class FormatValidator < BaseValidator
|
5
5
|
def check_validity!
|
6
6
|
unless options.include?(:with) ^ options.include?(:without) # ^ == xor, or "exclusive or"
|
7
|
-
raise ArgumentError,
|
7
|
+
raise ArgumentError, 'Either :with or :without must be supplied (but not both)'
|
8
8
|
end
|
9
9
|
check_options_validity(options, :with)
|
10
10
|
check_options_validity(options, :without)
|
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
module DataValidator
|
4
4
|
class InclusionValidator < BaseValidator
|
5
|
-
ERROR_MESSAGE =
|
6
|
-
|
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
7
|
|
8
8
|
def check_validity!
|
9
9
|
unless [:include?, :call].any?{ |method| options[:in].respond_to?(method) }
|
data/{locale → locales}/en.yml
RENAMED
data/spec/locales/jp.yml
ADDED
data/spec/spec_helper.rb
CHANGED
@@ -39,6 +39,14 @@ describe DataValidator::BaseValidator do
|
|
39
39
|
@obj.errors.should eql({name: ["name can't be blank", "name is invalid"]})
|
40
40
|
end
|
41
41
|
end
|
42
|
+
context 'non default locale error message' do
|
43
|
+
it do
|
44
|
+
I18n.locale = :jp
|
45
|
+
@obj.add_error(:blank)
|
46
|
+
@obj.errors.should eql({name: ["名前 は必須項目です"]})
|
47
|
+
end
|
48
|
+
after { I18n.locale = :en }
|
49
|
+
end
|
42
50
|
end
|
43
51
|
end
|
44
52
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -69,8 +69,9 @@ files:
|
|
69
69
|
- lib/validations/length.rb
|
70
70
|
- lib/validations/numericality.rb
|
71
71
|
- lib/validations/presence.rb
|
72
|
-
-
|
72
|
+
- locales/en.yml
|
73
73
|
- spec/data_validator_spec.rb
|
74
|
+
- spec/locales/jp.yml
|
74
75
|
- spec/spec_helper.rb
|
75
76
|
- spec/validations/acceptance_spec.rb
|
76
77
|
- spec/validations/base_spec.rb
|
@@ -106,6 +107,7 @@ specification_version: 3
|
|
106
107
|
summary: DataValidator is validation anywhere
|
107
108
|
test_files:
|
108
109
|
- spec/data_validator_spec.rb
|
110
|
+
- spec/locales/jp.yml
|
109
111
|
- spec/spec_helper.rb
|
110
112
|
- spec/validations/acceptance_spec.rb
|
111
113
|
- spec/validations/base_spec.rb
|