simple_validate 1.2.2 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rspec_status +4 -0
- data/.rubocop.yml +10 -0
- data/Gemfile +7 -5
- data/README.md +3 -29
- data/Rakefile +5 -3
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/lib/simple_validate/errors.rb +2 -0
- data/lib/simple_validate/utils.rb +14 -0
- data/lib/simple_validate/validates_base.rb +2 -0
- data/lib/simple_validate/validates_exclusion_of.rb +3 -0
- data/lib/simple_validate/validates_format_of.rb +5 -2
- data/lib/simple_validate/validates_inclusion_of.rb +3 -0
- data/lib/simple_validate/validates_length_of.rb +10 -12
- data/lib/simple_validate/validates_presence_of.rb +2 -0
- data/lib/simple_validate/validates_set_base.rb +2 -2
- data/lib/simple_validate/validates_type_of.rb +22 -0
- data/lib/simple_validate/version.rb +3 -1
- data/lib/simple_validate.rb +9 -7
- data/simple_validate.gemspec +14 -13
- metadata +15 -41
- data/.travis.yml +0 -3
- data/lib/simple_validate/validates_numericality_of.rb +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1f7051ee7f5fc5cec9a9f1f7e4859b6145172302ba667b71d11710b7ca34ad1e
|
4
|
+
data.tar.gz: bfbbc3767ffee4d622411b1fd1673e32710ceeb611d588e7fa34799e23aa092e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8721e31ba42d72dc48e2690284676930b50c6a9ad6765105e36d51a6c9d068ea8804e1bbfe83424a3d0b725b9a39b19a7cd65f0afe14fe8129da0e66d97d44e6
|
7
|
+
data.tar.gz: 6e0c1805d7e4027ecb6fec0b4d727a99ef02c0fbe33e9c0e54c2f0f92843de516d63dcf095c31c6b3bfc396f4b7f6258285df8522a056cc531e38cce09343b21
|
data/.rspec_status
ADDED
data/.rubocop.yml
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.1
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
1
8
|
Metrics/BlockLength:
|
2
9
|
Enabled: false
|
3
10
|
|
@@ -9,3 +16,6 @@ Metrics/AbcSize:
|
|
9
16
|
|
10
17
|
Style/DoubleNegation:
|
11
18
|
Enabled: false
|
19
|
+
|
20
|
+
Lint/ConstantDefinitionInBlock:
|
21
|
+
Enabled: false
|
data/Gemfile
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
gem "rake"
|
4
6
|
|
5
7
|
group :development do
|
6
|
-
gem
|
8
|
+
gem "pry"
|
7
9
|
end
|
8
10
|
|
9
11
|
group :test do
|
10
|
-
gem
|
11
|
-
gem
|
12
|
+
gem "rspec"
|
13
|
+
gem "rubocop"
|
12
14
|
end
|
13
15
|
|
14
16
|
gemspec
|
data/README.md
CHANGED
@@ -1,29 +1,15 @@
|
|
1
1
|
# Simple Validate
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
> Borrowing ideas from [validatable](https://github.com/jnunemaker/validatable) and Rails validations, this is a library for validations for any ruby object.
|
3
|
+
> PORO validation mixin with no deps
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
7
|
```ruby
|
12
8
|
gem 'simple_validate'
|
13
9
|
```
|
14
10
|
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install simple_validate
|
22
|
-
|
23
11
|
## Usage
|
24
12
|
|
25
|
-
### Example
|
26
|
-
|
27
13
|
```ruby
|
28
14
|
require 'simple_validate'
|
29
15
|
|
@@ -54,10 +40,10 @@ end
|
|
54
40
|
validates_presence_of :attribute
|
55
41
|
```
|
56
42
|
|
57
|
-
###
|
43
|
+
### Type
|
58
44
|
|
59
45
|
```ruby
|
60
|
-
|
46
|
+
validates_type_of :attribute, as: :string
|
61
47
|
```
|
62
48
|
|
63
49
|
### Format
|
@@ -101,18 +87,6 @@ end
|
|
101
87
|
validates_presence_of :attribute, if: Proc.new { true }
|
102
88
|
```
|
103
89
|
|
104
|
-
|
105
|
-
## Development
|
106
|
-
|
107
|
-
`$ rake` to run the specs
|
108
|
-
|
109
|
-
|
110
|
-
## Contributing
|
111
|
-
|
112
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/nikkypx/simple_validate.
|
113
|
-
|
114
|
-
|
115
90
|
## License
|
116
91
|
|
117
92
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
118
|
-
|
data/Rakefile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
require "rubocop/rake_task"
|
4
6
|
|
5
7
|
RSpec::Core::RakeTask.new(:spec)
|
6
8
|
RuboCop::RakeTask.new
|
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "simple_validate"
|
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
|
+
require "irb"
|
11
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleValidate
|
4
|
+
module Utils
|
5
|
+
def self.camelize(input) = input.to_s.split("_").map(&:capitalize).join
|
6
|
+
def self.extract_options!(args) = args.last.is_a?(Hash) ? args.pop : {}
|
7
|
+
def self.classify(input) = Object.const_get(camelize(input))
|
8
|
+
|
9
|
+
def self.article(input)
|
10
|
+
vowels = %w[a e i o u]
|
11
|
+
vowels.include?(input[0]) ? "an" : "a"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,15 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SimpleValidate
|
2
4
|
class ValidatesFormatOf < ValidatesBase
|
3
5
|
attr_accessor :regex
|
4
6
|
|
5
7
|
def initialize(attribute, options)
|
6
|
-
|
8
|
+
@regex = options[:with]
|
7
9
|
super(attribute, options[:message] ||
|
8
|
-
|
10
|
+
"is incorrect format", options[:if] || proc { true })
|
9
11
|
end
|
10
12
|
|
11
13
|
def valid?(instance)
|
12
14
|
raise ArgumentError if regex.nil? || !regex.is_a?(Regexp)
|
15
|
+
|
13
16
|
!!(instance.send(attribute) =~ regex)
|
14
17
|
end
|
15
18
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SimpleValidate
|
2
4
|
class ValidatesLengthOf < ValidatesBase
|
3
5
|
class InvalidLengthOption < ArgumentError; end
|
@@ -13,15 +15,13 @@ module SimpleValidate
|
|
13
15
|
end
|
14
16
|
|
15
17
|
def message
|
16
|
-
@message ||=
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
'is not the correct length'
|
24
|
-
end
|
18
|
+
@message ||= case validator
|
19
|
+
when :minimum
|
20
|
+
"is too short"
|
21
|
+
when :maximum
|
22
|
+
"is too long"
|
23
|
+
else
|
24
|
+
"is not the correct length"
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
@@ -47,9 +47,7 @@ module SimpleValidate
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def valid?(instance)
|
50
|
-
if options.keys.size > 1
|
51
|
-
raise ArgumentError, 'Only one length argument can be provided'
|
52
|
-
end
|
50
|
+
raise ArgumentError, "Only one length argument can be provided" if options.keys.size > 1
|
53
51
|
|
54
52
|
unless VALID_LENGTH_OPTIONS.include?(options.keys.first)
|
55
53
|
raise InvalidLengthOption, "Invalid length option given #{options.keys}"
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module SimpleValidate
|
4
4
|
class ValidatesSetBase < ValidatesBase
|
@@ -8,7 +8,7 @@ module SimpleValidate
|
|
8
8
|
self.options = options
|
9
9
|
self.set = Set.new(Array(options[:in]).map(&:to_s))
|
10
10
|
super(attribute, options[:message] ||
|
11
|
-
|
11
|
+
"breaks inclusion/exclusion rules", options[:if] || proc { true })
|
12
12
|
end
|
13
13
|
end
|
14
14
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SimpleValidate
|
4
|
+
class ValidatesTypeOf < ValidatesBase
|
5
|
+
SUPPORTED_TYPES = %i[string integer float].freeze
|
6
|
+
|
7
|
+
def initialize(attribute, options)
|
8
|
+
@type = options[:as]
|
9
|
+
|
10
|
+
raise ArgumentError unless @type && SUPPORTED_TYPES.include?(@type)
|
11
|
+
|
12
|
+
@klass = Utils.classify(options[:as])
|
13
|
+
|
14
|
+
super(attribute, options[:message] ||
|
15
|
+
"must be #{Utils.article(@type)} #{@type}", options[:if] || proc { true })
|
16
|
+
end
|
17
|
+
|
18
|
+
def valid?(instance)
|
19
|
+
instance.send(attribute).is_a?(@klass)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/simple_validate.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
%w[base
|
2
4
|
presence_of
|
3
5
|
format_of
|
4
|
-
|
6
|
+
type_of
|
5
7
|
length_of
|
6
8
|
set_base
|
7
9
|
inclusion_of
|
@@ -9,9 +11,9 @@
|
|
9
11
|
require "simple_validate/validates_#{validation}"
|
10
12
|
end
|
11
13
|
|
12
|
-
require
|
13
|
-
require
|
14
|
-
require
|
14
|
+
require "simple_validate/errors"
|
15
|
+
require "simple_validate/version"
|
16
|
+
require "simple_validate/utils"
|
15
17
|
|
16
18
|
module SimpleValidate
|
17
19
|
def self.included(klass)
|
@@ -36,7 +38,7 @@ module SimpleValidate
|
|
36
38
|
module ClassMethods
|
37
39
|
def method_missing(method, *args, &block)
|
38
40
|
if respond_to?(method)
|
39
|
-
add_validations(args, const_get(method
|
41
|
+
add_validations(args, const_get(Utils.camelize(method)))
|
40
42
|
else
|
41
43
|
super
|
42
44
|
end
|
@@ -46,7 +48,7 @@ module SimpleValidate
|
|
46
48
|
method.to_s =~ /(validates_
|
47
49
|
(format|
|
48
50
|
presence|
|
49
|
-
|
51
|
+
type|
|
50
52
|
inclusion|
|
51
53
|
exclusion|
|
52
54
|
length)_of)
|
@@ -54,7 +56,7 @@ module SimpleValidate
|
|
54
56
|
end
|
55
57
|
|
56
58
|
def add_validations(args, klass)
|
57
|
-
options =
|
59
|
+
options = Utils.extract_options! args
|
58
60
|
args.each do |attr|
|
59
61
|
validations << klass.new(attr, options)
|
60
62
|
end
|
data/simple_validate.gemspec
CHANGED
@@ -1,23 +1,24 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
2
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
-
require
|
5
|
+
require "simple_validate/version"
|
4
6
|
|
5
7
|
Gem::Specification.new do |spec|
|
6
|
-
spec.name =
|
8
|
+
spec.name = "simple_validate"
|
7
9
|
spec.version = SimpleValidate::VERSION
|
8
|
-
spec.authors = [
|
9
|
-
spec.email = [
|
10
|
+
spec.authors = ["Nick Palaniuk"]
|
11
|
+
spec.email = ["npalaniuk@gmail.com"]
|
10
12
|
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
13
|
+
spec.summary = "PORO validation mixin with no deps"
|
14
|
+
spec.description = "PORO validation mixin with no deps"
|
15
|
+
spec.homepage = "https://github.com/nikkypx/simple_validate"
|
16
|
+
spec.license = "MIT"
|
17
|
+
spec.required_ruby_version = ">= 3.1"
|
15
18
|
|
16
|
-
spec.files
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
20
|
f.match(%r{^(test|spec|features)/})
|
18
21
|
end
|
19
22
|
|
20
|
-
spec.require_paths = [
|
21
|
-
spec.add_dependency 'activesupport', '~> 5.0'
|
22
|
-
spec.add_development_dependency 'bundler'
|
23
|
+
spec.require_paths = ["lib"]
|
23
24
|
end
|
metadata
CHANGED
@@ -1,44 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_validate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Palaniuk
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
name: activesupport
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '5.0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '5.0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: bundler
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - ">="
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - ">="
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
|
-
description: Validations for any ruby object
|
11
|
+
date: 2023-08-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: PORO validation mixin with no deps
|
42
14
|
email:
|
43
15
|
- npalaniuk@gmail.com
|
44
16
|
executables: []
|
@@ -47,29 +19,32 @@ extra_rdoc_files: []
|
|
47
19
|
files:
|
48
20
|
- ".gitignore"
|
49
21
|
- ".rspec"
|
22
|
+
- ".rspec_status"
|
50
23
|
- ".rubocop.yml"
|
51
|
-
- ".travis.yml"
|
52
24
|
- Gemfile
|
53
25
|
- LICENSE.txt
|
54
26
|
- README.md
|
55
27
|
- Rakefile
|
28
|
+
- bin/console
|
29
|
+
- bin/setup
|
56
30
|
- lib/simple_validate.rb
|
57
31
|
- lib/simple_validate/errors.rb
|
32
|
+
- lib/simple_validate/utils.rb
|
58
33
|
- lib/simple_validate/validates_base.rb
|
59
34
|
- lib/simple_validate/validates_exclusion_of.rb
|
60
35
|
- lib/simple_validate/validates_format_of.rb
|
61
36
|
- lib/simple_validate/validates_inclusion_of.rb
|
62
37
|
- lib/simple_validate/validates_length_of.rb
|
63
|
-
- lib/simple_validate/validates_numericality_of.rb
|
64
38
|
- lib/simple_validate/validates_presence_of.rb
|
65
39
|
- lib/simple_validate/validates_set_base.rb
|
40
|
+
- lib/simple_validate/validates_type_of.rb
|
66
41
|
- lib/simple_validate/version.rb
|
67
42
|
- simple_validate.gemspec
|
68
43
|
homepage: https://github.com/nikkypx/simple_validate
|
69
44
|
licenses:
|
70
45
|
- MIT
|
71
46
|
metadata: {}
|
72
|
-
post_install_message:
|
47
|
+
post_install_message:
|
73
48
|
rdoc_options: []
|
74
49
|
require_paths:
|
75
50
|
- lib
|
@@ -77,16 +52,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
52
|
requirements:
|
78
53
|
- - ">="
|
79
54
|
- !ruby/object:Gem::Version
|
80
|
-
version: '
|
55
|
+
version: '3.1'
|
81
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
57
|
requirements:
|
83
58
|
- - ">="
|
84
59
|
- !ruby/object:Gem::Version
|
85
60
|
version: '0'
|
86
61
|
requirements: []
|
87
|
-
|
88
|
-
|
89
|
-
signing_key:
|
62
|
+
rubygems_version: 3.4.19
|
63
|
+
signing_key:
|
90
64
|
specification_version: 4
|
91
|
-
summary:
|
65
|
+
summary: PORO validation mixin with no deps
|
92
66
|
test_files: []
|
data/.travis.yml
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
module SimpleValidate
|
2
|
-
class ValidatesNumericalityOf < ValidatesBase
|
3
|
-
def initialize(attribute, options)
|
4
|
-
super(attribute, options[:message] ||
|
5
|
-
'must be a number', options[:if] || proc { true })
|
6
|
-
end
|
7
|
-
|
8
|
-
def valid?(instance)
|
9
|
-
instance.send(attribute).is_a?(Numeric)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|