pure_validator 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +8 -0
  5. data/Gemfile.lock +47 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +139 -0
  8. data/Rakefile +1 -0
  9. data/bin/console +14 -0
  10. data/lib/pure_validator/args_validator.rb +107 -0
  11. data/lib/pure_validator/concern.rb +136 -0
  12. data/lib/pure_validator/core_extensions/class_attribute.rb +143 -0
  13. data/lib/pure_validator/core_extensions/humanize.rb +44 -0
  14. data/lib/pure_validator/errors.rb +23 -0
  15. data/lib/pure_validator/i18n.rb +7 -0
  16. data/lib/pure_validator/locales/en.yml +24 -0
  17. data/lib/pure_validator/locales/ru.yml +24 -0
  18. data/lib/pure_validator/validation_errors.rb +248 -0
  19. data/lib/pure_validator/validator.rb +150 -0
  20. data/lib/pure_validator/validators/email_validator.rb +48 -0
  21. data/lib/pure_validator/validators/exclusion_validator.rb +22 -0
  22. data/lib/pure_validator/validators/inclusion_validator.rb +27 -0
  23. data/lib/pure_validator/validators/length_validator.rb +32 -0
  24. data/lib/pure_validator/validators/not_nil_validator.rb +25 -0
  25. data/lib/pure_validator/validators/numericality_validator.rb +39 -0
  26. data/lib/pure_validator/validators/presence_validator.rb +26 -0
  27. data/lib/pure_validator/validators/regexp_validator.rb +21 -0
  28. data/lib/pure_validator/validators/url_validator.rb +25 -0
  29. data/lib/pure_validator/validators.rb +11 -0
  30. data/lib/pure_validator/version.rb +3 -0
  31. data/lib/pure_validator.rb +43 -0
  32. data/pure_validator.gemspec +26 -0
  33. data/spec/pure_validator/args_validator_spec.rb +169 -0
  34. data/spec/pure_validator/errors_spec.rb +10 -0
  35. data/spec/pure_validator/validation_errors_spec.rb +109 -0
  36. data/spec/pure_validator/validator_spec.rb +234 -0
  37. data/spec/pure_validator/validators/email_validator_spec.rb +35 -0
  38. data/spec/pure_validator/validators/exclusion_validator_spec.rb +23 -0
  39. data/spec/pure_validator/validators/inclusion_validator_spec.rb +23 -0
  40. data/spec/pure_validator/validators/length_validator_spec.rb +38 -0
  41. data/spec/pure_validator/validators/not_nil_validator_spec.rb +44 -0
  42. data/spec/pure_validator/validators/numericality_validator_spec.rb +49 -0
  43. data/spec/pure_validator/validators/presence_validator_spec.rb +38 -0
  44. data/spec/pure_validator/validators/regexp_validator_spec.rb +23 -0
  45. data/spec/pure_validator/validators/url_validator_spec.rb +35 -0
  46. data/spec/spec_helper.rb +21 -0
  47. metadata +175 -0
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::EmailValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if email is valid" do
6
+ errors = PureValidator::Validators::EmailValidator.validate('test@example.com', true)
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value is invalid" do
11
+ errors = PureValidator::Validators::EmailValidator.validate('test@asdffd', true)
12
+ errors.should == ["invalid email"]
13
+ end
14
+
15
+ context "false as email_flag" do
16
+ it "fails if email is valid" do
17
+ errors = PureValidator::Validators::EmailValidator.validate('test@example.com', false)
18
+ expect(errors).to eq(["can't be email"])
19
+ end
20
+
21
+ it "passes if email is invalid" do
22
+ errors = PureValidator::Validators::EmailValidator.validate('test@asdffd', false)
23
+ expect(errors).to eq([])
24
+ end
25
+ end
26
+ end
27
+
28
+ describe ".validate_options" do
29
+ it "should raise error if validation attributes are invalid" do
30
+ lambda do
31
+ PureValidator::Validators::EmailValidator.validate_options("asdf")
32
+ end.should raise_error("validation_rule should be a Boolean")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::ExclusionValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if value is valid" do
6
+ errors = PureValidator::Validators::ExclusionValidator.validate(:wrong_type, in: [:new, :old, :medium])
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value is invalid" do
11
+ errors = PureValidator::Validators::ExclusionValidator.validate(:new, in: [:new, :old, :medium])
12
+ errors.should == ["should not be included in [:new, :old, :medium]"]
13
+ end
14
+ end
15
+
16
+ describe ".validate_options" do
17
+ it "should raise error if validation attributes are invalid" do
18
+ lambda do
19
+ PureValidator::Validators::ExclusionValidator.validate_options(wrong_option: false, in: [])
20
+ end.should raise_error("validation_rule has unacceptable options [:wrong_option]")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::InclusionValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if value is valid" do
6
+ errors = PureValidator::Validators::InclusionValidator.validate(:old, in: [:new, :old, :medium])
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value is invalid" do
11
+ errors = PureValidator::Validators::InclusionValidator.validate(:wrong_type, in: [:new, :old, :medium])
12
+ errors.should == ["should be included in [:new, :old, :medium]"]
13
+ end
14
+ end
15
+
16
+ describe ".validate_options" do
17
+ it "should raise error if validation attributes are invalid" do
18
+ lambda do
19
+ PureValidator::Validators::InclusionValidator.validate_options(wrong_option: false, in: [])
20
+ end.should raise_error("validation options has unacceptable options [:wrong_option]")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::LengthValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if text has valid length" do
6
+ errors = PureValidator::Validators::LengthValidator.validate('home', max: 5, min: 3)
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value has invalid max length" do
11
+ errors = PureValidator::Validators::LengthValidator.validate('long title', max: 5)
12
+ errors.should == ["can not be more than 5"]
13
+ end
14
+
15
+ it "should return errors if value has invalid min length" do
16
+ errors = PureValidator::Validators::LengthValidator.validate('ya', min: 3)
17
+ errors.should == ["can not be less than 3"]
18
+ end
19
+
20
+ it "should return errors if value has invalid equal_to length" do
21
+ errors = PureValidator::Validators::LengthValidator.validate('ya', equal_to: 3)
22
+ errors.should == ["should be equal to 3"]
23
+ end
24
+
25
+ it "should return errors if value has invalid not_equal_to length" do
26
+ errors = PureValidator::Validators::LengthValidator.validate('yad', not_equal_to: 3)
27
+ errors.should == ["should not be equal to 3"]
28
+ end
29
+ end
30
+
31
+ describe ".validate_options" do
32
+ it "should raise error if validation attributes are invalid" do
33
+ lambda do
34
+ PureValidator::Validators::LengthValidator.validate_options(max: 5, wrong_attr: 3)
35
+ end.should raise_error("validation_rule has unacceptable options [:wrong_attr]")
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::NotNilValidator do
4
+ def validate(*args)
5
+ PureValidator::Validators::NotNilValidator.validate(*args)
6
+ end
7
+
8
+ def passes(v)
9
+ expect(v).to be_empty
10
+ end
11
+
12
+ def fails(v, errors)
13
+ expect(v).to eq(errors)
14
+ end
15
+
16
+ describe ".validate" do
17
+ it "should return empty errors if object is not nil" do
18
+ passes(validate('home', true))
19
+ end
20
+
21
+ it "should return errors if object is nil" do
22
+ fails(validate(nil, true), ["can not be nil"])
23
+ end
24
+
25
+ context "with positive presence flag" do
26
+ it "passes for nil" do
27
+ passes(validate(nil, false))
28
+ end
29
+
30
+ it "fails for non-nil" do
31
+ fails(validate(1, false), ["should be nil"])
32
+ end
33
+ end
34
+ end
35
+
36
+ describe ".validate_options" do
37
+ it "should raise error if validation attributes are invalid" do
38
+ lambda do
39
+ PureValidator::Validators::NotNilValidator.validate_options("asdf")
40
+ end.should raise_error("validation_rule should be a Boolean")
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::NumericalityValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if number is valid" do
6
+ errors = PureValidator::Validators::NumericalityValidator.validate(4, less_than: 5, greater_than: 3)
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value is less than needed" do
11
+ errors = PureValidator::Validators::NumericalityValidator.validate(5, greater_than: 5)
12
+ errors.should == ["should be greater than 5"]
13
+ end
14
+
15
+ it "should return errors if value is less than or equal to what needed" do
16
+ errors = PureValidator::Validators::NumericalityValidator.validate(4, greater_than_or_equal_to: 5)
17
+ errors.should == ["should be greater than or equal to 5"]
18
+ end
19
+
20
+ it "should return errors if value is greater than needed" do
21
+ errors = PureValidator::Validators::NumericalityValidator.validate(5, less_than: 5)
22
+ errors.should == ["should be less than 5"]
23
+ end
24
+
25
+ it "should return errors if value is greater than or equal to what needed" do
26
+ errors = PureValidator::Validators::NumericalityValidator.validate(6, less_than_or_equal_to: 5)
27
+ errors.should == ["should be less than or equal to 5"]
28
+ end
29
+
30
+ it "should return errors if value is not even" do
31
+ errors = PureValidator::Validators::NumericalityValidator.validate(7, even: true)
32
+ errors.should == ["should be even number"]
33
+ end
34
+
35
+ it "should return errors if value is not odd" do
36
+ errors = PureValidator::Validators::NumericalityValidator.validate(6, odd: true)
37
+ errors.should == ["should be odd number"]
38
+ end
39
+
40
+ end
41
+
42
+ describe ".validate_options" do
43
+ it "should raise error if validation attributes are invalid" do
44
+ lambda do
45
+ PureValidator::Validators::NumericalityValidator.validate_options(less_than: 5, wrong_attr: 3)
46
+ end.should raise_error("validation_rule has unacceptable options [:wrong_attr]")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::PresenceValidator do
4
+ def validate(*args)
5
+ PureValidator::Validators::PresenceValidator.validate(*args)
6
+ end
7
+
8
+ def passes(v)
9
+ expect(v).to be_empty
10
+ end
11
+
12
+ def fails(v, errors)
13
+ expect(v).to eq(errors)
14
+ end
15
+
16
+ describe ".validate" do
17
+ it "should return empty errors if text is not blank" do
18
+ passes(validate('home', true))
19
+ end
20
+
21
+ it "should return errors if text is not specified" do
22
+ fails(validate(" ", true), ["can not be blank"])
23
+ end
24
+
25
+ it "should return errors if text is not specified" do
26
+ fails(validate("1", false), ["should be blank"])
27
+ end
28
+ end
29
+
30
+ describe ".validate_options" do
31
+ it "should raise error if validation attributes are invalid" do
32
+ lambda do
33
+ PureValidator::Validators::PresenceValidator.validate_options("asdf")
34
+ end.should raise_error("validation_rule should be a Boolean")
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::RegexpValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if value is valid" do
6
+ errors = PureValidator::Validators::RegexpValidator.validate('#aaa', /#\w{3,6}/)
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value is invalid" do
11
+ errors = PureValidator::Validators::RegexpValidator.validate('asdf', /#\w{3,6}/)
12
+ errors.should == ["does not match defined format"]
13
+ end
14
+ end
15
+
16
+ describe ".validate_options" do
17
+ it "should raise error if validation attributes are invalid" do
18
+ lambda do
19
+ PureValidator::Validators::RegexpValidator.validate_options({})
20
+ end.should raise_error("validation_rule should be a String or Regexp")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe PureValidator::Validators::UrlValidator do
4
+ describe ".validate" do
5
+ it "should return empty errors if email is valid" do
6
+ errors = PureValidator::Validators::UrlValidator.validate('example-asdf.com', true)
7
+ errors.should be_empty
8
+ end
9
+
10
+ it "should return errors if value is invalid" do
11
+ errors = PureValidator::Validators::UrlValidator.validate(':123asdffd.com', true)
12
+ errors.should == ["invalid url"]
13
+ end
14
+
15
+ context "false as url_flag" do
16
+ it "fails if url is valid" do
17
+ errors = PureValidator::Validators::UrlValidator.validate('example-asdf.com', false)
18
+ expect(errors).to eq(["can not be a url"])
19
+ end
20
+
21
+ it "passes if url is invalid" do
22
+ errors = PureValidator::Validators::UrlValidator.validate(':123asdffd.com', false)
23
+ expect(errors).to eq([])
24
+ end
25
+ end
26
+ end
27
+
28
+ describe ".validate_options" do
29
+ it "should raise error if validation attributes are invalid" do
30
+ lambda do
31
+ PureValidator::Validators::UrlValidator.validate_options("asdf")
32
+ end.should raise_error("validation_rule should be a Boolean")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require "byebug"
5
+ require 'simplecov'
6
+ SimpleCov.start do
7
+ add_filter "/spec/"
8
+ add_filter "/.direnv/"
9
+ add_filter "/core_extensions/class_attribute" # copy from ActiveSupport
10
+ add_filter "/core_extensions/humanize" # copy from ActiveSupport
11
+ add_filter "/pure_validator/concern" # copy from ActiveSupport
12
+ end
13
+ if ENV['CI']=='true'
14
+ require 'codecov'
15
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
16
+ end
17
+ require 'pure_validator'
18
+
19
+ RSpec.configure do |config|
20
+ config.color_enabled = true
21
+ end
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pure_validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Albert Gazizov
8
+ - Roman Heinrich
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-01-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.3'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.3'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: codecov
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: byebug
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: Object validation library
85
+ email:
86
+ - deeper4k@gmail.com
87
+ executables:
88
+ - console
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - lib/pure_validator.rb
101
+ - lib/pure_validator/args_validator.rb
102
+ - lib/pure_validator/concern.rb
103
+ - lib/pure_validator/core_extensions/class_attribute.rb
104
+ - lib/pure_validator/core_extensions/humanize.rb
105
+ - lib/pure_validator/errors.rb
106
+ - lib/pure_validator/i18n.rb
107
+ - lib/pure_validator/locales/en.yml
108
+ - lib/pure_validator/locales/ru.yml
109
+ - lib/pure_validator/validation_errors.rb
110
+ - lib/pure_validator/validator.rb
111
+ - lib/pure_validator/validators.rb
112
+ - lib/pure_validator/validators/email_validator.rb
113
+ - lib/pure_validator/validators/exclusion_validator.rb
114
+ - lib/pure_validator/validators/inclusion_validator.rb
115
+ - lib/pure_validator/validators/length_validator.rb
116
+ - lib/pure_validator/validators/not_nil_validator.rb
117
+ - lib/pure_validator/validators/numericality_validator.rb
118
+ - lib/pure_validator/validators/presence_validator.rb
119
+ - lib/pure_validator/validators/regexp_validator.rb
120
+ - lib/pure_validator/validators/url_validator.rb
121
+ - lib/pure_validator/version.rb
122
+ - pure_validator.gemspec
123
+ - spec/pure_validator/args_validator_spec.rb
124
+ - spec/pure_validator/errors_spec.rb
125
+ - spec/pure_validator/validation_errors_spec.rb
126
+ - spec/pure_validator/validator_spec.rb
127
+ - spec/pure_validator/validators/email_validator_spec.rb
128
+ - spec/pure_validator/validators/exclusion_validator_spec.rb
129
+ - spec/pure_validator/validators/inclusion_validator_spec.rb
130
+ - spec/pure_validator/validators/length_validator_spec.rb
131
+ - spec/pure_validator/validators/not_nil_validator_spec.rb
132
+ - spec/pure_validator/validators/numericality_validator_spec.rb
133
+ - spec/pure_validator/validators/presence_validator_spec.rb
134
+ - spec/pure_validator/validators/regexp_validator_spec.rb
135
+ - spec/pure_validator/validators/url_validator_spec.rb
136
+ - spec/spec_helper.rb
137
+ homepage: http://github.com/ddd-ruby/pure_validator
138
+ licenses:
139
+ - MIT
140
+ metadata: {}
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubyforge_project:
157
+ rubygems_version: 2.5.1
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: Moves validation logic to validators
161
+ test_files:
162
+ - spec/pure_validator/args_validator_spec.rb
163
+ - spec/pure_validator/errors_spec.rb
164
+ - spec/pure_validator/validation_errors_spec.rb
165
+ - spec/pure_validator/validator_spec.rb
166
+ - spec/pure_validator/validators/email_validator_spec.rb
167
+ - spec/pure_validator/validators/exclusion_validator_spec.rb
168
+ - spec/pure_validator/validators/inclusion_validator_spec.rb
169
+ - spec/pure_validator/validators/length_validator_spec.rb
170
+ - spec/pure_validator/validators/not_nil_validator_spec.rb
171
+ - spec/pure_validator/validators/numericality_validator_spec.rb
172
+ - spec/pure_validator/validators/presence_validator_spec.rb
173
+ - spec/pure_validator/validators/regexp_validator_spec.rb
174
+ - spec/pure_validator/validators/url_validator_spec.rb
175
+ - spec/spec_helper.rb