validate-rb 0.1.0.alpha.1 → 0.1.0.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/README.md +4 -4
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/validate/version.rb +1 -1
- data/lib/validate-rb.rb +3 -0
- data/lib/validate.rb +1 -75
- data/validate-rb.gemspec +30 -0
- metadata +21 -122
- data/LICENSE +0 -21
- data/lib/validate/arguments.rb +0 -93
- data/lib/validate/assertions.rb +0 -27
- data/lib/validate/ast.rb +0 -381
- data/lib/validate/compare.rb +0 -34
- data/lib/validate/constraint.rb +0 -217
- data/lib/validate/constraints/validation_context.rb +0 -167
- data/lib/validate/constraints.rb +0 -337
- data/lib/validate/errors.rb +0 -40
- data/lib/validate/helpers.rb +0 -11
- data/lib/validate/scope.rb +0 -48
- data/lib/validate/validators/dsl.rb +0 -44
- data/lib/validate/validators.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d7f65479ecf94e4e15474fd5f86ca4bd8b46a4260b0a1e77b3d666ca0e52e8d
|
4
|
+
data.tar.gz: dec398c1f408cd2bfdee3281e386c313f19d67d9cfc5ade0c3c22bf16c3b764c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32b614ffea7a9909f64a3ced25f761347b19358771f9b6579d0eaa55e7bda35c6cf30650026faa3e4149b7205cb77f5fdcfb574516ac6ad78eac59bf076e9b15
|
7
|
+
data.tar.gz: 897066f58c0f7f7197b1821902a5039279a34ac1cb3c196448a3d4a5caf2bc1e3fc45fa8fdf0919ad5ba45a09363a087afac3715d7201189b1127f31f3d6a8f9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6.6
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Validate.rb
|
1
|
+
# Validate.rb
|
2
2
|
|
3
3
|
Yummy constraint validations for Ruby
|
4
4
|
|
@@ -7,7 +7,7 @@ Yummy constraint validations for Ruby
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem '
|
10
|
+
gem 'validaterb'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -16,7 +16,7 @@ And then execute:
|
|
16
16
|
|
17
17
|
Or install it yourself as:
|
18
18
|
|
19
|
-
$ gem install
|
19
|
+
$ gem install validaterb
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
@@ -120,5 +120,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
120
120
|
|
121
121
|
## Contributing
|
122
122
|
|
123
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/gusto
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gusto-validation.
|
124
124
|
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/validate/version.rb
CHANGED
data/lib/validate-rb.rb
ADDED
data/lib/validate.rb
CHANGED
@@ -1,80 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require
|
4
|
-
require 'monitor'
|
3
|
+
require "validaterb/version"
|
5
4
|
|
6
|
-
require_relative 'validate/version'
|
7
|
-
require_relative 'validate/errors'
|
8
|
-
require_relative 'validate/constraint'
|
9
|
-
require_relative 'validate/assertions'
|
10
|
-
require_relative 'validate/arguments'
|
11
|
-
require_relative 'validate/ast'
|
12
|
-
require_relative 'validate/scope'
|
13
|
-
require_relative 'validate/helpers'
|
14
|
-
require_relative 'validate/constraints/validation_context'
|
15
|
-
require_relative 'validate/constraints'
|
16
|
-
require_relative 'validate/validators/dsl'
|
17
|
-
require_relative 'validate/validators'
|
18
|
-
require_relative 'validate/compare'
|
19
|
-
|
20
|
-
# Validate.rb can be used independently by calling {Validate.validate}
|
21
|
-
# or included in classes and modules.
|
22
|
-
#
|
23
|
-
# @example Validating an object using externally defined metadata
|
24
|
-
# Address = Struct.new(:street, :city, :state, :zip)
|
25
|
-
# Validate::Validators.define(Address) do
|
26
|
-
# attr(:street) { not_blank }
|
27
|
-
# attr(:city) { not_blank }
|
28
|
-
# attr(:state) { not_blank & length(2) }
|
29
|
-
# attr(:zip) { not_blank & match(/[0-9]{5}(\-[0-9]{4})?/) }
|
30
|
-
# end
|
31
|
-
# puts Validate.validate(Address.new)
|
32
|
-
#
|
33
|
-
# @example Validating an object using metdata defined in class
|
34
|
-
# class Address < Struct.new(:street, :city, :state, :zip)
|
35
|
-
# include Validate
|
36
|
-
# validate do
|
37
|
-
# attr(:street) { not_blank }
|
38
|
-
# attr(:city) { not_blank }
|
39
|
-
# attr(:state) { not_blank & length(2) }
|
40
|
-
# attr(:zip) { not_blank & match(/[0-9]{5}(\-[0-9]{4})?/) }
|
41
|
-
# end
|
42
|
-
# end
|
43
|
-
# puts Validate.validate(Address.new)
|
44
5
|
module Validate
|
45
|
-
# Validate an object and get constraint violations list back
|
46
|
-
#
|
47
|
-
# @param object [Object] object to validate
|
48
|
-
# @param as [Symbol, Class] (object.class) validator to use, defaults to
|
49
|
-
# object's class
|
50
|
-
#
|
51
|
-
# @return [Array<Constraint::Violation>] list of constraint violations
|
52
|
-
def self.validate(object, as: object.class)
|
53
|
-
violations = []
|
54
|
-
Scope.current
|
55
|
-
.validator(as)
|
56
|
-
.validate(Constraints::ValidationContext.root(object, violations))
|
57
|
-
violations.freeze
|
58
|
-
end
|
59
|
-
|
60
|
-
# Check if a given validator exists
|
61
|
-
#
|
62
|
-
# @param name [Symbol, Class] validator to check
|
63
|
-
#
|
64
|
-
# @return [Boolean] `true` if validator is present, `else` otherwise
|
65
|
-
def self.validator?(name)
|
66
|
-
Scope.current.validator?(name)
|
67
|
-
end
|
68
|
-
|
69
|
-
# Hook to allow for inclusion in class or module
|
70
|
-
def self.included(base)
|
71
|
-
base.extend(ClassMethods)
|
72
|
-
end
|
73
|
-
|
74
|
-
# @private
|
75
|
-
module ClassMethods
|
76
|
-
def validator(&body)
|
77
|
-
@validator ||= Validators.create(&body)
|
78
|
-
end
|
79
|
-
end
|
80
6
|
end
|
data/validate-rb.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/validate/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'validate-rb'
|
7
|
+
spec.version = Validate::VERSION
|
8
|
+
spec.authors = ['Bulat Shakirzyanov']
|
9
|
+
spec.email = ['bulat.shakirzyanov@gusto.com']
|
10
|
+
|
11
|
+
spec.summary = 'Yummy constraint validations for Ruby'
|
12
|
+
spec.description = 'Simple, powerful, and constraint-based validation'
|
13
|
+
spec.homepage = 'https://github.com/Gusto/validaterb'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
16
|
+
|
17
|
+
spec.metadata = {
|
18
|
+
'homepage_uri' => 'https://github.com/Gusto/validaterb',
|
19
|
+
'changelog_uri' => 'https://github.com/Gusto/validaterb/releases',
|
20
|
+
'source_code_uri' => 'https://github.com/Gusto/validaterb',
|
21
|
+
'bug_tracker_uri' => 'https://github.com/Gusto/validaterb/issues',
|
22
|
+
}
|
23
|
+
|
24
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.bindir = 'exe'
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ['lib']
|
30
|
+
end
|
metadata
CHANGED
@@ -1,113 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validate-rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bulat Shakirzyanov
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
12
|
-
dependencies:
|
13
|
-
- !ruby/object:Gem::Dependency
|
14
|
-
name: aruba
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :development
|
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: '2.1'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '2.1'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: cucumber
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '4.0'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '4.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: rake
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rspec
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '3.0'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '3.0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: sorbet
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
86
|
-
requirements:
|
87
|
-
- - ">="
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '0'
|
90
|
-
type: :development
|
91
|
-
prerelease: false
|
92
|
-
version_requirements: !ruby/object:Gem::Requirement
|
93
|
-
requirements:
|
94
|
-
- - ">="
|
95
|
-
- !ruby/object:Gem::Version
|
96
|
-
version: '0'
|
97
|
-
- !ruby/object:Gem::Dependency
|
98
|
-
name: yard
|
99
|
-
requirement: !ruby/object:Gem::Requirement
|
100
|
-
requirements:
|
101
|
-
- - ">="
|
102
|
-
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
104
|
-
type: :development
|
105
|
-
prerelease: false
|
106
|
-
version_requirements: !ruby/object:Gem::Requirement
|
107
|
-
requirements:
|
108
|
-
- - ">="
|
109
|
-
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
11
|
+
date: 2020-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
111
13
|
description: Simple, powerful, and constraint-based validation
|
112
14
|
email:
|
113
15
|
- bulat.shakirzyanov@gusto.com
|
@@ -115,31 +17,28 @@ executables: []
|
|
115
17
|
extensions: []
|
116
18
|
extra_rdoc_files: []
|
117
19
|
files:
|
118
|
-
-
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".ruby-version"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
119
25
|
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/validate-rb.rb
|
120
30
|
- lib/validate.rb
|
121
|
-
- lib/validate/arguments.rb
|
122
|
-
- lib/validate/assertions.rb
|
123
|
-
- lib/validate/ast.rb
|
124
|
-
- lib/validate/compare.rb
|
125
|
-
- lib/validate/constraint.rb
|
126
|
-
- lib/validate/constraints.rb
|
127
|
-
- lib/validate/constraints/validation_context.rb
|
128
|
-
- lib/validate/errors.rb
|
129
|
-
- lib/validate/helpers.rb
|
130
|
-
- lib/validate/scope.rb
|
131
|
-
- lib/validate/validators.rb
|
132
|
-
- lib/validate/validators/dsl.rb
|
133
31
|
- lib/validate/version.rb
|
134
|
-
|
32
|
+
- validate-rb.gemspec
|
33
|
+
homepage: https://github.com/Gusto/validaterb
|
135
34
|
licenses:
|
136
35
|
- MIT
|
137
36
|
metadata:
|
138
|
-
homepage_uri: https://github.com/Gusto/
|
139
|
-
changelog_uri: https://github.com/Gusto/
|
140
|
-
source_code_uri: https://github.com/Gusto/
|
141
|
-
bug_tracker_uri: https://github.com/Gusto/
|
142
|
-
post_install_message:
|
37
|
+
homepage_uri: https://github.com/Gusto/validaterb
|
38
|
+
changelog_uri: https://github.com/Gusto/validaterb/releases
|
39
|
+
source_code_uri: https://github.com/Gusto/validaterb
|
40
|
+
bug_tracker_uri: https://github.com/Gusto/validaterb/issues
|
41
|
+
post_install_message:
|
143
42
|
rdoc_options: []
|
144
43
|
require_paths:
|
145
44
|
- lib
|
@@ -155,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
54
|
version: 1.3.1
|
156
55
|
requirements: []
|
157
56
|
rubygems_version: 3.0.3
|
158
|
-
signing_key:
|
57
|
+
signing_key:
|
159
58
|
specification_version: 4
|
160
59
|
summary: Yummy constraint validations for Ruby
|
161
60
|
test_files: []
|
data/LICENSE
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2020 Gusto
|
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 all
|
13
|
-
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 THE
|
21
|
-
SOFTWARE.
|
data/lib/validate/arguments.rb
DELETED
@@ -1,93 +0,0 @@
|
|
1
|
-
module Validate
|
2
|
-
module Arguments
|
3
|
-
module ClassMethods
|
4
|
-
def method_added(method_name)
|
5
|
-
super
|
6
|
-
return if @args.empty?
|
7
|
-
|
8
|
-
method = instance_method(method_name)
|
9
|
-
guard = ArgumentsGuard.new(method, @args.dup)
|
10
|
-
|
11
|
-
@methods_guard.__send__(:define_method, method_name) do |*args, &block|
|
12
|
-
guard.send(method_name, *args, &block)
|
13
|
-
super(*args, &block)
|
14
|
-
end
|
15
|
-
ensure
|
16
|
-
@args.clear
|
17
|
-
end
|
18
|
-
|
19
|
-
def singleton_method_added(method_name)
|
20
|
-
super
|
21
|
-
return if @args.empty?
|
22
|
-
|
23
|
-
method = singleton_method(method_name)
|
24
|
-
guard = ArgumentsGuard.new(method, @args.dup)
|
25
|
-
|
26
|
-
@methods_guard.__send__(:define_singleton_method, method_name) do |*args, &block|
|
27
|
-
guard.send(method_name, *args, &block)
|
28
|
-
super(*args, &block)
|
29
|
-
end
|
30
|
-
ensure
|
31
|
-
@args.clear
|
32
|
-
end
|
33
|
-
|
34
|
-
def arg(name, &body)
|
35
|
-
if @args.include?(name)
|
36
|
-
raise Error::ArgumentError, "duplicate argument :#{name}"
|
37
|
-
end
|
38
|
-
|
39
|
-
@args[name] = Assertions.create(&body)
|
40
|
-
self
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def self.included(base)
|
45
|
-
base.extend(ClassMethods)
|
46
|
-
base.instance_exec do
|
47
|
-
@args = {}
|
48
|
-
prepend(@methods_guard = Module.new)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
class ArgumentsGuard
|
53
|
-
DEFAULT_VALUE = BasicObject.new
|
54
|
-
|
55
|
-
def initialize(method, rules)
|
56
|
-
signature = []
|
57
|
-
assertions = []
|
58
|
-
|
59
|
-
method.parameters.each do |(kind, name)|
|
60
|
-
case kind
|
61
|
-
when :req
|
62
|
-
signature << name.to_s
|
63
|
-
when :opt
|
64
|
-
signature << "#{name} = DEFAULT_VALUE"
|
65
|
-
when :rest
|
66
|
-
signature << "*#{name}"
|
67
|
-
when :keyreq
|
68
|
-
signature << "#{name}:"
|
69
|
-
when :key
|
70
|
-
signature << "#{name}: DEFAULT_VALUE"
|
71
|
-
when :keyrest
|
72
|
-
signature << "**#{name}"
|
73
|
-
when :block
|
74
|
-
signature << "&#{name}"
|
75
|
-
else
|
76
|
-
raise Error::ArgumentError, "unsupported parameter type #{kind}"
|
77
|
-
end
|
78
|
-
next unless rules.include?(name)
|
79
|
-
|
80
|
-
assertions << "@rules[:#{name}].assert(#{name}, message: 'invalid argument #{name}')"
|
81
|
-
end
|
82
|
-
|
83
|
-
singleton_class.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
|
84
|
-
def #{method.name}(#{signature.join(', ')})
|
85
|
-
#{assertions.join("\n ")}
|
86
|
-
end
|
87
|
-
RUBY
|
88
|
-
|
89
|
-
@rules = rules
|
90
|
-
end
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
data/lib/validate/assertions.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
# typed: strict
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module Validate
|
5
|
-
module Assertions
|
6
|
-
module_function
|
7
|
-
|
8
|
-
def create(*args, &block)
|
9
|
-
Assertion.new(AST::DefinitionContext.create(*args, &block))
|
10
|
-
end
|
11
|
-
|
12
|
-
class Assertion
|
13
|
-
def initialize(validation_context)
|
14
|
-
@constraints = validation_context
|
15
|
-
end
|
16
|
-
|
17
|
-
def assert(value, error_class: Error::ArgumentError, message: 'invalid value')
|
18
|
-
ctx = Constraints::ValidationContext.root(value)
|
19
|
-
@constraints.evaluate(ctx)
|
20
|
-
return value unless ctx.has_violations?
|
21
|
-
|
22
|
-
raise error_class, message,
|
23
|
-
cause: ctx.to_err
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|