validaty 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 97d6773f78744cfee06ba993fd0ab390c5acffdfccd315b393f0ecb1eb43714a
4
+ data.tar.gz: 54dbaa718ef12d53cdf5c4f5118f8a0c40f8b61fa019b7b2769778537dda47bd
5
+ SHA512:
6
+ metadata.gz: 8a65e26f5597470893dd91141f0f2734bc7e21c36ce0b336f23bf8a7d8806f9354bdf65f478fb8d19fcd76e9574bb27a29f74d3c6c0001bd97954b2e9840d7b2
7
+ data.tar.gz: 007da7a6fb80ba6edd0a1768e8f432b89cafd977c9a25e4e57c779c812052fb8bd3ca0e443e9350bc4cd29fd2cd834b43d621e08e7d82b856f6aadd16ea996d4
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --require spec_helper
2
+ --format documentation
3
+ --force-color
4
+ --order rand
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ require:
2
+ - rubocop-rails
3
+ - rubocop-factory_bot
4
+ - rubocop-capybara
5
+ - rubocop-rspec
6
+ - rubocop-rake
7
+
8
+ inherit_gem:
9
+ standard: config/base.yml
10
+
11
+ AllCops:
12
+ NewCops: enable
13
+ TargetRubyVersion: 3.3
14
+ TargetRailsVersion: 7.1
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Lavenda Software
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
13
+ all 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
21
+ THE SOFTWARE.
data/Makefile ADDED
@@ -0,0 +1,8 @@
1
+ GEM_TO_PUSH = `ls validaty*.gem | tail -n 1`
2
+
3
+ build:
4
+ gem build validaty.gemspec
5
+
6
+ publish:
7
+ @echo "gem push ${GEM_TO_PUSH}"
8
+ @gem push ${GEM_TO_PUSH}
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Validaty
2
+
3
+ Validaty has basic validations for Rails application
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ru
10
+ gem "validaty", "~> 0.0.1"
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```bash
22
+ bundle add validaty
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Imagine this schema:
28
+
29
+ ```rb
30
+ create_table "pixes", force: :cascade do |t|
31
+ t.uuid "public_id", null: false
32
+ t.integer "kind"
33
+ t.string "key", null: false
34
+ t.boolean "accepted", null: false, default: false
35
+ t.date "schedule_date", null: false
36
+ t.datetime "created_at", null: false
37
+ t.datetime "updated_at", null: false
38
+ end
39
+ ```
40
+
41
+ You should validate this way:
42
+
43
+ ```rb
44
+ class Pix < ApplicationRecord
45
+ enum :kind, {cpf: 0, cnpj: 1, email: 2, phone: 3, evp: 4}
46
+
47
+ validates :public_id, uuid: true
48
+ validates :kind, presence: true
49
+ validates :key, cpf: true, if: :cpf?
50
+ validates :key, cnpj: true, if: :cnpj?
51
+ validates :key, email: true, if: :email?
52
+ validates :key, phone: {country_code: :phone_country}, if: :phone?
53
+ validates :key, uuid: true, if: :evp?
54
+ validates :accepted, boolean: true
55
+ validates :schedule_date, date: true
56
+ end
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validaty.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,11 @@
1
+ class BooleanValidator < Validaty::Base
2
+ private
3
+
4
+ def valid_value?(value)
5
+ [true, false].include?(value)
6
+ end
7
+
8
+ def default_message_error
9
+ :boolean
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CNPJValidator < Validaty::AllowBlankBase
2
+ private
3
+
4
+ def valid_value?(value)
5
+ CNPJ.valid?(value)
6
+ end
7
+
8
+ def default_message_error
9
+ :invalid
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class CPFValidator < Validaty::AllowBlankBase
2
+ private
3
+
4
+ def valid_value?(value)
5
+ CPF.valid?(value)
6
+ end
7
+
8
+ def default_message_error
9
+ :invalid
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ class DateValidator < Validaty::AllowBlankBase
2
+ private
3
+
4
+ def valid_value?(value)
5
+ Date.parse(value)
6
+ true
7
+ rescue Date::Error
8
+ false
9
+ end
10
+
11
+ def default_message_error
12
+ :invalid
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require "uri"
2
+
3
+ class EmailValidator < Validaty::AllowBlankBase
4
+ private
5
+
6
+ def valid_value?(value)
7
+ value.match?(URI::MailTo::EMAIL_REGEXP)
8
+ end
9
+
10
+ def default_message_error
11
+ :invalid
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require "phone"
2
+
3
+ class PhoneValidator < Validaty::AllowBlankBase
4
+ private
5
+
6
+ def valid_value?(value)
7
+ Phoner::Phone.valid?(value)
8
+ end
9
+
10
+ def prepare_params(resource, attribute, value)
11
+ country_code = resource.send(options[:country_code]).to_s.gsub(/[^0-9]/, "")
12
+
13
+ value = ["+", country_code, value.to_s.gsub(/[^0-9]/, "")].join
14
+
15
+ [resource, attribute, value]
16
+ end
17
+
18
+ def default_message_error
19
+ :invalid
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ class UUIDValidator < Validaty::AllowBlankBase
2
+ UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
3
+
4
+ private
5
+
6
+ def valid_value?(value)
7
+ value.match?(UUID_REGEX)
8
+ end
9
+
10
+ def default_message_error
11
+ :invalid
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module Validaty
2
+ class AllowBlankBase < Base
3
+ def validate_each(resource, attribute, value)
4
+ return if value.blank?
5
+
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ module Validaty
2
+ class Base < ActiveModel::EachValidator
3
+ def validate_each(resource, attribute, value)
4
+ resource, attribute, value = prepare_params(resource, attribute, value)
5
+
6
+ return if valid_value?(value)
7
+
8
+ resource.errors.add(attribute, (options[:message] || default_message_error))
9
+ end
10
+
11
+ private
12
+
13
+ def prepare_params(resource, attribute, value)
14
+ [resource, attribute, value]
15
+ end
16
+
17
+ def valid_value?(value)
18
+ raise NotImplementedError, "You must implement 'valid_value?(value)' method"
19
+ end
20
+
21
+ def default_message_error
22
+ raise NotImplementedError, "You must implement 'default_message_error' method"
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Validaty
2
+ VERSION = "0.0.1"
3
+ end
data/lib/validaty.rb ADDED
@@ -0,0 +1,15 @@
1
+ require_relative "validaty/version"
2
+ require_relative "validaty/base"
3
+ require_relative "validaty/allow_blank_base"
4
+
5
+ require_relative "validators/boolean_validator"
6
+ require_relative "validators/cnpj_validator"
7
+ require_relative "validators/cpf_validator"
8
+ require_relative "validators/date_validator"
9
+ require_relative "validators/email_validator"
10
+ require_relative "validators/phone_validator"
11
+ require_relative "validators/uuid_validator"
12
+
13
+ module Validaty
14
+ class Error < StandardError; end
15
+ end
@@ -0,0 +1,4 @@
1
+ module Validaty
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
data/validaty.gemspec ADDED
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/validaty/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "validaty"
7
+ spec.version = Validaty::VERSION
8
+ spec.authors = ["Lavenda Software"]
9
+ spec.email = ["lavenda@lavenda.com.br"]
10
+
11
+ spec.summary = "Validaty has basic validations for Rails application"
12
+ spec.homepage = "https://github.com/LavendaSoftware/validaty"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.0.0"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "#{spec.homepage}/blob/main/README.md"
20
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/commits/main"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(__dir__) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (File.expand_path(f) == __FILE__) ||
27
+ f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
28
+ end
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ # https://github.com/fnando/cpf_cnpj
35
+ spec.add_dependency "cpf_cnpj"
36
+ # https://github.com/carr/phone#examples
37
+ spec.add_dependency "phone"
38
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validaty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Lavenda Software
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cpf_cnpj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: phone
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - lavenda@lavenda.com.br
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".rubocop.yml"
50
+ - ".standard.yml"
51
+ - LICENSE.txt
52
+ - Makefile
53
+ - README.md
54
+ - Rakefile
55
+ - lib/validators/boolean_validator.rb
56
+ - lib/validators/cnpj_validator.rb
57
+ - lib/validators/cpf_validator.rb
58
+ - lib/validators/date_validator.rb
59
+ - lib/validators/email_validator.rb
60
+ - lib/validators/phone_validator.rb
61
+ - lib/validators/uuid_validator.rb
62
+ - lib/validaty.rb
63
+ - lib/validaty/allow_blank_base.rb
64
+ - lib/validaty/base.rb
65
+ - lib/validaty/version.rb
66
+ - sig/validaty/spec.rbs
67
+ - validaty.gemspec
68
+ homepage: https://github.com/LavendaSoftware/validaty
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ allowed_push_host: https://rubygems.org
73
+ homepage_uri: https://github.com/LavendaSoftware/validaty
74
+ source_code_uri: https://github.com/LavendaSoftware/validaty/blob/main/README.md
75
+ changelog_uri: https://github.com/LavendaSoftware/validaty/commits/main
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 3.0.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.5.10
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Validaty has basic validations for Rails application
95
+ test_files: []