validaty 0.0.4 → 0.0.5

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: c94f22233330f514a413262ffea2a2f611b6a5fec8fb500fe3dc8b5615b543ae
4
+ data.tar.gz: fa246c30efb3390a4d14aa3b4b2e3b874165d74a1f92d915ddc928fca6defe2c
5
5
  SHA512:
6
- metadata.gz: 0f5b48d0b03e45217771628c252fe649776c41c77195e9ad031ffbfbfacd578142bfeac1ef05b338a33f5ff43af5186bda61d916d9f55c007f92574fd72fb974
7
- data.tar.gz: 60b48deb8cda274af36b38f5b0d4222c4645f1ad8e7208aff439795c59ce9a59f75a430f81b65601b01d2fef9205f2217823c807ba449dc42a161f9504317e49
6
+ metadata.gz: 42c89ff164a4e3ec740cd5a4e5dd2d3eb937be51e0a1cf84112ed4964475c2fe73e4ac29511a917ab63eba63803b0e390f893c5b77e999cc565d3c6f9d1a3aac
7
+ data.tar.gz: c3942fa5ec96764027dbce2ba8201d4215b35dd78f0dc3214f6973df5ef75727d386d50ddc5cecabd7e82cdcd40cc0d52fe56790731bc416277bd96d1b43de3d
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,8 @@ 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
32
33
  t.integer "kind"
33
34
  t.string "key", null: false
34
35
  t.string "url", null: false
@@ -45,12 +46,13 @@ You should validate this way:
45
46
  class Pix < ApplicationRecord
46
47
  enum :kind, {cpf: 0, cnpj: 1, email: 2, phone: 3, evp: 4}
47
48
 
48
- validates :public_id, uuid: true
49
+ validates :pid, uuid: true
50
+ validates :name, presence: true, word_count: {min: 3, max: 10}
49
51
  validates :kind, presence: true
50
52
  validates :key, cpf: true, if: :cpf?
51
53
  validates :key, cnpj: true, if: :cnpj?
52
54
  validates :key, email: true, if: :email?
53
- validates :key, phone: {country_code: :phone_country}, if: :phone?
55
+ validates :key, phone: {country_calling_code: :calling_code}, if: :phone?
54
56
  validates :key, uuid: true, if: :evp?
55
57
  validates :accepted, boolean: true
56
58
  validates :schedule_date, date: true
@@ -59,41 +61,10 @@ class Pix < ApplicationRecord
59
61
  # OR
60
62
  validates :url, presence: true, url: {domain: "domain.com"}
61
63
  # OR
62
- validates :url, presence: true, url: {starts_with: "https://domain.com/path"}
64
+ validates :url, presence: true, url: {start_with: "https://domain.com/path"}
63
65
  end
64
66
  ```
65
67
 
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
68
  ## Contributing
98
69
 
99
70
  Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validaty.
@@ -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,15 @@
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
+ boolean: "must be true or false"
12
+ domain: "must be from the %{domain} domain"
13
+ start_with: "must start with '%{start}'"
14
+ too_few_words: "must have at least %{count} words"
15
+ too_many_words: "must have at most %{count} words"
@@ -0,0 +1,15 @@
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
+ boolean: "deve ser verdadeiro ou falso"
12
+ domain: "precisa ser do domínio %{domain}"
13
+ start_with: "precisa começar com '%{start}'"
14
+ too_few_words: "deve ter no mínimo %{count} palavras"
15
+ 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.5"
3
3
  end
data/lib/validaty.rb CHANGED
@@ -10,7 +10,14 @@ require_relative "validators/email_validator"
10
10
  require_relative "validators/phone_validator"
11
11
  require_relative "validators/url_validator"
12
12
  require_relative "validators/uuid_validator"
13
+ require_relative "validators/word_count_validator"
13
14
 
14
15
  module Validaty
16
+ I18n.load_path += Dir[File.join(__dir__, "validaty/locales/*.yml")]
17
+
15
18
  class Error < StandardError; end
16
19
  end
20
+
21
+ CpfValidator = CPFValidator unless defined?(CpfValidator)
22
+ CnpjValidator = CNPJValidator unless defined?(CnpjValidator)
23
+ 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.5
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: []
@@ -58,9 +72,12 @@ files:
58
72
  - lib/validators/phone_validator.rb
59
73
  - lib/validators/url_validator.rb
60
74
  - lib/validators/uuid_validator.rb
75
+ - lib/validators/word_count_validator.rb
61
76
  - lib/validaty.rb
62
77
  - lib/validaty/allow_blank_base.rb
63
78
  - lib/validaty/base.rb
79
+ - lib/validaty/locales/en.yml
80
+ - lib/validaty/locales/pt-BR.yml
64
81
  - lib/validaty/version.rb
65
82
  - sig/validaty/spec.rbs
66
83
  - validaty.gemspec
@@ -86,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
103
  - !ruby/object:Gem::Version
87
104
  version: '0'
88
105
  requirements: []
89
- rubygems_version: 3.6.4
106
+ rubygems_version: 4.0.3
90
107
  specification_version: 4
91
108
  summary: Validaty has basic validations for Rails application
92
109
  test_files: []