ruby_open_form_validators 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c4d6eb667b43b3c571d848f23168cad9520a07ee
4
+ data.tar.gz: e27b04b2f59b68563eb838535557c6f209c6e019
5
+ SHA512:
6
+ metadata.gz: 0faa389cdd22e865515fc4b54d33324127dc8461a240e9e510aaeafc619ad41174127e829814031f3046cbea44d0401f8bc25c88c4b586afe2f5a4b4f1d8c5b0
7
+ data.tar.gz: 2de7a892446c5d6f50e4a9837d5c4ac2719fc6091e10d6a5091ed50d308a7fbe1f7552a235731b76c5b215734471fd53e3b9409d665b14501bca02803f38c3ea
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.reek.yml ADDED
@@ -0,0 +1,126 @@
1
+ detectors:
2
+ Attribute:
3
+ enabled: false
4
+ exclude: []
5
+ BooleanParameter:
6
+ enabled: true
7
+ exclude: []
8
+ ClassVariable:
9
+ enabled: false
10
+ exclude: []
11
+ ControlParameter:
12
+ enabled: true
13
+ exclude: []
14
+ DataClump:
15
+ enabled: true
16
+ exclude:
17
+ - "RubyOpenFormValidators::ValidationsHandler"
18
+ max_copies: 2
19
+ min_clump_size: 2
20
+ DuplicateMethodCall:
21
+ enabled: true
22
+ exclude: []
23
+ max_calls: 1
24
+ allow_calls: []
25
+ FeatureEnvy:
26
+ enabled: true
27
+ exclude: []
28
+ InstanceVariableAssumption:
29
+ enabled: false
30
+ IrresponsibleModule:
31
+ enabled: false
32
+ exclude: []
33
+ LongParameterList:
34
+ enabled: true
35
+ exclude: []
36
+ max_params: 4
37
+ overrides:
38
+ initialize:
39
+ max_params: 5
40
+ LongYieldList:
41
+ enabled: true
42
+ exclude: []
43
+ max_params: 3
44
+ ManualDispatch:
45
+ enabled: true
46
+ exclude: []
47
+ MissingSafeMethod:
48
+ enabled: false
49
+ exclude: []
50
+ ModuleInitialize:
51
+ enabled: true
52
+ exclude: []
53
+ NestedIterators:
54
+ enabled: true
55
+ exclude: []
56
+ max_allowed_nesting: 2
57
+ ignore_iterators: []
58
+ NilCheck:
59
+ enabled: false
60
+ exclude: []
61
+ RepeatedConditional:
62
+ enabled: true
63
+ exclude: []
64
+ max_ifs: 3
65
+ SubclassedFromCoreClass:
66
+ enabled: true
67
+ exclude: []
68
+ TooManyConstants:
69
+ enabled: true
70
+ exclude: []
71
+ max_constants: 5
72
+ TooManyInstanceVariables:
73
+ enabled: true
74
+ exclude: []
75
+ max_instance_variables: 9
76
+ TooManyMethods:
77
+ enabled: true
78
+ exclude: []
79
+ max_methods: 25
80
+ TooManyStatements:
81
+ enabled: true
82
+ exclude:
83
+ - initialize
84
+ max_statements: 12
85
+ UncommunicativeMethodName:
86
+ enabled: true
87
+ exclude: []
88
+ reject:
89
+ - "/^[a-z]$/"
90
+ - "/[0-9]$/"
91
+ - "/[A-Z]/"
92
+ accept: []
93
+ UncommunicativeModuleName:
94
+ enabled: true
95
+ exclude: []
96
+ reject:
97
+ - "/^.$/"
98
+ - "/[0-9]$/"
99
+ accept:
100
+ - Inline::C
101
+ - "/V[0-9]/"
102
+ UncommunicativeParameterName:
103
+ enabled: true
104
+ exclude: []
105
+ reject:
106
+ - "/^.$/"
107
+ - "/[0-9]$/"
108
+ - "/[A-Z]/"
109
+ accept: []
110
+ UncommunicativeVariableName:
111
+ enabled: true
112
+ exclude:
113
+ - "RubyOpenFormValidators::ValidationsHandler#handler"
114
+ reject:
115
+ - "/^.$/"
116
+ - "/[0-9]$/"
117
+ - "/[A-Z]/"
118
+ accept:
119
+ - _
120
+ UnusedParameters:
121
+ enabled: true
122
+ exclude: []
123
+ UnusedPrivateMethod:
124
+ enabled: false
125
+ UtilityFunction:
126
+ enabled: false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ Documentation:
2
+ Enabled: false
3
+
4
+ Metrics/AbcSize:
5
+ Max: 15
6
+
7
+ Metrics/BlockLength:
8
+ CountComments: false
9
+ Max: 25
10
+ Exclude:
11
+ - spec/**/*
12
+ ExcludedMethods:
13
+ - class_methods
14
+
15
+ Metrics/BlockNesting:
16
+ Max: 4
17
+
18
+ Metrics/ClassLength:
19
+ CountComments: false
20
+ Max: 200
21
+
22
+ Metrics/CyclomaticComplexity:
23
+ Max: 7
24
+
25
+ Metrics/MethodLength:
26
+ CountComments: false
27
+ Max: 24
28
+
29
+ Metrics/ModuleLength:
30
+ CountComments: false
31
+ Max: 200
32
+
33
+ Metrics/LineLength:
34
+ Max: 100
35
+
36
+ Metrics/ParameterLists:
37
+ Max: 5
38
+ CountKeywordArgs: true
39
+
40
+ Metrics/PerceivedComplexity:
41
+ Max: 12
42
+
43
+ Style/ModuleFunction:
44
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+
3
+ before_install: gem install bundler -v 2.0.2
4
+
5
+ cache: bundler
6
+
7
+ rvm:
8
+ - 2.4.1
9
+
10
+ sudo: false
11
+
12
+ script:
13
+ - bundle exec rake code_analysis
14
+ - bundle exec rspec
@@ -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 elianmarchisio@gmail.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/CONTRIBUTING.md ADDED
@@ -0,0 +1,42 @@
1
+ ## Contributing ##
2
+
3
+ You can contribute to this repo if you have an issue, found a bug or think there's some functionality required that would add value to the gem. To do so, please check if there's not already an [issue](https://github.com/rootstrap/ruby_open_form_validators/issues) for that, if you find there's not, create a new one with as much detail as possible.
4
+
5
+ If you want to contribute with code as well, please follow the next steps:
6
+
7
+ 1. Read, understand and agree to our [code of conduct](https://github.com/rootstrap/ruby_open_form_validators/blob/master/CODE_OF_CONDUCT.md)
8
+ 2. [Fork the repo](https://help.github.com/articles/about-forks/)
9
+ 3. Clone the project into your machine:
10
+ `$ git clone git@github.com:[YOUR GITHUB USERNAME]/ruby_open_form_validators.git`
11
+ 4. Access the repo:
12
+ `$ cd ruby_open_form_validators`
13
+ 5. Create your feature/bugfix branch:
14
+ `$ git checkout -b your_new_feature`
15
+ or
16
+ `$ git checkout -b fix/your_fix` in case of a bug fix
17
+ (if your PR is to address an existing issue, it would be good to name the branch after the issue, for example: if you are trying to solve issue 182, then a good idea for the branch name would be `182_your_new_feature`)
18
+ 6. Write tests for your changes (feature/bug)
19
+ 7. Code your (feature/bugfix)
20
+ 8. Run the code analysis tool by doing:
21
+ `$ rake code_analysis`
22
+ 9. Run the tests:
23
+ `$ bundle exec rspec`
24
+ All tests must pass. If all tests (both code analysis and rspec) do pass, then you are ready to go to the next step:
25
+ 10. Commit your changes:
26
+ `$ git commit -m 'Your feature or bugfix title'`
27
+ 11. Push to the branch `$ git push origin your_new_feature`
28
+ 12. Create a new [pull request](https://help.github.com/articles/creating-a-pull-request/)
29
+
30
+ Some helpful guides that will help you know how we work:
31
+ 1. [Code review](https://github.com/rootstrap/tech-guides/tree/master/code-review)
32
+ 2. [GIT workflow](https://github.com/rootstrap/tech-guides/tree/master/git)
33
+ 3. [Ruby style guide](https://github.com/rootstrap/tech-guides/tree/master/ruby)
34
+ 4. [Rails style guide](https://github.com/rootstrap/tech-guides/blob/master/ruby/rails.md)
35
+ 5. [RSpec style guide](https://github.com/rootstrap/tech-guides/blob/master/ruby/rspec/README.md)
36
+
37
+ For more information or guides like the ones mentioned above, please check our [tech guides](https://github.com/rootstrap/tech-guides). Keep in mind that the more you know about these guides, the easier it will be for your code to get approved and merged.
38
+
39
+ Note: We work with one commit per pull request, so if you make your commit and realize you were missing something or want to add something more to it, don't create a new commit with the changes, but use `$ git commit --amend` instead. This same principle also applies for when changes are requested on an open pull request.
40
+
41
+
42
+ Thank you very much for your time and for considering helping in this project.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in ruby_open_form_validators.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Rootstrap
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,88 @@
1
+ [![Build Status](https://travis-ci.org/rootstrap/ruby_open_form_validators.svg?branch=master)](https://travis-ci.org/rootstrap/ruby_open_form_validators)
2
+
3
+ # RubyOpenFormValidators
4
+
5
+ Ruby impementation of the Open Form Validators library (https://github.com/rootstrap/open-form-validators). It supports several validators such as *minValue*, *maxValue*, *minLength*, *maxLength*, *minDate, *maxDate, *earliestToday*, *email* and also combinations.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ruby_open_form_validators'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruby_open_form_validators
22
+
23
+ ## Usage
24
+
25
+ It can be used using a single or a combined validator string.
26
+
27
+ Passing validations will return the same result:
28
+ ```ruby
29
+ $ RubyOpenFormValidators.validate("50", "minValue25")
30
+ { text: "50", valid: true, messages: [] }
31
+
32
+ $ RubyOpenFormValidators.validate("Lorem ipsum", "minLength5, maxLength15")
33
+ { text: "Lorem ipsum", valid: true, messages: [] }
34
+ ```
35
+
36
+ Failing validations will return different messages according to the used validators.
37
+ ```ruby
38
+ # Single validator examples:
39
+ $ RubyOpenFormValidators.validate("20", "minValue25")
40
+ { text: "20", valid: false, message: "Value must be greater than 25" }
41
+
42
+ $ RubyOpenFormValidators.validate("Lorem ipsum", "maxLength5")
43
+ { text: "Lorem ipsum", valid: false, message: "Length must be shorter than 5 characters" }
44
+
45
+ $ RubyOpenFormValidators.validate("20190905", "minDate20190806")
46
+ { text: "20190905", valid: false, message: "Date must be after 20190806" }
47
+
48
+ $ RubyOpenFormValidators.validate("20181206", "earliestToday")
49
+ { text: "20181206", valid: false, message: "Date must be after today's date" }
50
+
51
+ $ RubyOpenFormValidators.validate("@example.com", "email")
52
+ { text: @example., valid: false, message: "Wrong email format" }
53
+
54
+ # Combined validator examples:
55
+ $ RubyOpenFormValidators.validate("20", "minValue25,maxValue30")
56
+ { text: "20", valid: false, message: "Value must be greater than 25" }
57
+
58
+ $ RubyOpenFormValidators.validate("Lorem ipsum", "minLength2,maxLength5")
59
+ { text: "Lorem ipsum", valid: false, message: "Length must be shorter than 5 characters" }
60
+
61
+ $ RubyOpenFormValidators.validate("20190905", "minDate20190806,maxDate20190810")
62
+ { text: "20190905", valid: false, message: "Date must be after 20190806" }
63
+ ```
64
+
65
+ ## Development
66
+
67
+ 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.
68
+
69
+ 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).
70
+
71
+ ## Contributing
72
+
73
+ Please read our [CONTRIBUTING](https://github.com/rootstrap/ruby_open_form_validators/blob/master/CONTRIBUTING.md) and our [CODE_OF_CONDUCT](https://github.com/rootstrap/ruby_open_form_validators/blob/master/CODE_OF_CONDUCT.md) files for details on our code of conduct, and the process for submitting pull requests to us.
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
78
+
79
+ ## Code of Conduct
80
+
81
+ Everyone interacting in the RubyOpenFormValidators project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rootstrap/ruby_open_form_validators/blob/master/CODE_OF_CONDUCT.md).
82
+
83
+ ## Credits
84
+
85
+ RubyOpenFormValidators is maintained by [Rootstrap](http://www.rootstrap.com) with the help of our
86
+ [contributors](https://github.com/rootstrap/ruby_open_form_validators/contributors).
87
+
88
+ [<img src="https://s3-us-west-1.amazonaws.com/rootstrap.com/img/rs.png" width="100"/>](http://www.rootstrap.com)
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ task :code_analysis do
4
+ sh 'bundle exec rubocop lib spec'
5
+ sh 'bundle exec reek lib'
6
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'ruby_open_form_validators'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_open_form_validators/version'
4
+ require 'ruby_open_form_validators/validations_handler'
5
+ require 'ruby_open_form_validators/parser'
6
+ require 'active_support/core_ext/string/inflections'
7
+
8
+ module RubyOpenFormValidators
9
+ def self.validate(value, validators)
10
+ empty_accum = { text: value, valid: true, messages: [] }
11
+ validators.split(',').each_with_object(empty_accum) do |validator, accum|
12
+ validator_key = Parser.remove_digits(validator).underscore
13
+ result = if ValidationsHandler.method_defined?(validator_key)
14
+ ValidationsHandler.public_send(validator_key, value, validator)
15
+ else
16
+ { valid: true, message: nil }
17
+ end
18
+
19
+ accum[:valid] &&= result[:valid]
20
+ message = result[:message]
21
+ accum[:messages] << message unless message.nil?
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyOpenFormValidators
4
+ module Constants
5
+ EMAIL_REGEX = %r{\A[a-zA-Z0-9][a-zA-Z0-9.!\#$%&'*+/=?^_`{|}~-]*
6
+ @
7
+ [a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
8
+ (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\z}x.freeze
9
+
10
+ DATE_FORMAT = '%Y%m%d'
11
+ end
12
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_open_form_validators/constants'
4
+
5
+ module RubyOpenFormValidators
6
+ module Parser
7
+ module_function
8
+
9
+ def remove_non_digits(string)
10
+ string.gsub(/^[a-zA-Z]+/, '')
11
+ end
12
+
13
+ def remove_digits(string)
14
+ string.gsub(/\d+(.\d+(e[+-]\d+)?)?/, '')
15
+ end
16
+
17
+ def to_date!(attribute)
18
+ Date.parse(attribute, Constants::DATE_FORMAT)
19
+ end
20
+
21
+ def to_number!(attribute)
22
+ return attribute if attribute.is_a?(Numeric)
23
+ return attribute.to_f if attribute.is_a?(String) && numeric?(attribute)
24
+
25
+ raise 'invalid numeric value'
26
+ end
27
+
28
+ def numeric?(string_number)
29
+ !Float(string_number).nil?
30
+ rescue StandardError
31
+ false
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_open_form_validators/parser'
4
+ require 'ruby_open_form_validators/constants'
5
+ require 'active_support/core_ext/date_time/calculations'
6
+
7
+ module RubyOpenFormValidators
8
+ module ValidationsHandler
9
+ extend self
10
+
11
+ def min_value(value, validator)
12
+ handler do
13
+ expected_value = Parser.remove_non_digits(validator)
14
+ valid = Parser.to_number!(value) >= Parser.to_number!(expected_value)
15
+ create_response(valid, "Value must be greater than #{expected_value}")
16
+ end
17
+ end
18
+
19
+ def max_value(value, validator)
20
+ handler do
21
+ expected_value = Parser.remove_non_digits(validator)
22
+ valid = Parser.to_number!(value) <= Parser.to_number!(expected_value)
23
+ create_response(valid, "Value must be less than #{expected_value}")
24
+ end
25
+ end
26
+
27
+ def min_length(value, validator)
28
+ handler do
29
+ expected_length = Parser.remove_non_digits(validator)
30
+ valid = value.to_s.length >= Parser.to_number!(expected_length)
31
+ create_response(valid, "Length must be longer than #{expected_length} characters")
32
+ end
33
+ end
34
+
35
+ def max_length(value, validator)
36
+ handler do
37
+ expected_length = Parser.remove_non_digits(validator)
38
+ valid = value.length <= Parser.to_number!(expected_length)
39
+ create_response(valid, "Length must be shorter than #{expected_length} characters")
40
+ end
41
+ end
42
+
43
+ def min_date(value, validator)
44
+ handler do
45
+ expected_value = Parser.remove_non_digits(validator)
46
+ valid = Parser.to_date!(value) >= Parser.to_date!(expected_value)
47
+ create_response(valid, "Date must be after #{expected_value}")
48
+ end
49
+ end
50
+
51
+ def max_date(value, validator)
52
+ handler do
53
+ expected_value = Parser.remove_non_digits(validator)
54
+ valid = Parser.to_date!(value) <= Parser.to_date!(expected_value)
55
+ create_response(valid, "Date must be before #{expected_value}")
56
+ end
57
+ end
58
+
59
+ def earliest_today(value, _)
60
+ handler do
61
+ today = DateTime.now.beginning_of_day.strftime(Constants::DATE_FORMAT)
62
+ valid = Parser.to_date!(value) >= Parser.to_date!(today)
63
+ create_response(valid, "Date must be after today's date")
64
+ end
65
+ end
66
+
67
+ def email(value, _)
68
+ valid = Constants::EMAIL_REGEX.match?(value)
69
+ create_response(valid, 'Wrong email format')
70
+ end
71
+
72
+ def create_response(valid, error_message)
73
+ {
74
+ valid: valid,
75
+ message: valid ? nil : error_message
76
+ }
77
+ end
78
+
79
+ def handler
80
+ yield
81
+ rescue StandardError => e
82
+ create_response(false, e.message)
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyOpenFormValidators
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ruby_open_form_validators/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ruby_open_form_validators'
9
+ spec.version = RubyOpenFormValidators::VERSION
10
+ spec.authors = ['Elián Marchisio']
11
+ spec.email = ['elian.marchisio@rootstrap.com']
12
+
13
+ spec.summary = 'Ruby gem for validating OpenForm.'
14
+ spec.homepage = 'https://github.com/rootstrap/ruby_open_form_validators'
15
+ spec.license = 'MIT'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = 'exe'
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ['lib']
28
+
29
+ spec.add_development_dependency 'bundler', '~> 2.0'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'reek', '~> 5.5.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.0'
33
+ spec.add_development_dependency 'rubocop', '~> 0.76.0'
34
+
35
+ spec.add_dependency 'activesupport', '~> 5.2', '>= 5.2.3'
36
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_open_form_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elián Marchisio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-03 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: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: reek
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.76.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.76.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.2'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 5.2.3
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: '5.2'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 5.2.3
103
+ description:
104
+ email:
105
+ - elian.marchisio@rootstrap.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".reek.yml"
112
+ - ".rspec"
113
+ - ".rubocop.yml"
114
+ - ".travis.yml"
115
+ - CODE_OF_CONDUCT.md
116
+ - CONTRIBUTING.md
117
+ - Gemfile
118
+ - LICENSE.txt
119
+ - README.md
120
+ - Rakefile
121
+ - bin/console
122
+ - bin/setup
123
+ - lib/ruby_open_form_validators.rb
124
+ - lib/ruby_open_form_validators/constants.rb
125
+ - lib/ruby_open_form_validators/parser.rb
126
+ - lib/ruby_open_form_validators/validations_handler.rb
127
+ - lib/ruby_open_form_validators/version.rb
128
+ - ruby_open_form_validators.gemspec
129
+ homepage: https://github.com/rootstrap/ruby_open_form_validators
130
+ licenses:
131
+ - MIT
132
+ metadata:
133
+ homepage_uri: https://github.com/rootstrap/ruby_open_form_validators
134
+ source_code_uri: https://github.com/rootstrap/ruby_open_form_validators
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.5.2.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Ruby gem for validating OpenForm.
155
+ test_files: []