hashy_validator 0.1.1 → 0.1.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1785de0e37317584b99155bc14ef2515d791e5fd573a41e62441926081219557
4
- data.tar.gz: 5e5e3bfd8fecc6ef53c6935c941eb137da10d4fada0f6a28408f545f64111345
3
+ metadata.gz: 169c5f3bf1fa5deb20aaa9788eb1dc2bef55031a026b6b2d67ee050749edd9a1
4
+ data.tar.gz: 9a583d90637b4aec761dc5dfe6a133e34c8b62cd11b53d653567e632e0a096f6
5
5
  SHA512:
6
- metadata.gz: 93b7190ef7035ae3b87dbd7d76ecccac1a26633ca419f3804308fe37c77c5f526fd862cbedd62f7412cc0597b1667740382033e59781b6d4fb89b2025062411d
7
- data.tar.gz: 68fabdacfccd074d3577334d83e2704431151bb64cd225ea62421155a7f3bea3a35fb9061c16435b6f3d6d3f3cfabf4a03fda08304326cf0d582dadbeebb7e1c
6
+ metadata.gz: f8853fec7ff9a3f67432d8ec71e68faf680f964317c6a874a12711a237faa1eeb8e7e55a4eda73c3570eb406975481529efaf9a1a8fea9d2867233f16842f1d2
7
+ data.tar.gz: a9c9f21c28c9036a943a6599173ac2a524b1ae70ac66dea91c979fe9f923330d58ab3d554e24e67ea3175ce0b9ce1effeba2e366c3616c9e08b0fa6a1be02756
data/CHANGELOG.md ADDED
@@ -0,0 +1,46 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ### [0.1.4](https://www.github.com/flecto-io/hashy-validator/compare/v0.1.3...v0.1.4) (2024-08-24)
9
+
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
+
21
+ ### Bug Fixes
22
+
23
+ * changelog ([6b8c151](https://www.github.com/flecto-io/hashy-validator/commit/6b8c15105bd085dafa6358f27f780ad4826a42e7))
24
+ * organization ([abd87b7](https://www.github.com/flecto-io/hashy-validator/commit/abd87b7a04d092b87f0d400ac087261bfc4bd07e))
25
+
26
+ ### [0.1.1](https://www.github.com/flecto-io/hashy-validator/compare/v0.1.0...v0.1.1) (2024-01-11)
27
+
28
+ ### Bug Fixes
29
+
30
+ * not initialized Class::HashValidator ([798ba63](https://www.github.com/flecto-io/hashy-validator/commit/798ba637885a4c9863351506b10d689a5f1c8a60))
31
+
32
+ ## 0.1.0 (2024-01-11)
33
+
34
+
35
+ ### Bug Fixes
36
+
37
+ * Code Climate coverage reporter is not compatible ([8df1ec3](https://www.github.com/flecto-io/hashy-validator/commit/8df1ec308a940a8f84e01456e1f9d0851c15035b))
38
+ * unique validation ([3c9a6dc](https://www.github.com/flecto-io/hashy-validator/commit/3c9a6dcc02d43cfb81103a52b3632d0d51c0329e))
39
+ - Validation lib
40
+ - Version module
41
+
42
+ ## [Unreleased]
43
+
44
+ ## Fixed
45
+
46
+ - Class::HashValidator not initialized
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ <div style="display: flex">
2
+ <a href="https://codeclimate.com/github/flecto-io/hashy-validator/maintainability"><img src="https://api.codeclimate.com/v1/badges/8818718c3f8ac08a1f05/maintainability" /></a>
3
+ <a href="https://codeclimate.com/github/flecto-io/hashy-validator/test_coverage"><img src="https://api.codeclimate.com/v1/badges/8818718c3f8ac08a1f05/test_coverage" /></a>
4
+ </div>
5
+
6
+ # HashyValidator
7
+
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
+ - `unique`: A value within each hash that has to be unique across the whole array
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'hashy_validator'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```bash
22
+ $ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```bash
28
+ $ gem install hashy_validator
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ To leverage HashyValidator in your Rails model, follow these steps:
34
+
35
+ 1. Add the gem to your Gemfile and run `bundle install` as mentioned above.
36
+
37
+ 2. In your model, use the `validate` method to apply the `hashy_array` validation.
38
+
39
+ ```ruby
40
+ class YourModel < ApplicationRecord
41
+ validates :pricing, hashy_array: {
42
+ minutes: HashValidator.multiple('integer', 'unique'),
43
+ price_cents: HashValidator.multiple('integer')
44
+ }
45
+ end
46
+ ```
47
+
48
+ Customize each entry validators according to [HashValidator](https://github.com/jamesbrooks/hash_validator) criteria
49
+
50
+ # Testing
51
+
52
+ ```bash
53
+ rake test
54
+ ```
55
+
56
+ ## Contributing
57
+
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.
59
+ Before submitting a PR do not forget to run all tests by doing `rake test` or against a single file `ruby -I. test/validation_sqlite_test.rb`.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,31 @@
1
+ require_relative "lib/hashy_validator/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'hashy_validator'
5
+ spec.version = HashyValidator::Version::STRING
6
+ spec.date = '2013-12-31'
7
+ spec.summary = 'Custom Active Model validator for validating arrays of hashes'
8
+ spec.description = 'The hashy_validator gem provides the HashyArrayValidator, a custom Active Model validator designed to validate arrays of hashes within ActiveRecord model attributes. Utilizing the HashValidator gem, on top of hash_validator gem. The gem includes a comprehensive test suite using the minitest framework.'
9
+ spec.authors = ['Flecto Team']
10
+ spec.email = 'dev@flecto.io'
11
+
12
+
13
+ spec.required_ruby_version = '>= 2.0.0'
14
+ spec.require_paths = ["lib"]
15
+ spec.files = Dir["{lib}/**/*"] + ["README.md", "CHANGELOG.md", "hashy_validator.gemspec"]
16
+
17
+ spec.add_dependency "activerecord", ">= 6.0.0", "< 8.0.0"
18
+ spec.add_dependency "hash_validator", "~> 1.1"
19
+
20
+ spec.add_development_dependency 'sqlite3', '~> 1.4'
21
+ spec.add_development_dependency 'rake', '~> 13.1.0'
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'
25
+
26
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3")
27
+ spec.add_development_dependency 'minitest', '>= 5.15.0', '< 5.16'
28
+ else
29
+ spec.add_development_dependency 'minitest', '>= 5.15.0'
30
+ end
31
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HashyArrayValidator < ActiveModel::EachValidator
4
+ def validate_each(record, attribute, value)
5
+ instance_value = HashyValueValidator.new(value)
6
+ unless instance_value.valid?
7
+ record.errors.add(attribute, instance_value.reason)
8
+ return false
9
+ end
10
+
11
+ value = instance_value.value
12
+
13
+ # look for boolean and unique validator entries
14
+ unique_attrs = {}
15
+ boolean_attrs = []
16
+ validations =
17
+ # force validator keys to be strings
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")
21
+ boolean_attrs << val_attr
22
+ [val_attr, val]
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
26
+ unique_attrs[val_attr] ||= []
27
+ # we have to make a new object to remove the unique entry,
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" })
31
+ # return the value
32
+ val.validations.blank? ? nil : [val_attr, new_val]
33
+ elsif val.is_a?(String) && val == "unique"
34
+ # same as above but substring
35
+ unique_attrs[val_attr] ||= []
36
+ nil
37
+ else
38
+ [val_attr, val]
39
+ end
40
+ end.compact.to_h
41
+
42
+ # force all array entries to have string keys
43
+ # discard keys that do not have validators
44
+ value = value.map { |e| e.stringify_keys.slice(*validations.keys) }
45
+
46
+ # we validate each object in the array
47
+ value.each do |t|
48
+ # if boolean found as any of the validations we force value to boolean - if present
49
+ boolean_attrs.each do |boolean_attr|
50
+ t[boolean_attr] = get_boolean_value(t[boolean_attr]) if t.key?(boolean_attr)
51
+ end
52
+
53
+ # keep track of unique values and add error if needed
54
+ unique_attrs.each_key do |unique_attr|
55
+ if unique_attrs[unique_attr].include?(t[unique_attr])
56
+ record.errors.add(attribute, "'#{unique_attr}' not unique")
57
+ else
58
+ unique_attrs[unique_attr] << t[unique_attr]
59
+ end
60
+ end
61
+
62
+ # use default hash validator
63
+ validator = HashValidator.validate(t, validations)
64
+ unless validator.valid?
65
+ validator.errors.each { |k, v| record.errors.add(attribute, "'#{k}' #{v}") }
66
+ end
67
+ end
68
+
69
+ # update the value even if errors found
70
+ # we use send write param so we also support attr_accessor attributes
71
+ record.send("#{attribute}=", value)
72
+ end
73
+
74
+ private
75
+
76
+ def get_boolean_value(value)
77
+ return true if [true, "true"].include?(value)
78
+ return false if [false, "false"].include?(value)
79
+
80
+ nil
81
+ end
82
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class HashyValueValidator
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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HashyValidator
4
+ module Version
5
+ MAJOR = 0
6
+ MINOR = 1
7
+ PATCH = 3
8
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
9
+ end
10
+ end
@@ -0,0 +1,6 @@
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.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: 7.2.0
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: 7.2.0
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
@@ -108,7 +136,14 @@ email: dev@flecto.io
108
136
  executables: []
109
137
  extensions: []
110
138
  extra_rdoc_files: []
111
- files: []
139
+ files:
140
+ - CHANGELOG.md
141
+ - README.md
142
+ - hashy_validator.gemspec
143
+ - lib/hashy_validator.rb
144
+ - lib/hashy_validator/hashy_array_validator.rb
145
+ - lib/hashy_validator/hashy_value_validator.rb
146
+ - lib/hashy_validator/version.rb
112
147
  homepage:
113
148
  licenses: []
114
149
  metadata: {}
@@ -127,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
162
  - !ruby/object:Gem::Version
128
163
  version: '0'
129
164
  requirements: []
130
- rubygems_version: 3.3.26
165
+ rubygems_version: 3.4.19
131
166
  signing_key:
132
167
  specification_version: 4
133
168
  summary: Custom Active Model validator for validating arrays of hashes