fluent_validation 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +41 -0
  9. data/Rakefile +7 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/fluent_validation.gemspec +36 -0
  13. data/lib/fluent_validation/attribute_rule.rb +32 -0
  14. data/lib/fluent_validation/fluent_validator.rb +30 -0
  15. data/lib/fluent_validation/results/validation_failure.rb +13 -0
  16. data/lib/fluent_validation/results/validation_result.rb +15 -0
  17. data/lib/fluent_validation/results/validation_success.rb +7 -0
  18. data/lib/fluent_validation/rule_builder.rb +95 -0
  19. data/lib/fluent_validation/validators/attribute_validator.rb +28 -0
  20. data/lib/fluent_validation/validators/attribute_validator_context.rb +14 -0
  21. data/lib/fluent_validation/validators/child_collection_validator_adaptor.rb +20 -0
  22. data/lib/fluent_validation/validators/email_validator.rb +19 -0
  23. data/lib/fluent_validation/validators/exact_length_validator.rb +23 -0
  24. data/lib/fluent_validation/validators/greater_than_or_equal_validator.rb +19 -0
  25. data/lib/fluent_validation/validators/greater_than_validator.rb +19 -0
  26. data/lib/fluent_validation/validators/inclusive_between_validator.rb +20 -0
  27. data/lib/fluent_validation/validators/length_validator.rb +24 -0
  28. data/lib/fluent_validation/validators/less_than_or_equal_validator.rb +19 -0
  29. data/lib/fluent_validation/validators/less_than_validator.rb +19 -0
  30. data/lib/fluent_validation/validators/not_empty_validator.rb +28 -0
  31. data/lib/fluent_validation/validators/not_empty_validator_array_handler.rb +15 -0
  32. data/lib/fluent_validation/validators/not_empty_validator_handler.rb +28 -0
  33. data/lib/fluent_validation/validators/not_empty_validator_string_handler.rb +15 -0
  34. data/lib/fluent_validation/validators/not_nil_validator.rb +15 -0
  35. data/lib/fluent_validation/validators/predicate_validator.rb +19 -0
  36. data/lib/fluent_validation/validators/regular_expression_validator.rb +19 -0
  37. data/lib/fluent_validation/validators/validator.rb +9 -0
  38. data/lib/fluent_validation/version.rb +3 -0
  39. data/lib/fluent_validation.rb +5 -0
  40. metadata +124 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 69569fd0b2c5a4af838a91ca8cc26ceff756f5ae
4
+ data.tar.gz: d6f2aded807928d463d64b1ae2dac3529cb49494
5
+ SHA512:
6
+ metadata.gz: 9810fbbf4de91a52af5b0635835852c0522d26f566fcd60775b5f466307a1b40415db3fcba2cb6bf06ecc11ed3fdc56faa5284419cebfa1c9d1ee2953f3fa394
7
+ data.tar.gz: 32b85486da56acbba02279a3a160d934772db5d09e5bfd5d178fed43eb375db6120a187c267ee0a73c640f7731b65978c183006fcd1afbfe5c246b7f865488c3
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+
14
+ # RubyMine
15
+ /.idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at nan.yu@rea-group.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fluent_validation.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Nan Yu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # FluentValidation
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fluent_validation`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fluent_validation'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fluent_validation
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fluent_validation. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fluent_validation"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fluent_validation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fluent_validation"
8
+ spec.version = FluentValidation::VERSION
9
+ spec.authors = ["Aaron Yu"]
10
+ spec.email = ["aaronyu@live.com"]
11
+
12
+ spec.summary = %q{Write a short summary, because Rubygems requires one.}
13
+ spec.description = %q{Write a longer description or delete this line.}
14
+ spec.homepage = "https://aaronyu.visualstudio.com/_git/fluent_validation"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.14"
34
+ spec.add_development_dependency "rake", "~> 12.0.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,32 @@
1
+ require 'fluent_validation/results/validation_success'
2
+ require 'fluent_validation/results/validation_failure'
3
+ require 'fluent_validation/validators/attribute_validator_context'
4
+
5
+ module FluentValidation
6
+ class AttributeRule
7
+ attr_reader :expression, :validators
8
+ attr_accessor :attribute_name, :condition
9
+
10
+ def initialize(&expression)
11
+ @expression = expression
12
+ @validators = Array.new
13
+ end
14
+
15
+ def add_validator(validator)
16
+ @validators << validator
17
+ end
18
+
19
+ def validate(object)
20
+ validation_failures = Array.new
21
+ if @condition.nil? || @condition.call(object)
22
+ value = expression.call object
23
+ validator_context = Validators::AttributeValidatorContext.new @attribute_name, value
24
+ @validators.each do |validator|
25
+ result = validator.validate validator_context
26
+ validation_failures.concat result if result.class != Results::ValidationSuccess
27
+ end
28
+ end
29
+ validation_failures
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,30 @@
1
+ require 'fluent_validation/attribute_rule'
2
+ require 'fluent_validation/rule_builder'
3
+ require 'fluent_validation/results/validation_failure'
4
+ require 'fluent_validation/results/validation_success'
5
+ require 'fluent_validation/results/validation_result'
6
+
7
+ module FluentValidation
8
+ class FluentValidator
9
+ def initialize
10
+ @attribute_rules = Array.new
11
+ end
12
+
13
+ def rule_for(&expression)
14
+ rule = AttributeRule.new(&expression)
15
+ @attribute_rules << rule
16
+ RuleBuilder.new(rule)
17
+ end
18
+
19
+ def validate(object)
20
+ failures = Array.new
21
+
22
+ @attribute_rules.each do |rule|
23
+ result = rule.validate object
24
+ failures.concat result if result.class != Results::ValidationSuccess
25
+ end
26
+
27
+ Results::ValidationResult.new failures
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ module FluentValidation
2
+ module Results
3
+ class ValidationFailure
4
+ attr_reader :attribute_name
5
+ attr_reader :message
6
+
7
+ def initialize (attribute_name, message)
8
+ @attribute_name = attribute_name
9
+ @message = message
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module FluentValidation
2
+ module Results
3
+ class ValidationResult
4
+ attr_reader :validation_failures
5
+
6
+ def initialize(validation_failures)
7
+ @validation_failures = validation_failures
8
+ end
9
+
10
+ def is_valid?
11
+ @validation_failures.nil? || @validation_failures.length <= 0
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module FluentValidation
2
+ module Results
3
+ class ValidationSuccess
4
+
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,95 @@
1
+ require 'fluent_validation/validators/not_nil_validator'
2
+ require 'fluent_validation/validators/length_validator'
3
+ require 'fluent_validation/validators/exact_length_validator'
4
+ require 'fluent_validation/validators/email_validator'
5
+ require 'fluent_validation/validators/less_than_validator'
6
+ require 'fluent_validation/validators/less_than_or_equal_validator'
7
+ require 'fluent_validation/validators/greater_than_validator'
8
+ require 'fluent_validation/validators/greater_than_or_equal_validator'
9
+ require 'fluent_validation/validators/regular_expression_validator'
10
+ require 'fluent_validation/validators/predicate_validator'
11
+ require 'fluent_validation/validators/child_collection_validator_adaptor'
12
+
13
+ module FluentValidation
14
+ class RuleBuilder
15
+ def initialize(attribute_rule)
16
+ @attribute_rule = attribute_rule
17
+ end
18
+
19
+ def not_nil
20
+ validator = Validators::NotNilValidator.new
21
+ @attribute_rule.add_validator validator
22
+ self
23
+ end
24
+
25
+ def length(min, max)
26
+ validator = Validators::LengthValidator.new(min, max)
27
+ @attribute_rule.add_validator validator
28
+ self
29
+ end
30
+
31
+ def exact_length(length)
32
+ validator = Validators::ExactLengthValidator.new(length)
33
+ @attribute_rule.add_validator validator
34
+ self
35
+ end
36
+
37
+ def email_address
38
+ validator = Validators::EmailValidator.new
39
+ @attribute_rule.add_validator validator
40
+ self
41
+ end
42
+
43
+ def less_than(comparison_value)
44
+ validator = Validators::LessThanValidator.new(comparison_value)
45
+ @attribute_rule.add_validator validator
46
+ self
47
+ end
48
+
49
+ def less_than_or_equal(comparison_value)
50
+ validator = Validators::LessThanOrEqualValidator.new(comparison_value)
51
+ @attribute_rule.add_validator validator
52
+ self
53
+ end
54
+
55
+ def greater_than(comparison_value)
56
+ validator = Validators::GreaterThanValidator.new(comparison_value)
57
+ @attribute_rule.add_validator validator
58
+ self
59
+ end
60
+
61
+ def greater_than_or_equal(comparison_value)
62
+ validator = Validators::GreaterThanOrEqualValidator.new(comparison_value)
63
+ @attribute_rule.add_validator validator
64
+ self
65
+ end
66
+
67
+ def matches(regexp)
68
+ validator = Validators::RegularExpressionValidator.new(regexp)
69
+ @attribute_rule.add_validator validator
70
+ self
71
+ end
72
+
73
+ def must(&expression)
74
+ validator = Validators::PredicateValidator.new(expression)
75
+ @attribute_rule.add_validator validator
76
+ self
77
+ end
78
+
79
+ def set_collection_validator(validator)
80
+ validator_adaptor = Validators::ChildCollectionValidatorAdaptor.new validator
81
+ @attribute_rule.add_validator validator_adaptor
82
+ self
83
+ end
84
+
85
+ def with_name(name)
86
+ @attribute_rule.attribute_name = name
87
+ self
88
+ end
89
+
90
+ def when(&expression)
91
+ @attribute_rule.condition = expression
92
+ self
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,28 @@
1
+ require 'fluent_validation/validators/validator'
2
+ require 'fluent_validation/results/validation_failure'
3
+ require 'fluent_validation/results/validation_success'
4
+
5
+ module FluentValidation
6
+ module Validators
7
+ class AttributeValidator < Validator
8
+ def validate(validator_context)
9
+ validation_failures = Array.new
10
+
11
+ unless is_valid?(validator_context)
12
+ failure_message = generate_failure_message(validator_context.attribute_name, validator_context.attribute_value)
13
+ validation_failures << Results::ValidationFailure.new(validator_context.attribute_name, failure_message)
14
+ end
15
+
16
+ validation_failures
17
+ end
18
+
19
+ def is_valid?(validator_context)
20
+ raise NotImplementedError.new 'is_valid?'
21
+ end
22
+
23
+ def generate_failure_message(attribute_name, attribute_value)
24
+ raise NotImplementedError.new 'generate_failure_message'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,14 @@
1
+ module FluentValidation
2
+ module Validators
3
+ class AttributeValidatorContext
4
+ attr_reader :attribute_name, :attribute_value
5
+
6
+ def initialize(attribute_name, attribute_value)
7
+ @attribute_name = attribute_name
8
+ @attribute_value = attribute_value
9
+ end
10
+
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,20 @@
1
+ require 'fluent_validation/validators/validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class ChildCollectionValidatorAdaptor < Validator
6
+ def initialize(fluent_validator)
7
+ @fluent_validator = fluent_validator
8
+ end
9
+
10
+ def validate(validator_context)
11
+ validation_failures = Array.new
12
+ validator_context.attribute_value.each do |item|
13
+ result = @fluent_validator.validate item
14
+ validation_failures.concat result.validation_failures
15
+ end
16
+ validation_failures
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class EmailValidator < AttributeValidator
6
+ def initialize
7
+ @regexp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ @regexp.match? validator_context.attribute_value
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "#{attribute_name} is not a valid email address"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class ExactLengthValidator < AttributeValidator
6
+ def initialize(length)
7
+ @length = length
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ if validator_context.attribute_value.nil?
12
+ false
13
+ else
14
+ validator_context.attribute_value.length == @length
15
+ end
16
+ end
17
+
18
+ def generate_failure_message(attribute_name, attribute_value)
19
+ "#{attribute_name} must be #{@length} characters in length. You provided #{attribute_value.length} characters"
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class GreaterThanOrEqualValidator < AttributeValidator
6
+ def initialize(comparison_value)
7
+ @comparison_value = comparison_value
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ validator_context.attribute_value >= @comparison_value
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "#{attribute_name} must be greater than or equal to #{@comparison_value}."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class GreaterThanValidator < AttributeValidator
6
+ def initialize(comparison_value)
7
+ @comparison_value = comparison_value
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ validator_context.attribute_value > @comparison_value
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "#{attribute_name} must be greater than #{@comparison_value}."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class InclusiveBetweenValidator < AttributeValidator
6
+ def initialize(from, to)
7
+ @from = from
8
+ @to = to
9
+ end
10
+
11
+ def is_valid?(validator_context)
12
+ validator_context.attribute_value >=@from && validator_context.attribute_value <= @to
13
+ end
14
+
15
+ def generate_failure_message(attribute_name, attribute_value)
16
+ "#{attribute_name} must be between #{@from} and #{@to}. You entered #{attribute_value}."
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class LengthValidator < AttributeValidator
6
+ def initialize(min, max)
7
+ @min = min
8
+ @max = max
9
+ end
10
+
11
+ def is_valid?(validator_context)
12
+ if validator_context.attribute_value.nil?
13
+ false
14
+ else
15
+ validator_context.attribute_value.length >= @min && validator_context.attribute_value.length <= @max
16
+ end
17
+ end
18
+
19
+ def generate_failure_message(attribute_name, attribute_value)
20
+ "#{attribute_name} must be between #{@min} and #{@max} characters. You provided #{attribute_value.length} characters."
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class LessThanOrEqualValidator < AttributeValidator
6
+ def initialize(comparison_value)
7
+ @comparison_value = comparison_value
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ validator_context.attribute_value <= @comparison_value
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "#{attribute_name} must be less than or equal to #{@comparison_value}."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class LessThanValidator < AttributeValidator
6
+ def initialize(comparison_value)
7
+ @comparison_value = comparison_value
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ validator_context.attribute_value < @comparison_value
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "#{attribute_name} must be less than #{@comparison_value}."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,28 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+ require 'fluent_validation/validators/not_empty_validator_string_handler'
3
+ require 'fluent_validation/validators/not_empty_validator_array_handler'
4
+
5
+ module FluentValidation
6
+ module Validators
7
+ class NotEmptyValidator < AttributeValidator
8
+ def initialize
9
+ super
10
+ @not_empty_validator_chain = build_validator_chain
11
+ end
12
+
13
+ def is_valid?(validator_context)
14
+ @not_empty_validator_chain.handle validator_context.attribute_value
15
+ end
16
+
17
+ def generate_failure_message(attribute_name, attribute_value)
18
+ "#{attribute_name} must not be empty."
19
+ end
20
+
21
+ private
22
+ def build_validator_chain
23
+ array_validator = NotEmptyValidatorArrayHandler.new nil
24
+ NotEmptyValidatorStringHandler.new array_validator
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class NotEmptyValidatorArrayHandler < NotEmptyValidatorHandler
6
+ def can_handle?(object)
7
+ object.class == Array
8
+ end
9
+
10
+ def respond(object)
11
+ object.length > 0
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module FluentValidation
2
+ module Validators
3
+ class NotEmptyValidatorHandler
4
+ def initialize(next_handler)
5
+ @next_handler = next_handler
6
+ end
7
+
8
+ def handle(object)
9
+ if can_handle? object
10
+ respond object
11
+ elsif !@next_handler.nil?
12
+ @next_handler.handle object
13
+ else
14
+ true
15
+ end
16
+ end
17
+
18
+ private
19
+ def can_handle?(object)
20
+ raise NotImplementedError.new 'can_handle?'
21
+ end
22
+
23
+ def respond(object)
24
+ raise NotImplementedError.new 'respond'
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'fluent_validation/validators/not_empty_validator_handler'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class NotEmptyValidatorStringHandler < NotEmptyValidatorHandler
6
+ def can_handle?(object)
7
+ object.class == String
8
+ end
9
+
10
+ def respond(object)
11
+ object.length > 0
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class NotNilValidator < AttributeValidator
6
+ def is_valid?(validator_context)
7
+ !validator_context.attribute_value.nil?
8
+ end
9
+
10
+ def generate_failure_message(attribute_name, attribute_value)
11
+ "#{attribute_name} must not be empty."
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class PredicateValidator < AttributeValidator
6
+ def initialize(expression)
7
+ @expression = expression
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ @expression.call(validator_context.attribute_value)
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "The specified condition was not met for #{attribute_name}."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'fluent_validation/validators/attribute_validator'
2
+
3
+ module FluentValidation
4
+ module Validators
5
+ class RegularExpressionValidator < AttributeValidator
6
+ def initialize(pattern)
7
+ @pattern = pattern
8
+ end
9
+
10
+ def is_valid?(validator_context)
11
+ @pattern.match?(validator_context.attribute_value)
12
+ end
13
+
14
+ def generate_failure_message(attribute_name, attribute_value)
15
+ "#{attribute_name} is not in the correct format."
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module FluentValidation
2
+ module Validators
3
+ class Validator
4
+ def validate(validator_context)
5
+ raise NotImplementedError.new 'validate'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module FluentValidation
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,5 @@
1
+ require 'fluent_validation/version'
2
+
3
+ module FluentValidation
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fluent_validation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Yu
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - aaronyu@live.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - fluent_validation.gemspec
73
+ - lib/fluent_validation.rb
74
+ - lib/fluent_validation/attribute_rule.rb
75
+ - lib/fluent_validation/fluent_validator.rb
76
+ - lib/fluent_validation/results/validation_failure.rb
77
+ - lib/fluent_validation/results/validation_result.rb
78
+ - lib/fluent_validation/results/validation_success.rb
79
+ - lib/fluent_validation/rule_builder.rb
80
+ - lib/fluent_validation/validators/attribute_validator.rb
81
+ - lib/fluent_validation/validators/attribute_validator_context.rb
82
+ - lib/fluent_validation/validators/child_collection_validator_adaptor.rb
83
+ - lib/fluent_validation/validators/email_validator.rb
84
+ - lib/fluent_validation/validators/exact_length_validator.rb
85
+ - lib/fluent_validation/validators/greater_than_or_equal_validator.rb
86
+ - lib/fluent_validation/validators/greater_than_validator.rb
87
+ - lib/fluent_validation/validators/inclusive_between_validator.rb
88
+ - lib/fluent_validation/validators/length_validator.rb
89
+ - lib/fluent_validation/validators/less_than_or_equal_validator.rb
90
+ - lib/fluent_validation/validators/less_than_validator.rb
91
+ - lib/fluent_validation/validators/not_empty_validator.rb
92
+ - lib/fluent_validation/validators/not_empty_validator_array_handler.rb
93
+ - lib/fluent_validation/validators/not_empty_validator_handler.rb
94
+ - lib/fluent_validation/validators/not_empty_validator_string_handler.rb
95
+ - lib/fluent_validation/validators/not_nil_validator.rb
96
+ - lib/fluent_validation/validators/predicate_validator.rb
97
+ - lib/fluent_validation/validators/regular_expression_validator.rb
98
+ - lib/fluent_validation/validators/validator.rb
99
+ - lib/fluent_validation/version.rb
100
+ homepage: https://aaronyu.visualstudio.com/_git/fluent_validation
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.6.11
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Write a short summary, because Rubygems requires one.
124
+ test_files: []