hashy_validator 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +11 -1
- data/README.md +8 -5
- data/hashy_validator.gemspec +3 -1
- data/lib/hashy_validator/hashy_array_validator.rb +20 -14
- data/lib/hashy_validator/hashy_value_validator.rb +33 -41
- data/lib/hashy_validator/version.rb +7 -7
- data/lib/hashy_validator.rb +6 -4
- metadata +34 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 169c5f3bf1fa5deb20aaa9788eb1dc2bef55031a026b6b2d67ee050749edd9a1
|
4
|
+
data.tar.gz: 9a583d90637b4aec761dc5dfe6a133e34c8b62cd11b53d653567e632e0a096f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f8853fec7ff9a3f67432d8ec71e68faf680f964317c6a874a12711a237faa1eeb8e7e55a4eda73c3570eb406975481529efaf9a1a8fea9d2867233f16842f1d2
|
7
|
+
data.tar.gz: a9c9f21c28c9036a943a6599173ac2a524b1ae70ac66dea91c979fe9f923330d58ab3d554e24e67ea3175ce0b9ce1effeba2e366c3616c9e08b0fa6a1be02756
|
data/CHANGELOG.md
CHANGED
@@ -5,9 +5,19 @@ All notable changes to this project will be documented in this file.
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
7
|
|
8
|
-
### [0.1.
|
8
|
+
### [0.1.4](https://www.github.com/flecto-io/hashy-validator/compare/v0.1.3...v0.1.4) (2024-08-24)
|
9
9
|
|
10
10
|
|
11
|
+
### Bug Fixes
|
12
|
+
|
13
|
+
* Accepts dependencies version of activerecord lower than 8 ([eab4692](https://www.github.com/flecto-io/hashy-validator/commit/eab46925196ada0db0c408037cdf90b9ce4dacca))
|
14
|
+
|
15
|
+
### [0.1.2](https://www.github.com/flecto-io/hashy-validator/compare/v0.1.2...v0.1.3) (2024-01-13)
|
16
|
+
|
17
|
+
### Changes
|
18
|
+
|
19
|
+
* Production ready
|
20
|
+
|
11
21
|
### Bug Fixes
|
12
22
|
|
13
23
|
* changelog ([6b8c151](https://www.github.com/flecto-io/hashy-validator/commit/6b8c15105bd085dafa6358f27f780ad4826a42e7))
|
data/README.md
CHANGED
@@ -8,7 +8,6 @@
|
|
8
8
|
HashyValidator is a custom Ruby on Rails validator designed to validate an array of hashes based on [HashValidator](https://github.com/jamesbrooks/hash_validator) criteria but also the following new criteria:
|
9
9
|
- `unique`: A value within each hash that has to be unique across the whole array
|
10
10
|
|
11
|
-
|
12
11
|
## Installation
|
13
12
|
|
14
13
|
Add this line to your application's Gemfile:
|
@@ -17,8 +16,6 @@ Add this line to your application's Gemfile:
|
|
17
16
|
gem 'hashy_validator'
|
18
17
|
```
|
19
18
|
|
20
|
-
<b>THIS GEM IS NOT READY FOR PRODUCTION YET!</b>
|
21
|
-
|
22
19
|
And then execute:
|
23
20
|
|
24
21
|
```bash
|
@@ -42,14 +39,20 @@ To leverage HashyValidator in your Rails model, follow these steps:
|
|
42
39
|
```ruby
|
43
40
|
class YourModel < ApplicationRecord
|
44
41
|
validates :pricing, hashy_array: {
|
45
|
-
minutes: HashValidator.multiple('
|
46
|
-
price_cents: HashValidator.multiple('
|
42
|
+
minutes: HashValidator.multiple('integer', 'unique'),
|
43
|
+
price_cents: HashValidator.multiple('integer')
|
47
44
|
}
|
48
45
|
end
|
49
46
|
```
|
50
47
|
|
51
48
|
Customize each entry validators according to [HashValidator](https://github.com/jamesbrooks/hash_validator) criteria
|
52
49
|
|
50
|
+
# Testing
|
51
|
+
|
52
|
+
```bash
|
53
|
+
rake test
|
54
|
+
```
|
55
|
+
|
53
56
|
## Contributing
|
54
57
|
|
55
58
|
Bug reports and pull requests are welcome. This project is intended to be a safe, welcoming space for collaboration. To ease up contribution we provide a VSCode _devcontainer_ to run the project in a container.
|
data/hashy_validator.gemspec
CHANGED
@@ -14,12 +14,14 @@ Gem::Specification.new do |spec|
|
|
14
14
|
spec.require_paths = ["lib"]
|
15
15
|
spec.files = Dir["{lib}/**/*"] + ["README.md", "CHANGELOG.md", "hashy_validator.gemspec"]
|
16
16
|
|
17
|
-
spec.add_dependency "activerecord", ">= 6.0.0", "
|
17
|
+
spec.add_dependency "activerecord", ">= 6.0.0", "< 8.0.0"
|
18
18
|
spec.add_dependency "hash_validator", "~> 1.1"
|
19
19
|
|
20
20
|
spec.add_development_dependency 'sqlite3', '~> 1.4'
|
21
21
|
spec.add_development_dependency 'rake', '~> 13.1.0'
|
22
22
|
spec.add_development_dependency 'simplecov', '0.17.1'
|
23
|
+
spec.add_development_dependency 'rubocop', '~> 1.59'
|
24
|
+
spec.add_development_dependency 'rubocop-shopify', '~> 2.14'
|
23
25
|
|
24
26
|
if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3")
|
25
27
|
spec.add_development_dependency 'minitest', '>= 5.15.0', '< 5.16'
|
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class HashyArrayValidator < ActiveModel::EachValidator
|
2
4
|
def validate_each(record, attribute, value)
|
3
5
|
instance_value = HashyValueValidator.new(value)
|
4
|
-
unless instance_value.
|
6
|
+
unless instance_value.valid?
|
5
7
|
record.errors.add(attribute, instance_value.reason)
|
6
8
|
return false
|
7
9
|
end
|
@@ -13,19 +15,22 @@ class HashyArrayValidator < ActiveModel::EachValidator
|
|
13
15
|
boolean_attrs = []
|
14
16
|
validations =
|
15
17
|
# force validator keys to be strings
|
16
|
-
options.stringify_keys.map do |val_attr,val|
|
17
|
-
|
18
|
+
options.stringify_keys.map do |val_attr, val|
|
19
|
+
is_multiple = val.is_a?(HashValidator::Validations::Multiple)
|
20
|
+
if (is_multiple && val.validations.include?("boolean")) || (val.is_a?(String) && val == "boolean")
|
18
21
|
boolean_attrs << val_attr
|
19
22
|
[val_attr, val]
|
20
|
-
elsif
|
21
|
-
# if unique key present, then remove that entry
|
23
|
+
elsif is_multiple && val.validations.include?("unique")
|
24
|
+
# if unique key present, then remove that entry
|
25
|
+
# (since its not from HashValidator standard) and keep its history
|
22
26
|
unique_attrs[val_attr] ||= []
|
23
27
|
# we have to make a new object to remove the unique entry,
|
24
|
-
# because deleting it directly from the original object
|
25
|
-
|
28
|
+
# because deleting it directly from the original object
|
29
|
+
# (val) would result into deleting the verification forever
|
30
|
+
new_val = HashValidator::Validations::Multiple.new(val.validations.reject { |v| v == "unique" })
|
26
31
|
# return the value
|
27
32
|
val.validations.blank? ? nil : [val_attr, new_val]
|
28
|
-
elsif val.is_a?(String) && val ==
|
33
|
+
elsif val.is_a?(String) && val == "unique"
|
29
34
|
# same as above but substring
|
30
35
|
unique_attrs[val_attr] ||= []
|
31
36
|
nil
|
@@ -36,7 +41,7 @@ class HashyArrayValidator < ActiveModel::EachValidator
|
|
36
41
|
|
37
42
|
# force all array entries to have string keys
|
38
43
|
# discard keys that do not have validators
|
39
|
-
value = value.map{ |e| e.stringify_keys.slice(*validations.keys) }
|
44
|
+
value = value.map { |e| e.stringify_keys.slice(*validations.keys) }
|
40
45
|
|
41
46
|
# we validate each object in the array
|
42
47
|
value.each do |t|
|
@@ -46,7 +51,7 @@ class HashyArrayValidator < ActiveModel::EachValidator
|
|
46
51
|
end
|
47
52
|
|
48
53
|
# keep track of unique values and add error if needed
|
49
|
-
unique_attrs.
|
54
|
+
unique_attrs.each_key do |unique_attr|
|
50
55
|
if unique_attrs[unique_attr].include?(t[unique_attr])
|
51
56
|
record.errors.add(attribute, "'#{unique_attr}' not unique")
|
52
57
|
else
|
@@ -57,7 +62,7 @@ class HashyArrayValidator < ActiveModel::EachValidator
|
|
57
62
|
# use default hash validator
|
58
63
|
validator = HashValidator.validate(t, validations)
|
59
64
|
unless validator.valid?
|
60
|
-
validator.errors.each { |k,v| record.errors.add(attribute, "'#{k
|
65
|
+
validator.errors.each { |k, v| record.errors.add(attribute, "'#{k}' #{v}") }
|
61
66
|
end
|
62
67
|
end
|
63
68
|
|
@@ -66,11 +71,12 @@ class HashyArrayValidator < ActiveModel::EachValidator
|
|
66
71
|
record.send("#{attribute}=", value)
|
67
72
|
end
|
68
73
|
|
69
|
-
|
74
|
+
private
|
70
75
|
|
71
76
|
def get_boolean_value(value)
|
72
|
-
return true if
|
73
|
-
return false if
|
77
|
+
return true if [true, "true"].include?(value)
|
78
|
+
return false if [false, "false"].include?(value)
|
79
|
+
|
74
80
|
nil
|
75
81
|
end
|
76
82
|
end
|
@@ -1,42 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
class HashyValueValidator
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def check_is_array
|
37
|
-
unless @value.is_a?(Array)
|
38
|
-
@valid = false
|
39
|
-
@reason = :not_an_array
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
4
|
+
def initialize(value)
|
5
|
+
@value = value.blank? ? [] : value
|
6
|
+
@valid = true
|
7
|
+
@reason = nil
|
8
|
+
|
9
|
+
check_parse_value
|
10
|
+
check_is_array
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
@valid
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :value, :reason
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def check_parse_value
|
22
|
+
@value = JSON.parse(@value) if @value.is_a?(String)
|
23
|
+
rescue JSON::ParserError
|
24
|
+
@valid = false
|
25
|
+
@reason = :invalid
|
26
|
+
end
|
27
|
+
|
28
|
+
def check_is_array
|
29
|
+
return if @value.is_a?(Array)
|
30
|
+
|
31
|
+
@valid = false
|
32
|
+
@reason = :not_an_array
|
33
|
+
end
|
34
|
+
end
|
@@ -1,10 +1,10 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module HashyValidator
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
module Version
|
5
|
+
MAJOR = 0
|
6
|
+
MINOR = 1
|
7
|
+
PATCH = 3
|
8
|
+
STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
|
9
|
+
end
|
10
|
+
end
|
data/lib/hashy_validator.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
require "hash_validator"
|
5
|
+
require_relative "hashy_validator/hashy_value_validator"
|
6
|
+
require_relative "hashy_validator/hashy_array_validator"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hashy_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Flecto Team
|
@@ -17,9 +17,9 @@ dependencies:
|
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 6.0.0
|
20
|
-
- - "
|
20
|
+
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: 8.0.0
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -27,9 +27,9 @@ dependencies:
|
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: 6.0.0
|
30
|
-
- - "
|
30
|
+
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
32
|
+
version: 8.0.0
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: hash_validator
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -86,6 +86,34 @@ dependencies:
|
|
86
86
|
- - '='
|
87
87
|
- !ruby/object:Gem::Version
|
88
88
|
version: 0.17.1
|
89
|
+
- !ruby/object:Gem::Dependency
|
90
|
+
name: rubocop
|
91
|
+
requirement: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '1.59'
|
96
|
+
type: :development
|
97
|
+
prerelease: false
|
98
|
+
version_requirements: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '1.59'
|
103
|
+
- !ruby/object:Gem::Dependency
|
104
|
+
name: rubocop-shopify
|
105
|
+
requirement: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '2.14'
|
110
|
+
type: :development
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '2.14'
|
89
117
|
- !ruby/object:Gem::Dependency
|
90
118
|
name: minitest
|
91
119
|
requirement: !ruby/object:Gem::Requirement
|
@@ -134,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
162
|
- !ruby/object:Gem::Version
|
135
163
|
version: '0'
|
136
164
|
requirements: []
|
137
|
-
rubygems_version: 3.
|
165
|
+
rubygems_version: 3.4.19
|
138
166
|
signing_key:
|
139
167
|
specification_version: 4
|
140
168
|
summary: Custom Active Model validator for validating arrays of hashes
|