validaty 0.0.4 → 0.0.6

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: 65e36ce029f6e67e16945db50fc428805cae04fa02ddf8e0b84681f1e205dbf1
4
- data.tar.gz: f111f5e64cf1f3378d10f027f543b9b924bfeb6698186c7c312ca6e81b36d301
3
+ metadata.gz: f17c8fe7d65b9d0717e3c22184c4740b231a1914334423c8c4ac3a3199723768
4
+ data.tar.gz: d59df6488fe521445b8190e15b22dd5c2911744870e08670d188076b592d0b26
5
5
  SHA512:
6
- metadata.gz: 0f5b48d0b03e45217771628c252fe649776c41c77195e9ad031ffbfbfacd578142bfeac1ef05b338a33f5ff43af5186bda61d916d9f55c007f92574fd72fb974
7
- data.tar.gz: 60b48deb8cda274af36b38f5b0d4222c4645f1ad8e7208aff439795c59ce9a59f75a430f81b65601b01d2fef9205f2217823c807ba449dc42a161f9504317e49
6
+ metadata.gz: afa2e5f89f0edfbec8c8605d6622e3c5922d8985d6e2309c907e036166ac7364911c676d424098a7615298d931214d4213f6c9302170d7ac115c2c9bd6ba2c6a
7
+ data.tar.gz: d46187d5583e7bc3a0795bd0f1f9a6b64ca816b4587051217c841146abf579b5cddc2c3a5e49527f8dfb3f3df596d9199f53acbab1f35e4e7df1acbadab15552
data/.rubocop.yml CHANGED
@@ -1,7 +1,4 @@
1
1
  require:
2
- - rubocop-rails
3
- - rubocop-factory_bot
4
- - rubocop-capybara
5
2
  - rubocop-rspec
6
3
  - rubocop-rake
7
4
 
@@ -10,5 +7,7 @@ inherit_gem:
10
7
 
11
8
  AllCops:
12
9
  NewCops: enable
13
- TargetRubyVersion: 3.3
14
- TargetRailsVersion: 7.1
10
+ TargetRubyVersion: 3.4
11
+
12
+ RSpec/MultipleExpectations:
13
+ Max: 2
data/Makefile CHANGED
@@ -6,3 +6,15 @@ build:
6
6
  publish:
7
7
  @echo "gem push ${GEM_TO_PUSH}"
8
8
  @gem push ${GEM_TO_PUSH}
9
+
10
+ ci:
11
+ make rubocop rspec
12
+
13
+ rspec:
14
+ bin/rspec
15
+
16
+ rubocop:
17
+ bin/rubocop
18
+
19
+ rubocop-fix:
20
+ bin/rubocop -A
data/README.md CHANGED
@@ -7,7 +7,7 @@ Validaty has basic validations for Rails application
7
7
  Add this line to your application's Gemfile:
8
8
 
9
9
  ```ru
10
- gem "validaty", "~> 0.0.3"
10
+ gem "validaty", "~> 0.0.5"
11
11
  ```
12
12
 
13
13
  And then execute:
@@ -28,7 +28,9 @@ Imagine this schema:
28
28
 
29
29
  ```rb
30
30
  create_table "pixes", force: :cascade do |t|
31
- t.uuid "public_id", null: false
31
+ t.uuid "pid", null: false
32
+ t.string "name", null: false
33
+ t.string "zip_code", null: false
32
34
  t.integer "kind"
33
35
  t.string "key", null: false
34
36
  t.string "url", null: false
@@ -45,12 +47,14 @@ You should validate this way:
45
47
  class Pix < ApplicationRecord
46
48
  enum :kind, {cpf: 0, cnpj: 1, email: 2, phone: 3, evp: 4}
47
49
 
48
- validates :public_id, uuid: true
50
+ validates :pid, uuid: true
51
+ validates :name, presence: true, word_count: {min: 3, max: 10}
52
+ validates :zip_code, brazilian_zip_code: true
49
53
  validates :kind, presence: true
50
54
  validates :key, cpf: true, if: :cpf?
51
55
  validates :key, cnpj: true, if: :cnpj?
52
56
  validates :key, email: true, if: :email?
53
- validates :key, phone: {country_code: :phone_country}, if: :phone?
57
+ validates :key, phone: {country_calling_code: :calling_code}, if: :phone?
54
58
  validates :key, uuid: true, if: :evp?
55
59
  validates :accepted, boolean: true
56
60
  validates :schedule_date, date: true
@@ -59,41 +63,10 @@ class Pix < ApplicationRecord
59
63
  # OR
60
64
  validates :url, presence: true, url: {domain: "domain.com"}
61
65
  # OR
62
- validates :url, presence: true, url: {starts_with: "https://domain.com/path"}
66
+ validates :url, presence: true, url: {start_with: "https://domain.com/path"}
63
67
  end
64
68
  ```
65
69
 
66
- ## Translations
67
-
68
- If you using url validation with options `:domain` or `:starts_with`, you must add in your locales:
69
-
70
- ```yaml
71
- pt-BR:
72
- ...
73
- errors:
74
- messages:
75
- domain: "precisa ser do domínio %{domain}"
76
- starts_with: "precisa começar com %{start}"
77
- ```
78
-
79
- ## Troubleshooting
80
-
81
- If you need use CPF or CNPJ validation you need add 2 acronym to `config/initializers/inflections.rb`
82
-
83
- ```rb
84
- ActiveSupport::Inflector.inflections(:en) do |inflect|
85
- inflect.acronym "CPF"
86
- inflect.acronym "CNPJ"
87
- end
88
- ```
89
-
90
- If you can't add inflections in your application, the suggestion is create an initializer `config/initializers/validaty.rb` with this content:
91
-
92
- ```rb
93
- CnpjValidator = CNPJValidator
94
- CpfValidator = CPFValidator
95
- ```
96
-
97
70
  ## Contributing
98
71
 
99
72
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validaty.
@@ -0,0 +1,13 @@
1
+ class BrazilianZipCodeValidator < Validaty::AllowBlankBase
2
+ ZIP_CODE_REGEX = /\A\d{5}-?\d{3}\z/
3
+
4
+ private
5
+
6
+ def valid_value?(value)
7
+ value.match?(ZIP_CODE_REGEX)
8
+ end
9
+
10
+ def default_message_error
11
+ :invalid_zip_code
12
+ end
13
+ end
@@ -8,6 +8,6 @@ class CNPJValidator < Validaty::AllowBlankBase
8
8
  end
9
9
 
10
10
  def default_message_error
11
- :invalid
11
+ :invalid_cnpj
12
12
  end
13
13
  end
@@ -8,6 +8,6 @@ class CPFValidator < Validaty::AllowBlankBase
8
8
  end
9
9
 
10
10
  def default_message_error
11
- :invalid
11
+ :invalid_cpf
12
12
  end
13
13
  end
@@ -1,3 +1,5 @@
1
+ require "date"
2
+
1
3
  class DateValidator < Validaty::AllowBlankBase
2
4
  private
3
5
 
@@ -9,6 +11,6 @@ class DateValidator < Validaty::AllowBlankBase
9
11
  end
10
12
 
11
13
  def default_message_error
12
- :invalid
14
+ :invalid_date
13
15
  end
14
16
  end
@@ -8,6 +8,6 @@ class EmailValidator < Validaty::AllowBlankBase
8
8
  end
9
9
 
10
10
  def default_message_error
11
- :invalid
11
+ :invalid_email
12
12
  end
13
13
  end
@@ -1,6 +1,8 @@
1
1
  require "phone"
2
2
 
3
3
  class PhoneValidator < Validaty::AllowBlankBase
4
+ DEFAULT_COUNTRY_CALLING_CODE = "55"
5
+
4
6
  private
5
7
 
6
8
  def valid_value?(value)
@@ -8,14 +10,19 @@ class PhoneValidator < Validaty::AllowBlankBase
8
10
  end
9
11
 
10
12
  def prepare_params(resource, attribute, value)
11
- country_code = resource.send(options[:country_code]).to_s.gsub(/[^0-9]/, "")
13
+ country_calling_code =
14
+ resource
15
+ .send(options[:country_calling_code])
16
+ .to_s
17
+ .gsub(/[^0-9]/, "")
18
+ .presence || DEFAULT_COUNTRY_CALLING_CODE
12
19
 
13
- value = ["+", country_code, value.to_s.gsub(/[^0-9]/, "")].join
20
+ value = ["+", country_calling_code, value.to_s.gsub(/[^0-9]/, "")].join
14
21
 
15
22
  [resource, attribute, value]
16
23
  end
17
24
 
18
25
  def default_message_error
19
- :invalid
26
+ :invalid_phone
20
27
  end
21
28
  end
@@ -6,8 +6,10 @@ class UrlValidator < Validaty::AllowBlankBase
6
6
  resource.errors.add(attribute, :domain, domain: options[:domain])
7
7
  end
8
8
 
9
- if options[:starts_with].present? && !value.to_s.starts_with?(options[:starts_with])
10
- resource.errors.add(attribute, :starts_with, start: options[:starts_with])
9
+ start = options[:start_with].presence || options[:starts_with]
10
+
11
+ if start.present? && !value.to_s.start_with?(start)
12
+ resource.errors.add(attribute, :start_with, start:)
11
13
  end
12
14
 
13
15
  super
@@ -23,6 +25,6 @@ class UrlValidator < Validaty::AllowBlankBase
23
25
  end
24
26
 
25
27
  def default_message_error
26
- :invalid
28
+ :invalid_url
27
29
  end
28
30
  end
@@ -8,6 +8,6 @@ class UUIDValidator < Validaty::AllowBlankBase
8
8
  end
9
9
 
10
10
  def default_message_error
11
- :invalid
11
+ :invalid_uuid
12
12
  end
13
13
  end
@@ -0,0 +1,23 @@
1
+ class WordCountValidator < Validaty::Base
2
+ def validate_each(resource, attribute, value)
3
+ return if value.blank?
4
+
5
+ words = value.to_s.split.size
6
+ min = options[:min]
7
+ max = options[:max]
8
+
9
+ if min && words < min
10
+ resource.errors.add(attribute, :too_few_words, count: min)
11
+ end
12
+
13
+ if max && words > max
14
+ resource.errors.add(attribute, :too_many_words, count: max)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def valid_value?(value)
21
+ value.to_s.split.count >= WORDS_QUANTITY
22
+ end
23
+ end
data/lib/validaty/base.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "active_model"
2
+
1
3
  module Validaty
2
4
  class Base < ActiveModel::EachValidator
3
5
  def validate_each(resource, attribute, value)
@@ -5,7 +7,7 @@ module Validaty
5
7
 
6
8
  return if valid_value?(value)
7
9
 
8
- resource.errors.add(attribute, (options[:message] || default_message_error))
10
+ resource.errors.add(attribute, options[:message] || default_message_error)
9
11
  end
10
12
 
11
13
  private
@@ -0,0 +1,16 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ invalid_url: "is not a valid URL"
5
+ invalid_uuid: "is not a valid UUID"
6
+ invalid_phone: "is not a valid phone number"
7
+ invalid_email: "is not a valid email"
8
+ invalid_date: "is not a valid date"
9
+ invalid_cnpj: "is not a valid CNPJ"
10
+ invalid_cpf: "is not a valid CPF"
11
+ invalid_zip_code: "is not a valid CEP"
12
+ boolean: "must be true or false"
13
+ domain: "must be from the %{domain} domain"
14
+ start_with: "must start with '%{start}'"
15
+ too_few_words: "must have at least %{count} words"
16
+ too_many_words: "must have at most %{count} words"
@@ -0,0 +1,16 @@
1
+ pt-BR:
2
+ errors:
3
+ messages:
4
+ invalid_url: "não é uma URL válida"
5
+ invalid_uuid: "não é um UUID válido"
6
+ invalid_phone: "não é um telefone válido"
7
+ invalid_email: "não é um email válido"
8
+ invalid_date: "não é uma data válida"
9
+ invalid_cnpj: "não é um CNPJ válido"
10
+ invalid_cpf: "não é um CPF válido"
11
+ invalid_zip_code: "não é um CEP válido"
12
+ boolean: "deve ser verdadeiro ou falso"
13
+ domain: "precisa ser do domínio %{domain}"
14
+ start_with: "precisa começar com '%{start}'"
15
+ too_few_words: "deve ter no mínimo %{count} palavras"
16
+ too_many_words: "deve ter no máximo %{count} palavras"
@@ -1,3 +1,3 @@
1
1
  module Validaty
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/validaty.rb CHANGED
@@ -3,6 +3,7 @@ require_relative "validaty/base"
3
3
  require_relative "validaty/allow_blank_base"
4
4
 
5
5
  require_relative "validators/boolean_validator"
6
+ require_relative "validators/brazilian_zip_code_validator"
6
7
  require_relative "validators/cnpj_validator"
7
8
  require_relative "validators/cpf_validator"
8
9
  require_relative "validators/date_validator"
@@ -10,7 +11,14 @@ require_relative "validators/email_validator"
10
11
  require_relative "validators/phone_validator"
11
12
  require_relative "validators/url_validator"
12
13
  require_relative "validators/uuid_validator"
14
+ require_relative "validators/word_count_validator"
13
15
 
14
16
  module Validaty
17
+ I18n.load_path += Dir[File.join(__dir__, "validaty/locales/*.yml")]
18
+
15
19
  class Error < StandardError; end
16
20
  end
21
+
22
+ CpfValidator = CPFValidator unless defined?(CpfValidator)
23
+ CnpjValidator = CNPJValidator unless defined?(CnpjValidator)
24
+ UuidValidator = UUIDValidator unless defined?(UuidValidator)
data/validaty.gemspec CHANGED
@@ -35,4 +35,6 @@ Gem::Specification.new do |spec|
35
35
  spec.add_dependency "cpf_cnpj"
36
36
  # https://github.com/carr/phone#examples
37
37
  spec.add_dependency "phone"
38
+ # https://github.com/rails/rails/tree/v8.1.2/activemodel
39
+ spec.add_development_dependency "activemodel", ">= 0"
38
40
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validaty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lavenda Software
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-03-07 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: cpf_cnpj
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: activemodel
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
40
54
  email:
41
55
  - lavenda@lavenda.com.br
42
56
  executables: []
@@ -51,6 +65,7 @@ files:
51
65
  - README.md
52
66
  - Rakefile
53
67
  - lib/validators/boolean_validator.rb
68
+ - lib/validators/brazilian_zip_code_validator.rb
54
69
  - lib/validators/cnpj_validator.rb
55
70
  - lib/validators/cpf_validator.rb
56
71
  - lib/validators/date_validator.rb
@@ -58,9 +73,12 @@ files:
58
73
  - lib/validators/phone_validator.rb
59
74
  - lib/validators/url_validator.rb
60
75
  - lib/validators/uuid_validator.rb
76
+ - lib/validators/word_count_validator.rb
61
77
  - lib/validaty.rb
62
78
  - lib/validaty/allow_blank_base.rb
63
79
  - lib/validaty/base.rb
80
+ - lib/validaty/locales/en.yml
81
+ - lib/validaty/locales/pt-BR.yml
64
82
  - lib/validaty/version.rb
65
83
  - sig/validaty/spec.rbs
66
84
  - validaty.gemspec
@@ -86,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
104
  - !ruby/object:Gem::Version
87
105
  version: '0'
88
106
  requirements: []
89
- rubygems_version: 3.6.4
107
+ rubygems_version: 4.0.3
90
108
  specification_version: 4
91
109
  summary: Validaty has basic validations for Rails application
92
110
  test_files: []