simple_validate 1.2.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec_status +4 -0
- data/.rubocop.yml +10 -0
- data/Gemfile +5 -5
- data/README.md +3 -29
- data/Rakefile +4 -4
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/lib/simple_validate/utils.rb +14 -0
- data/lib/simple_validate/validates_format_of.rb +2 -2
- data/lib/simple_validate/validates_length_of.rb +8 -12
- data/lib/simple_validate/validates_set_base.rb +1 -3
- data/lib/simple_validate/validates_type_of.rb +22 -0
- data/lib/simple_validate/version.rb +1 -1
- data/lib/simple_validate.rb +7 -7
- data/simple_validate.gemspec +12 -13
- metadata +15 -40
- data/.travis.yml +0 -3
- data/lib/simple_validate/validates_numericality_of.rb +0 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
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,16 +1,16 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
source
|
3
|
+
source "https://rubygems.org"
|
4
4
|
|
5
|
-
gem
|
5
|
+
gem "rake"
|
6
6
|
|
7
7
|
group :development do
|
8
|
-
gem
|
8
|
+
gem "pry"
|
9
9
|
end
|
10
10
|
|
11
11
|
group :test do
|
12
|
-
gem
|
13
|
-
gem
|
12
|
+
gem "rspec"
|
13
|
+
gem "rubocop"
|
14
14
|
end
|
15
15
|
|
16
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,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rspec/core/rake_task"
|
5
|
+
require "rubocop/rake_task"
|
6
6
|
|
7
7
|
RSpec::Core::RakeTask.new(:spec)
|
8
8
|
RuboCop::RakeTask.new
|
9
9
|
|
10
|
-
task default: %i[spec rubocop
|
10
|
+
task default: %i[spec rubocop]
|
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
|
@@ -5,9 +5,9 @@ module SimpleValidate
|
|
5
5
|
attr_accessor :regex
|
6
6
|
|
7
7
|
def initialize(attribute, options)
|
8
|
-
|
8
|
+
@regex = options[:with]
|
9
9
|
super(attribute, options[:message] ||
|
10
|
-
|
10
|
+
"is incorrect format", options[:if] || proc { true })
|
11
11
|
end
|
12
12
|
|
13
13
|
def valid?(instance)
|
@@ -15,15 +15,13 @@ module SimpleValidate
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def message
|
18
|
-
@message ||=
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
'is not the correct length'
|
26
|
-
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"
|
27
25
|
end
|
28
26
|
end
|
29
27
|
|
@@ -49,9 +47,7 @@ module SimpleValidate
|
|
49
47
|
end
|
50
48
|
|
51
49
|
def valid?(instance)
|
52
|
-
if options.keys.size > 1
|
53
|
-
raise ArgumentError, 'Only one length argument can be provided'
|
54
|
-
end
|
50
|
+
raise ArgumentError, "Only one length argument can be provided" if options.keys.size > 1
|
55
51
|
|
56
52
|
unless VALID_LENGTH_OPTIONS.include?(options.keys.first)
|
57
53
|
raise InvalidLengthOption, "Invalid length option given #{options.keys}"
|
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'set'
|
4
|
-
|
5
3
|
module SimpleValidate
|
6
4
|
class ValidatesSetBase < ValidatesBase
|
7
5
|
attr_accessor :set, :options
|
@@ -10,7 +8,7 @@ module SimpleValidate
|
|
10
8
|
self.options = options
|
11
9
|
self.set = Set.new(Array(options[:in]).map(&:to_s))
|
12
10
|
super(attribute, options[:message] ||
|
13
|
-
|
11
|
+
"breaks inclusion/exclusion rules", options[:if] || proc { true })
|
14
12
|
end
|
15
13
|
end
|
16
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
@@ -3,7 +3,7 @@
|
|
3
3
|
%w[base
|
4
4
|
presence_of
|
5
5
|
format_of
|
6
|
-
|
6
|
+
type_of
|
7
7
|
length_of
|
8
8
|
set_base
|
9
9
|
inclusion_of
|
@@ -11,9 +11,9 @@
|
|
11
11
|
require "simple_validate/validates_#{validation}"
|
12
12
|
end
|
13
13
|
|
14
|
-
require
|
15
|
-
require
|
16
|
-
require
|
14
|
+
require "simple_validate/errors"
|
15
|
+
require "simple_validate/version"
|
16
|
+
require "simple_validate/utils"
|
17
17
|
|
18
18
|
module SimpleValidate
|
19
19
|
def self.included(klass)
|
@@ -38,7 +38,7 @@ module SimpleValidate
|
|
38
38
|
module ClassMethods
|
39
39
|
def method_missing(method, *args, &block)
|
40
40
|
if respond_to?(method)
|
41
|
-
add_validations(args, const_get(method
|
41
|
+
add_validations(args, const_get(Utils.camelize(method)))
|
42
42
|
else
|
43
43
|
super
|
44
44
|
end
|
@@ -48,7 +48,7 @@ module SimpleValidate
|
|
48
48
|
method.to_s =~ /(validates_
|
49
49
|
(format|
|
50
50
|
presence|
|
51
|
-
|
51
|
+
type|
|
52
52
|
inclusion|
|
53
53
|
exclusion|
|
54
54
|
length)_of)
|
@@ -56,7 +56,7 @@ module SimpleValidate
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def add_validations(args, klass)
|
59
|
-
options =
|
59
|
+
options = Utils.extract_options! args
|
60
60
|
args.each do |attr|
|
61
61
|
validations << klass.new(attr, options)
|
62
62
|
end
|
data/simple_validate.gemspec
CHANGED
@@ -1,25 +1,24 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
lib = File.expand_path(
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
-
require
|
5
|
+
require "simple_validate/version"
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
|
-
spec.name =
|
8
|
+
spec.name = "simple_validate"
|
9
9
|
spec.version = SimpleValidate::VERSION
|
10
|
-
spec.authors = [
|
11
|
-
spec.email = [
|
10
|
+
spec.authors = ["Nick Palaniuk"]
|
11
|
+
spec.email = ["npalaniuk@gmail.com"]
|
12
12
|
|
13
|
-
spec.summary =
|
14
|
-
spec.description =
|
15
|
-
spec.homepage =
|
16
|
-
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"
|
17
18
|
|
18
|
-
spec.files
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
20
|
f.match(%r{^(test|spec|features)/})
|
20
21
|
end
|
21
22
|
|
22
|
-
spec.require_paths = [
|
23
|
-
spec.add_dependency 'activesupport'
|
24
|
-
spec.add_development_dependency 'bundler'
|
23
|
+
spec.require_paths = ["lib"]
|
25
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: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '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,15 +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
|
-
rubygems_version: 3.
|
88
|
-
signing_key:
|
62
|
+
rubygems_version: 3.4.19
|
63
|
+
signing_key:
|
89
64
|
specification_version: 4
|
90
|
-
summary:
|
65
|
+
summary: PORO validation mixin with no deps
|
91
66
|
test_files: []
|
data/.travis.yml
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module SimpleValidate
|
4
|
-
class ValidatesNumericalityOf < ValidatesBase
|
5
|
-
def initialize(attribute, options)
|
6
|
-
super(attribute, options[:message] ||
|
7
|
-
'must be a number', options[:if] || proc { true })
|
8
|
-
end
|
9
|
-
|
10
|
-
def valid?(instance)
|
11
|
-
instance.send(attribute).is_a?(Numeric)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|