cnpj-val 0.0.0 β 1.0.0
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 +4 -4
- data/CHANGELOG.md +18 -0
- data/LICENSE +9 -0
- data/README.md +368 -0
- data/README.pt.md +355 -0
- data/src/cnpj-val/cnpj_val.rb +37 -0
- data/src/cnpj-val/cnpj_validator.rb +171 -0
- data/src/cnpj-val/cnpj_validator_options.rb +202 -0
- data/src/cnpj-val/errors.rb +90 -0
- data/src/cnpj-val/types.rb +61 -0
- data/src/cnpj-val/version.rb +1 -1
- data/src/cnpj-val.rb +42 -4
- metadata +44 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7172a612dbe87713bafe70467d39f323ffb1383f5a6fa68b8d879e810f11d7c6
|
|
4
|
+
data.tar.gz: 21ff8816320c615b86fb9ef9b8f3b28096a181d0d020798981c004d6f9b2b0f1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bd0448664e6a04229ee79f7c2b88237c59ee9f1cb6c98fbc019f808c05ea4fb8f9f4fe9a7bc3649365a2ded5a3443bb9f0ea4cbe0399377689833017074f3376
|
|
7
|
+
data.tar.gz: 1df67a31b2f49224574cfdab299dea4f7b122b5b28979f31e6e9c99f38667bab936aed3f39faa5f319918288a73c979b90be7d1052760628ca0bb8ea74180999
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# cnpj-val
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### π Stable Version Released!
|
|
6
|
+
|
|
7
|
+
Utility module to validate CNPJ (Brazilian legal entity ID) strings. Main features:
|
|
8
|
+
|
|
9
|
+
- **Multiple interfaces**: supports `CnpjVal.cnpj_val` and `CnpjVal::CnpjValidator` with shared `CnpjVal::CnpjValidatorOptions` defaults; an `options` argument (instance or `Hash`) is never merged with keyword overrides β passing both at once raises `InvalidArgumentCombinationError`.
|
|
10
|
+
- **Alphanumeric CNPJ**: validates the [14-character alphanumeric CNPJ](https://www.gov.br/receitafederal/pt-br/assuntos/noticias/2023/julho/cnpj-alfa-numerico) (digits and `AβZ`); default `type` is `"alphanumeric"`, with optional `"numeric"` for digits-only.
|
|
11
|
+
- **Flexible input**: accepts a `String` or `Array<String>` (formatted or raw); non-significant characters are stripped per `type`.
|
|
12
|
+
- **`type` and `case_sensitive` options**: control character set and whether lowercase letters are accepted on alphanumeric input.
|
|
13
|
+
- **Structured errors**: `CnpjVal::Error` marker with misuse leaves (`TypeMismatchError`, `InvalidArgumentCombinationError`) and domain leaf (`ValidationError` under `DomainError`); invalid CNPJ data returns `false`.
|
|
14
|
+
- **Check-digit delegation**: validation uses `CnpjDV::CnpjCheckDigits` from `cnpj-dv` for eligibility rules and verifier digits.
|
|
15
|
+
- **Keyword options**: `case_sensitive:` and `type:` overrides on `CnpjVal.cnpj_val`, `CnpjValidator#initialize`, and `#is_valid` (mutually exclusive with an `options` argument).
|
|
16
|
+
- **Constants**: `CnpjVal::CNPJ_LENGTH` and `CnpjValidatorOptions::DEFAULT_CASE_SENSITIVE` / `DEFAULT_TYPE`.
|
|
17
|
+
|
|
18
|
+
For detailed usage and API reference, see the [README](./README.md).
|
data/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Julio L. Muller
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/cnpj-val)
|
|
4
|
+
[](https://rubygems.org/gems/cnpj-val)
|
|
5
|
+
[](https://www.ruby-lang.org/)
|
|
6
|
+
[](https://github.com/LacusSolutions/br-utils-ruby/actions)
|
|
7
|
+
[](https://github.com/LacusSolutions/br-utils-ruby)
|
|
8
|
+
[](https://github.com/LacusSolutions/br-utils-ruby/blob/main/LICENSE)
|
|
9
|
+
|
|
10
|
+
> π **Full support for the [new alphanumeric CNPJ format](https://github.com/user-attachments/files/23937961/calculodvcnpjalfanaumerico.pdf).**
|
|
11
|
+
|
|
12
|
+
> π [Acessar documentaΓ§Γ£o em portuguΓͺs](./README.pt.md)
|
|
13
|
+
|
|
14
|
+
A Ruby utility to validate CNPJ (Brazilian Business Tax ID) values.
|
|
15
|
+
|
|
16
|
+
## Ruby Support
|
|
17
|
+
|
|
18
|
+
|  |  |  |
|
|
19
|
+
| --- | --- | --- |
|
|
20
|
+
| Passing β | Passing β | Passing β |
|
|
21
|
+
|
|
22
|
+
## Features
|
|
23
|
+
|
|
24
|
+
- β
**Alphanumeric CNPJ**: Validates 14-character CNPJ in numeric or alphanumeric format
|
|
25
|
+
- β
**Flexible input**: Accepts `String` or `Array` of strings; array elements are concatenated in order
|
|
26
|
+
- β
**Format agnostic**: Strips non-alphanumeric characters (or non-digits when `type` is `numeric`) and optionally uppercases before validation
|
|
27
|
+
- β
**Optional case sensitivity**: When `case_sensitive` is `false`, lowercase letters are accepted for alphanumeric CNPJ
|
|
28
|
+
- β
**Per-call override model**: Instance defaults can be overridden for one `is_valid` call only
|
|
29
|
+
- β
**Error handling**: API misuse vs domain errors with a `CnpjVal::Error` marker for library-wide rescue
|
|
30
|
+
- β
**Minimal dependencies**: [`cnpj-dv`](https://rubygems.org/gems/cnpj-dv) for check-digit calculation and [`lacus-utils`](https://rubygems.org/gems/lacus-utils) for type descriptions in error messages
|
|
31
|
+
- β
**Dual API style**: Object-oriented (`CnpjVal::CnpjValidator`) and functional (`CnpjVal.cnpj_val`)
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
Install the gem directly:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
gem install cnpj-val
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Or add it to your `Gemfile` and run `bundle install`:
|
|
42
|
+
|
|
43
|
+
```ruby
|
|
44
|
+
gem 'cnpj-val'
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Require
|
|
48
|
+
|
|
49
|
+
```ruby
|
|
50
|
+
require 'cnpj-val'
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
require 'cnpj-val'
|
|
57
|
+
|
|
58
|
+
validator = CnpjVal::CnpjValidator.new
|
|
59
|
+
|
|
60
|
+
validator.is_valid('98765432000198') # => true
|
|
61
|
+
validator.is_valid('98.765.432/0001-98') # => true
|
|
62
|
+
validator.is_valid('98765432000199') # => false
|
|
63
|
+
|
|
64
|
+
validator.is_valid('1QB5UKALPYFP59') # => true (alphanumeric)
|
|
65
|
+
validator.is_valid('1QB5UKALpyfp59') # => false (default is case-sensitive)
|
|
66
|
+
validator.is_valid('1QB5UKALpyfp59', case_sensitive: false) # => true
|
|
67
|
+
|
|
68
|
+
validator.is_valid('96206256120884') # => true (numeric)
|
|
69
|
+
validator.is_valid('1QB5UKALPYFP59', type: 'numeric') # => false (letters stripped β length β 14)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Functional helper:
|
|
73
|
+
|
|
74
|
+
```ruby
|
|
75
|
+
require 'cnpj-val'
|
|
76
|
+
|
|
77
|
+
CnpjVal.cnpj_val('98765432000198') # => true
|
|
78
|
+
CnpjVal.cnpj_val('98.765.432/0001-98') # => true
|
|
79
|
+
CnpjVal.cnpj_val('98765432000199') # => false
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Usage
|
|
83
|
+
|
|
84
|
+
The main entry points are the class `CnpjVal::CnpjValidator`, the options class `CnpjVal::CnpjValidatorOptions`, and the helper `CnpjVal.cnpj_val`.
|
|
85
|
+
|
|
86
|
+
### `CnpjVal::CnpjValidator`
|
|
87
|
+
|
|
88
|
+
- **`initialize(options = nil, **keywords)`**: Optional default validation options. When `options` is given (a `CnpjVal::CnpjValidatorOptions` instance or a `Hash`) alone, it determines the default options; a `CnpjVal::CnpjValidatorOptions` instance is stored by reference (mutating it later affects future `is_valid` calls that do not pass per-call options), while a `Hash` builds a new instance. When `options` is omitted (`nil`), the default options are built exclusively from the keyword arguments (`case_sensitive:`, `type:`). Passing `options` together with any non-`nil` keyword raises `InvalidArgumentCombinationError` instead of silently ignoring the keywords. Example: `CnpjVal::CnpjValidator.new(type: 'numeric', case_sensitive: false)`.
|
|
89
|
+
- **`options`**: Returns the instanceβs `CnpjVal::CnpjValidatorOptions` (same object used internally).
|
|
90
|
+
- **`is_valid(cnpj_input, options = nil, **keywords)`**: Validates a CNPJ value.
|
|
91
|
+
|
|
92
|
+
Input is normalized to a string (arrays of strings are concatenated). When `case_sensitive` is `false`, the string is uppercased before sanitization. Characters are stripped according to `type`. If the sanitized length is not exactly **14**, the last two characters are not digits, or check digits do not match (`CnpjDV::CnpjCheckDigits` from **`cnpj-dv`**), the method returns `false` β no exception is raised for validation failure.
|
|
93
|
+
|
|
94
|
+
If the input is not a `String` or an `Array` of strings, **`CnpjVal::TypeMismatchError`** is raised.
|
|
95
|
+
|
|
96
|
+
Per-call `options` and keyword arguments are never merged: a given `options` argument alone fully overrides the instance defaults for this call; otherwise, any given keyword overrides the instance defaults for this call. When neither is given, the instance defaults are used as-is. The instance defaults are never mutated by a per-call override. Passing `options` together with any non-`nil` keyword raises `InvalidArgumentCombinationError`.
|
|
97
|
+
|
|
98
|
+
```ruby
|
|
99
|
+
require 'cnpj-val'
|
|
100
|
+
|
|
101
|
+
validator = CnpjVal::CnpjValidator.new(type: 'numeric')
|
|
102
|
+
|
|
103
|
+
validator.is_valid('98.765.432/0001-98') # => true
|
|
104
|
+
validator.is_valid('1QB5UKALPYFP59') # => false (letters stripped β length β 14)
|
|
105
|
+
validator.is_valid('1QB5UKALpyfp59', type: 'alphanumeric', case_sensitive: false) # => true
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Default options on the instance; per-call overrides:
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
require 'cnpj-val'
|
|
112
|
+
|
|
113
|
+
validator = CnpjVal::CnpjValidator.new(case_sensitive: false)
|
|
114
|
+
|
|
115
|
+
validator.is_valid('1qb5ukalpyfp59') # => true (instance defaults)
|
|
116
|
+
validator.is_valid('1qb5ukalpyfp59', case_sensitive: true) # this call only: false
|
|
117
|
+
validator.is_valid('1qb5ukalpyfp59') # => true again
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### `CnpjVal::CnpjValidatorOptions`
|
|
121
|
+
|
|
122
|
+
Holds validator settings (`case_sensitive`, `type`). Construct with an optional options `Hash` or `CnpjVal::CnpjValidatorOptions` instance, optional extra override objects (merged in order), and/or keyword arguments. Exposes accessors: `case_sensitive`, `type`.
|
|
123
|
+
|
|
124
|
+
- **`all`**: Returns a shallow, frozen `Hash` snapshot of all current options.
|
|
125
|
+
- **`set(options)`**: Updates multiple fields at once; returns `self`. Accepts a `Hash` or another `CnpjVal::CnpjValidatorOptions` instance. Omitted keys retain their current values.
|
|
126
|
+
|
|
127
|
+
```ruby
|
|
128
|
+
require 'cnpj-val'
|
|
129
|
+
|
|
130
|
+
options = CnpjVal::CnpjValidatorOptions.new(case_sensitive: false, type: 'numeric')
|
|
131
|
+
options.case_sensitive # => false
|
|
132
|
+
options.type # => "numeric"
|
|
133
|
+
options.set({ type: 'alphanumeric' }) # merge and return self
|
|
134
|
+
options.all # => frozen snapshot of current options
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Functional helper
|
|
138
|
+
|
|
139
|
+
`CnpjVal.cnpj_val` builds a new `CnpjVal::CnpjValidator` from the same constructor parameters and calls `is_valid(cnpj_input)` once. Pass either keyword arguments **or** a `Hash`/`CnpjVal::CnpjValidatorOptions` instance for options β not both (passing `options` with any non-`nil` keyword raises `InvalidArgumentCombinationError`):
|
|
140
|
+
|
|
141
|
+
```ruby
|
|
142
|
+
require 'cnpj-val'
|
|
143
|
+
|
|
144
|
+
CnpjVal.cnpj_val('98765432000198') # => true
|
|
145
|
+
CnpjVal.cnpj_val('1QB5UKALpyfp59', case_sensitive: false) # => true
|
|
146
|
+
CnpjVal.cnpj_val('1QB5UKALPYFP59', type: 'numeric') # => false
|
|
147
|
+
CnpjVal.cnpj_val('1QB5UKALpyfp59', { # Hash form
|
|
148
|
+
type: 'alphanumeric',
|
|
149
|
+
case_sensitive: false,
|
|
150
|
+
}) # => true
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Input formats
|
|
154
|
+
|
|
155
|
+
**String:** Raw digits and/or letters, or formatted CNPJ (e.g. `98.765.432/0001-98`, `1Q.B5U.KAL/PYFP-59`). Characters are stripped according to `type`; when `case_sensitive` is `false`, letters are uppercased before alphanumeric validation.
|
|
156
|
+
|
|
157
|
+
**Array of strings:** Each element must be a `String`; values are concatenated (e.g. per digit, grouped segments, or mixed with punctuation). Non-string elements raise **`CnpjVal::TypeMismatchError`**.
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
require 'cnpj-val'
|
|
161
|
+
|
|
162
|
+
CnpjVal.cnpj_val(['1', 'Q', 'B', '5', 'U', 'K', 'A', 'L', 'P', 'Y', 'F', 'P', '5', '9']) # => true
|
|
163
|
+
CnpjVal.cnpj_val(['1Q.B5U', 'KAL', 'PYFP-59']) # => true
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Validation options
|
|
167
|
+
|
|
168
|
+
| Parameter | Type | Default | Description |
|
|
169
|
+
|-----------|------|---------|-------------|
|
|
170
|
+
| `type` | `'alphanumeric'` \| `'numeric'` \| `nil` | `'alphanumeric'` | Character set after sanitization: alphanumeric (`0`β`9`, `A`β`Z`, `a`β`z`) or numeric-only (`0`β`9`) |
|
|
171
|
+
| `case_sensitive` | `Boolean`, `nil` | `true` | When `false`, lowercase letters are uppercased before alphanumeric validation |
|
|
172
|
+
|
|
173
|
+
Invalid CNPJ (wrong length after sanitization, invalid check digits, ineligible base/branch `00000000` / `0000`, repeated digits, non-numeric verifier digits) returns **`false`** β no exception is raised for validation failure.
|
|
174
|
+
|
|
175
|
+
Example with all options:
|
|
176
|
+
|
|
177
|
+
```ruby
|
|
178
|
+
require 'cnpj-val'
|
|
179
|
+
|
|
180
|
+
CnpjVal.cnpj_val(
|
|
181
|
+
'1QB5UKALpyfp59',
|
|
182
|
+
type: 'alphanumeric',
|
|
183
|
+
case_sensitive: false,
|
|
184
|
+
)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Error handling
|
|
188
|
+
|
|
189
|
+
Errors fall into two categories:
|
|
190
|
+
|
|
191
|
+
| Category | Meaning |
|
|
192
|
+
|---|---|
|
|
193
|
+
| **API misuse** | The caller invoked the library incorrectly (wrong type for the CNPJ input or an option, or an invalid argument combination). |
|
|
194
|
+
| **Domain error** | The call was structurally correct, but a value violates a business rule (invalid `type` string). |
|
|
195
|
+
|
|
196
|
+
Every custom error includes the `CnpjVal::Error` marker module. Domain failures (`ValidationError`) inherit from `CnpjVal::DomainError` (`RangeError`). Invalid CNPJ data returns `false` (it does not raise).
|
|
197
|
+
|
|
198
|
+
**Important:** passing both an `options` instance/`Hash` and any non-`nil` keyword argument raises `InvalidArgumentCombinationError`.
|
|
199
|
+
|
|
200
|
+
#### Summary
|
|
201
|
+
|
|
202
|
+
| Class | Inherits from | Category | Trigger condition |
|
|
203
|
+
|---|---|---|---|
|
|
204
|
+
| `CnpjVal::InvalidArgumentCombinationError` | `ArgumentError` (+ `include Error`) | API misuse | Both an `options` instance/`Hash` and any non-`nil` keyword argument are passed at once |
|
|
205
|
+
| `CnpjVal::TypeMismatchError` | `TypeError` (+ `include Error`) | API misuse | CNPJ input or option has the wrong data type |
|
|
206
|
+
| `CnpjVal::ValidationError` | `CnpjVal::DomainError` | Domain error | `type` is not one of the allowed values |
|
|
207
|
+
|
|
208
|
+
#### `CnpjVal::Error` (marker module)
|
|
209
|
+
|
|
210
|
+
- **Inheritance:** module marker mixed into every library error via `include` (not a class).
|
|
211
|
+
- **Category:** N/A (rescue target only) β not a failure mode by itself.
|
|
212
|
+
- **When it is raised:** Never raised directly; included by every custom error the library raises.
|
|
213
|
+
- **Example:** N/A
|
|
214
|
+
- **How to rescue it:**
|
|
215
|
+
|
|
216
|
+
```ruby
|
|
217
|
+
rescue CnpjVal::Error
|
|
218
|
+
# everything this library raises
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
#### `CnpjVal::DomainError`
|
|
222
|
+
|
|
223
|
+
- **Inheritance:** `CnpjVal::DomainError < RangeError` (includes `CnpjVal::Error`)
|
|
224
|
+
- **Category:** Domain error β ancestor for all domain failures.
|
|
225
|
+
- **When it is raised:** Not raised directly; prefer raising a leaf subclass.
|
|
226
|
+
- **Example:** Prefer `raise CnpjVal::ValidationError` over raising `DomainError` directly.
|
|
227
|
+
- **How to rescue it:**
|
|
228
|
+
|
|
229
|
+
```ruby
|
|
230
|
+
rescue CnpjVal::DomainError
|
|
231
|
+
# ValidationError and other DomainError subclasses
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### `CnpjVal::TypeMismatchError`
|
|
235
|
+
|
|
236
|
+
- **Inheritance:** `CnpjVal::TypeMismatchError < TypeError` (includes `CnpjVal::Error`)
|
|
237
|
+
- **Category:** API misuse β the caller passed a value of the wrong type.
|
|
238
|
+
- **When it is raised:** Raised when the CNPJ input is not a `String` or `Array` of strings, or when a validator option (`type`) has the wrong runtime type.
|
|
239
|
+
- **Example:**
|
|
240
|
+
|
|
241
|
+
```ruby
|
|
242
|
+
CnpjVal.cnpj_val(12_345_678_000_198) # raises CnpjVal::TypeMismatchError
|
|
243
|
+
CnpjVal.cnpj_val('98765432000198', type: 123) # raises CnpjVal::TypeMismatchError
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
- **How to rescue it:**
|
|
247
|
+
|
|
248
|
+
```ruby
|
|
249
|
+
rescue CnpjVal::TypeMismatchError
|
|
250
|
+
# this library's type-contract violation
|
|
251
|
+
|
|
252
|
+
rescue TypeError
|
|
253
|
+
# native type errors, including this library's TypeMismatchError
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
#### `CnpjVal::InvalidArgumentCombinationError`
|
|
257
|
+
|
|
258
|
+
- **Inheritance:** `CnpjVal::InvalidArgumentCombinationError < ArgumentError` (includes `CnpjVal::Error`)
|
|
259
|
+
- **Category:** API misuse β the provided arguments do not form a valid API signature.
|
|
260
|
+
- **When it is raised:** Raised when `CnpjValidator.new`, `#is_valid`, or `cnpj_val` receives both an `options` argument (instance or `Hash`) and any non-`nil` keyword argument at the same time.
|
|
261
|
+
- **Example:**
|
|
262
|
+
|
|
263
|
+
```ruby
|
|
264
|
+
begin
|
|
265
|
+
CnpjVal.cnpj_val('98765432000198', { type: 'numeric' }, case_sensitive: false)
|
|
266
|
+
rescue CnpjVal::InvalidArgumentCombinationError => e
|
|
267
|
+
puts e.message
|
|
268
|
+
# Pass either an options instance/Hash to `options`, or keyword arguments (case_sensitive:, type:), not both.
|
|
269
|
+
end
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
- **How to rescue it:**
|
|
273
|
+
|
|
274
|
+
```ruby
|
|
275
|
+
rescue CnpjVal::InvalidArgumentCombinationError
|
|
276
|
+
# this library's invalid signature combination
|
|
277
|
+
|
|
278
|
+
rescue ArgumentError
|
|
279
|
+
# native argument errors, including this library's InvalidArgumentCombinationError
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
#### `CnpjVal::ValidationError`
|
|
283
|
+
|
|
284
|
+
- **Inheritance:** `CnpjVal::ValidationError < CnpjVal::DomainError < RangeError` (includes `CnpjVal::Error`)
|
|
285
|
+
- **Category:** Domain error β a value fails a non-numeric, non-length domain rule.
|
|
286
|
+
- **When it is raised:** Raised when `type` is not one of `'alphanumeric'` or `'numeric'`.
|
|
287
|
+
- **Example:**
|
|
288
|
+
|
|
289
|
+
```ruby
|
|
290
|
+
CnpjVal.cnpj_val('98765432000198', type: 'invalid') # raises CnpjVal::ValidationError
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
- **How to rescue it:**
|
|
294
|
+
|
|
295
|
+
```ruby
|
|
296
|
+
rescue CnpjVal::ValidationError
|
|
297
|
+
# this exact domain validation failure
|
|
298
|
+
|
|
299
|
+
rescue CnpjVal::DomainError
|
|
300
|
+
# ValidationError and other DomainError subclasses
|
|
301
|
+
```
|
|
302
|
+
|
|
303
|
+
#### Rescue granularity
|
|
304
|
+
|
|
305
|
+
```ruby
|
|
306
|
+
# 1) Single native class β catches misuse errors of that kind.
|
|
307
|
+
rescue TypeError
|
|
308
|
+
# CnpjVal::TypeMismatchError and any other TypeError (library or not)
|
|
309
|
+
|
|
310
|
+
# 2) CnpjVal::DomainError β catches all business-rule violations under DomainError.
|
|
311
|
+
rescue CnpjVal::DomainError
|
|
312
|
+
# CnpjVal::ValidationError and other DomainError subclasses
|
|
313
|
+
|
|
314
|
+
# 3) CnpjVal::Error β catches everything the library raises.
|
|
315
|
+
rescue CnpjVal::Error
|
|
316
|
+
# every custom error that includes CnpjVal::Error
|
|
317
|
+
|
|
318
|
+
# 4) Specific leaf class β catches only that exact failure mode.
|
|
319
|
+
rescue CnpjVal::ValidationError
|
|
320
|
+
# only CnpjVal::ValidationError
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
Notable attributes on raised errors:
|
|
324
|
+
|
|
325
|
+
- `TypeMismatchError`: `option_name` (nil for CNPJ input), `actual_input`, `actual_type`, `expected_type`
|
|
326
|
+
- `ValidationError`: `option_name`, `actual_input`, `expected_values`
|
|
327
|
+
|
|
328
|
+
## API
|
|
329
|
+
|
|
330
|
+
### Exports
|
|
331
|
+
|
|
332
|
+
After `require 'cnpj-val'`:
|
|
333
|
+
|
|
334
|
+
- **`CnpjVal.cnpj_val`**: `(cnpj_input, options = nil, **keywords) -> Boolean` β convenience helper.
|
|
335
|
+
- **`CnpjVal::CnpjValidator`**: Class to validate CNPJ with optional default options; accepts `String` or `Array<String>` in `is_valid`.
|
|
336
|
+
- **`CnpjVal::CnpjValidatorOptions`**: Class holding options; supports merge via constructor, `set`, and keyword arguments.
|
|
337
|
+
- **`CnpjVal::CNPJ_LENGTH`**: `14` (constant).
|
|
338
|
+
- **`CnpjVal::VERSION`**: gem version string.
|
|
339
|
+
- **Type predicate**: `CnpjVal::CnpjInput` β `CnpjVal::CnpjInput.accept?(value)` / `CnpjVal::CnpjInput === value` is true only for `String` or `Array<String>`.
|
|
340
|
+
- **Type markers**: `CnpjVal::CnpjType`, `CnpjVal::CnpjValidatorOptionsInput`.
|
|
341
|
+
- **Errors**: `CnpjVal::Error`, `CnpjVal::DomainError`, `CnpjVal::InvalidArgumentCombinationError`, `CnpjVal::TypeMismatchError`, `CnpjVal::ValidationError`.
|
|
342
|
+
|
|
343
|
+
### Other available resources
|
|
344
|
+
|
|
345
|
+
- **`CnpjVal::CnpjValidatorOptions::CNPJ_LENGTH`**: `14`.
|
|
346
|
+
- **`CnpjVal::CnpjValidatorOptions::DEFAULT_CASE_SENSITIVE`**: `true`.
|
|
347
|
+
- **`CnpjVal::CnpjValidatorOptions::DEFAULT_TYPE`**: `'alphanumeric'`.
|
|
348
|
+
|
|
349
|
+
## Contribution & Support
|
|
350
|
+
|
|
351
|
+
We welcome contributions! Please see our [Contributing Guidelines](https://github.com/LacusSolutions/br-utils-ruby/blob/main/CONTRIBUTING.md) for details. If you find this project helpful, please consider:
|
|
352
|
+
|
|
353
|
+
- β Starring the repository
|
|
354
|
+
- π€ Contributing to the codebase
|
|
355
|
+
- π‘ [Suggesting new features](https://github.com/LacusSolutions/br-utils-ruby/issues)
|
|
356
|
+
- π [Reporting bugs](https://github.com/LacusSolutions/br-utils-ruby/issues)
|
|
357
|
+
|
|
358
|
+
## License
|
|
359
|
+
|
|
360
|
+
This project is licensed under the MIT License β see the [LICENSE](https://github.com/LacusSolutions/br-utils-ruby/blob/main/LICENSE) file for details.
|
|
361
|
+
|
|
362
|
+
## Changelog
|
|
363
|
+
|
|
364
|
+
See [CHANGELOG](./CHANGELOG.md) for a list of changes and version history.
|
|
365
|
+
|
|
366
|
+
---
|
|
367
|
+
|
|
368
|
+
Made with β€οΈ by [Lacus Solutions](https://github.com/LacusSolutions)
|