hashy_validator 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1785de0e37317584b99155bc14ef2515d791e5fd573a41e62441926081219557
4
- data.tar.gz: 5e5e3bfd8fecc6ef53c6935c941eb137da10d4fada0f6a28408f545f64111345
3
+ metadata.gz: bb2d96afac20d3a5de238c5909ba7001ebfabdbbb790ea9b0ead248853018005
4
+ data.tar.gz: 299cb764bb7dfcd2bc69ef07cd791d8b76726b69eb0f7a12a0f28c7b1d71d55f
5
5
  SHA512:
6
- metadata.gz: 93b7190ef7035ae3b87dbd7d76ecccac1a26633ca419f3804308fe37c77c5f526fd862cbedd62f7412cc0597b1667740382033e59781b6d4fb89b2025062411d
7
- data.tar.gz: 68fabdacfccd074d3577334d83e2704431151bb64cd225ea62421155a7f3bea3a35fb9061c16435b6f3d6d3f3cfabf4a03fda08304326cf0d582dadbeebb7e1c
6
+ metadata.gz: 6d27b7aa8aae40311a1526fd5fd70c38f962e646d5a73618295f24b874e29cca0d76661111dfbeb7fccf86ccbc5e199201ac0ef4dea5293d0e8838ea2615f401
7
+ data.tar.gz: 6a2c04d418e28ad116b0d70b2b6d31c58bd0ffff4aa9aaf9e1f3648f8cacbcc4556a90894bb835a7da4b6430d20b42a1e84a9b4a51e64345e2cf4f0b971f76af
data/CHANGELOG.md ADDED
@@ -0,0 +1,36 @@
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.3](https://www.github.com/flecto-io/hashy-validator/compare/v0.1.2...v0.1.3) (2024-01-13)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * changelog ([6b8c151](https://www.github.com/flecto-io/hashy-validator/commit/6b8c15105bd085dafa6358f27f780ad4826a42e7))
14
+ * organization ([abd87b7](https://www.github.com/flecto-io/hashy-validator/commit/abd87b7a04d092b87f0d400ac087261bfc4bd07e))
15
+
16
+ ### [0.1.1](https://www.github.com/flecto-io/hashy-validator/compare/v0.1.0...v0.1.1) (2024-01-11)
17
+
18
+ ### Bug Fixes
19
+
20
+ * not initialized Class::HashValidator ([798ba63](https://www.github.com/flecto-io/hashy-validator/commit/798ba637885a4c9863351506b10d689a5f1c8a60))
21
+
22
+ ## 0.1.0 (2024-01-11)
23
+
24
+
25
+ ### Bug Fixes
26
+
27
+ * Code Climate coverage reporter is not compatible ([8df1ec3](https://www.github.com/flecto-io/hashy-validator/commit/8df1ec308a940a8f84e01456e1f9d0851c15035b))
28
+ * unique validation ([3c9a6dc](https://www.github.com/flecto-io/hashy-validator/commit/3c9a6dcc02d43cfb81103a52b3632d0d51c0329e))
29
+ - Validation lib
30
+ - Version module
31
+
32
+ ## [Unreleased]
33
+
34
+ ## Fixed
35
+
36
+ - Class::HashValidator not initialized
data/README.md ADDED
@@ -0,0 +1,60 @@
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
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'hashy_validator'
18
+ ```
19
+
20
+ <b>THIS GEM IS NOT READY FOR PRODUCTION YET!</b>
21
+
22
+ And then execute:
23
+
24
+ ```bash
25
+ $ bundle install
26
+ ```
27
+
28
+ Or install it yourself as:
29
+
30
+ ```bash
31
+ $ gem install hashy_validator
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ To leverage HashyValidator in your Rails model, follow these steps:
37
+
38
+ 1. Add the gem to your Gemfile and run `bundle install` as mentioned above.
39
+
40
+ 2. In your model, use the `validate` method to apply the `hashy_array` validation.
41
+
42
+ ```ruby
43
+ class YourModel < ApplicationRecord
44
+ validates :pricing, hashy_array: {
45
+ minutes: HashValidator.multiple('required', 'integer', 'unique'),
46
+ price_cents: HashValidator.multiple('required', 'integer')
47
+ }
48
+ end
49
+ ```
50
+
51
+ Customize each entry validators according to [HashValidator](https://github.com/jamesbrooks/hash_validator) criteria
52
+
53
+ ## Contributing
54
+
55
+ 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.
56
+ 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`.
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
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", "<= 7.2.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
+
24
+ if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3")
25
+ spec.add_development_dependency 'minitest', '>= 5.15.0', '< 5.16'
26
+ else
27
+ spec.add_development_dependency 'minitest', '>= 5.15.0'
28
+ end
29
+ end
@@ -0,0 +1,76 @@
1
+ class HashyArrayValidator < ActiveModel::EachValidator
2
+ def validate_each(record, attribute, value)
3
+ instance_value = HashyValueValidator.new(value)
4
+ unless instance_value.is_valid
5
+ record.errors.add(attribute, instance_value.reason)
6
+ return false
7
+ end
8
+
9
+ value = instance_value.value
10
+
11
+ # look for boolean and unique validator entries
12
+ unique_attrs = {}
13
+ boolean_attrs = []
14
+ validations =
15
+ # force validator keys to be strings
16
+ options.stringify_keys.map do |val_attr,val|
17
+ if (val.is_a?(HashValidator::Validations::Multiple) && val.validations.include?('boolean')) || (val.is_a?(String) && val == 'boolean')
18
+ boolean_attrs << val_attr
19
+ [val_attr, val]
20
+ elsif val.is_a?(HashValidator::Validations::Multiple) && val.validations.include?('unique')
21
+ # if unique key present, then remove that entry (since its not from HashValidator standard) and keep its history
22
+ unique_attrs[val_attr] ||= []
23
+ # we have to make a new object to remove the unique entry,
24
+ # because deleting it directly from the original object (val) would result into deleting the verification forever
25
+ new_val = HashValidator::Validations::Multiple.new(val.validations.reject{|v| v == 'unique'})
26
+ # return the value
27
+ val.validations.blank? ? nil : [val_attr, new_val]
28
+ elsif val.is_a?(String) && val == 'unique'
29
+ # same as above but substring
30
+ unique_attrs[val_attr] ||= []
31
+ nil
32
+ else
33
+ [val_attr, val]
34
+ end
35
+ end.compact.to_h
36
+
37
+ # force all array entries to have string keys
38
+ # discard keys that do not have validators
39
+ value = value.map{ |e| e.stringify_keys.slice(*validations.keys) }
40
+
41
+ # we validate each object in the array
42
+ value.each do |t|
43
+ # if boolean found as any of the validations we force value to boolean - if present
44
+ boolean_attrs.each do |boolean_attr|
45
+ t[boolean_attr] = get_boolean_value(t[boolean_attr]) if t.key?(boolean_attr)
46
+ end
47
+
48
+ # keep track of unique values and add error if needed
49
+ unique_attrs.keys.each do |unique_attr|
50
+ if unique_attrs[unique_attr].include?(t[unique_attr])
51
+ record.errors.add(attribute, "'#{unique_attr}' not unique")
52
+ else
53
+ unique_attrs[unique_attr] << t[unique_attr]
54
+ end
55
+ end
56
+
57
+ # use default hash validator
58
+ validator = HashValidator.validate(t, validations)
59
+ unless validator.valid?
60
+ validator.errors.each { |k,v| record.errors.add(attribute, "'#{k.to_s}' #{v}") }
61
+ end
62
+ end
63
+
64
+ # update the value even if errors found
65
+ # we use send write param so we also support attr_accessor attributes
66
+ record.send("#{attribute}=", value)
67
+ end
68
+
69
+ private
70
+
71
+ def get_boolean_value(value)
72
+ return true if value == true || value == 'true'
73
+ return false if value == false || value == 'false'
74
+ nil
75
+ end
76
+ end
@@ -0,0 +1,42 @@
1
+ class HashyValueValidator
2
+ def initialize(value)
3
+ @value = value.blank? ? [] : value
4
+ @valid = true
5
+ @reason = nil
6
+
7
+ check_parse_value
8
+ check_is_array
9
+
10
+ @value
11
+ end
12
+
13
+ def is_valid
14
+ @valid
15
+ end
16
+
17
+ def value
18
+ @value
19
+ end
20
+
21
+ def reason
22
+ @reason
23
+ end
24
+
25
+ private
26
+
27
+ def check_parse_value
28
+ begin
29
+ @value = JSON.parse(@value) if @value.is_a?(String)
30
+ rescue JSON::ParserError
31
+ @valid = false
32
+ @reason = :invalid
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
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HashyValidator
4
+ module Version
5
+ MAJOR = 0
6
+ MINOR = 1
7
+ PATCH = 2
8
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
9
+ end
10
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_record'
2
+ require 'hash_validator'
3
+ require_relative './hashy_validator/hashy_value_validator'
4
+ 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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flecto Team
@@ -108,7 +108,14 @@ email: dev@flecto.io
108
108
  executables: []
109
109
  extensions: []
110
110
  extra_rdoc_files: []
111
- files: []
111
+ files:
112
+ - CHANGELOG.md
113
+ - README.md
114
+ - hashy_validator.gemspec
115
+ - lib/hashy_validator.rb
116
+ - lib/hashy_validator/hashy_array_validator.rb
117
+ - lib/hashy_validator/hashy_value_validator.rb
118
+ - lib/hashy_validator/version.rb
112
119
  homepage:
113
120
  licenses: []
114
121
  metadata: {}