validate-rb 0.1.0.pre
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.
- checksums.yaml +7 -0
- 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 +124 -0
- data/Rakefile +6 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/lib/validate-rb.rb +3 -0
- data/lib/validate.rb +6 -0
- data/lib/validate/version.rb +5 -0
- data/validate-rb.gemspec +30 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3d7f65479ecf94e4e15474fd5f86ca4bd8b46a4260b0a1e77b3d666ca0e52e8d
|
4
|
+
data.tar.gz: dec398c1f408cd2bfdee3281e386c313f19d67d9cfc5ade0c3c22bf16c3b764c
|
5
|
+
SHA512:
|
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
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
# Validate.rb
|
2
|
+
|
3
|
+
Yummy constraint validations for Ruby
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'validaterb'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install validaterb
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Defining a validator
|
24
|
+
|
25
|
+
Validators are a collection of constraints
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'validate'
|
29
|
+
|
30
|
+
# validators can be named
|
31
|
+
Validate::Validators.define(:create_user_request) do
|
32
|
+
# attr is a type of constraint that defines constraints on an attribute
|
33
|
+
attr(:username) { not_blank }
|
34
|
+
attr(:address) do
|
35
|
+
not_nil
|
36
|
+
# 'valid' constraint continues validation further down the object graph
|
37
|
+
valid
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# validators can also be defined for a specific class
|
42
|
+
Validate::Validators.define(Address) do
|
43
|
+
attr(:street) do
|
44
|
+
not_blank
|
45
|
+
is_a(String)
|
46
|
+
attr(:length) { max(255) }
|
47
|
+
end
|
48
|
+
|
49
|
+
attr(:zip) do
|
50
|
+
# constraints have default error messages, which can be changed
|
51
|
+
match(/[0-9]{5}\-[0-9]{4}/, message: '%{value.inspect} must be a zip')
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
address = Address.new(street: '123 Any Road', zip: '11223-3445')
|
56
|
+
request = CreateUser.new(username: 'janedoe',
|
57
|
+
address: address)
|
58
|
+
|
59
|
+
violations = Validate.validate(request, as: :create_user_request)
|
60
|
+
|
61
|
+
violations.group_by(&:path).each do |path, violations|
|
62
|
+
puts "#{path} is invalid:"
|
63
|
+
violations.each do |v|
|
64
|
+
puts " #{v.message}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
### Creating constraints
|
70
|
+
|
71
|
+
Constraints have properties and can be evaluated
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# constraints must have a name
|
75
|
+
Validate::Constraints.define(:not_blank) do
|
76
|
+
# evaluation can 'fail' or 'pass'
|
77
|
+
evaluate { |value| fail if value.nil? || value.empty? }
|
78
|
+
end
|
79
|
+
|
80
|
+
# 'attr' is just another constraint, like 'not_blank'
|
81
|
+
Validate::Constraints.define(:attr) do
|
82
|
+
# constraints can have options
|
83
|
+
# every constraint at least has a :message option
|
84
|
+
# constraint options can have validation constraints
|
85
|
+
option(:name) { not_blank & is_a(Symbol) }
|
86
|
+
option(:validator) { is_a(Validate::Validators::Validator) }
|
87
|
+
|
88
|
+
# by default, constraints expect **kwargs for options
|
89
|
+
# initializer can be defined to translates from arbitrary args to options map
|
90
|
+
initialize do |name, &validation_block|
|
91
|
+
{
|
92
|
+
name: name,
|
93
|
+
# validators can be created anonymously
|
94
|
+
validator: Validate::Validators.create(&validation_block)
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
evaluate do |value, ctx|
|
99
|
+
# pass constraints on non-values to support optional validation
|
100
|
+
pass if value.nil?
|
101
|
+
|
102
|
+
# fetch an option
|
103
|
+
name = options[:name]
|
104
|
+
fail unless value.respond_to?(name)
|
105
|
+
|
106
|
+
# validation context can be used to traverse object graph
|
107
|
+
# `ValidationContext#attr(attribute)` creates a `ValidationContext` for object's `attribute`
|
108
|
+
# there is also `ValidationContext#key` to validate keys in a hash, useful for ENV validation
|
109
|
+
attr_ctx = ctx.attr(name)
|
110
|
+
options[:validator]&.validate(attr_ctx)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
## Development
|
116
|
+
|
117
|
+
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.
|
118
|
+
|
119
|
+
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).
|
120
|
+
|
121
|
+
## Contributing
|
122
|
+
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/gusto-validation.
|
124
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
data/bin/setup
ADDED
data/lib/validate-rb.rb
ADDED
data/lib/validate.rb
ADDED
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
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate-rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bulat Shakirzyanov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Simple, powerful, and constraint-based validation
|
14
|
+
email:
|
15
|
+
- bulat.shakirzyanov@gusto.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".ruby-version"
|
23
|
+
- ".travis.yml"
|
24
|
+
- Gemfile
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/console
|
28
|
+
- bin/setup
|
29
|
+
- lib/validate-rb.rb
|
30
|
+
- lib/validate.rb
|
31
|
+
- lib/validate/version.rb
|
32
|
+
- validate-rb.gemspec
|
33
|
+
homepage: https://github.com/Gusto/validaterb
|
34
|
+
licenses:
|
35
|
+
- MIT
|
36
|
+
metadata:
|
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:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 2.3.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.1
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.0.3
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Yummy constraint validations for Ruby
|
60
|
+
test_files: []
|