zod-br-tax-id
Advanced tools
Comparing version 0.0.3 to 1.0.0
# zod-br-tax-id | ||
## 1.0.0 | ||
### Major Changes | ||
- 7fb2e4d: CPF + CNPJ | ||
## 0.0.3 | ||
@@ -4,0 +10,0 @@ |
@@ -1,3 +0,15 @@ | ||
declare const hello: () => void; | ||
import { z } from 'zod'; | ||
export { hello }; | ||
type ZodCNPJ = { | ||
strict?: boolean; | ||
message?: string; | ||
}; | ||
type ZodCPF = ZodCNPJ & { | ||
alternatives?: boolean; | ||
}; | ||
declare const cpf: (options?: ZodCPF) => z.ZodType<string, z.ZodTypeDef, string>; | ||
declare const cnpj: (options?: ZodCNPJ) => z.ZodType<string, z.ZodTypeDef, string>; | ||
export { ZodCNPJ, ZodCPF, cnpj, cpf }; |
@@ -5,3 +5,17 @@ "use strict"; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __getOwnPropSymbols = Object.getOwnPropertySymbols; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __propIsEnum = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
if (__getOwnPropSymbols) | ||
for (var prop of __getOwnPropSymbols(b)) { | ||
if (__propIsEnum.call(b, prop)) | ||
__defNormalProp(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var __export = (target, all) => { | ||
@@ -24,11 +38,116 @@ for (var name in all) | ||
__export(src_exports, { | ||
hello: () => hello | ||
cnpj: () => cnpj_default, | ||
cpf: () => cpf_default | ||
}); | ||
module.exports = __toCommonJS(src_exports); | ||
var hello = () => { | ||
console.log("Hello world!"); | ||
// src/cpf/index.ts | ||
var import_zod = require("zod"); | ||
var defaultOptions = { | ||
message: "CPF inv\xE1lido", | ||
strict: false, | ||
alternatives: false | ||
}; | ||
var cpf = (options) => import_zod.z.custom((val) => { | ||
const { strict, alternatives } = __spreadValues(__spreadValues({}, defaultOptions), options); | ||
const cpfString = String(val); | ||
if (strict) | ||
return strictValidations(cpfString); | ||
if (cpfString.length > 16) | ||
return false; | ||
const noPattern = /^(\d){11}$/; | ||
const officialPattern = /^\d{3}\.\d{3}\.\d{3}-\d{2}$/; | ||
const affiliatedEntitiesPattern = /^\d{3}\.\d{3}\.\d{3} - \d{2}$/; | ||
const cicPattern = /^\d{3} \d{3} \d{3} \d{2}$/; | ||
let cpfPattern; | ||
if (alternatives) { | ||
cpfPattern = new RegExp(`^(${officialPattern.source}|${affiliatedEntitiesPattern.source}|${cicPattern.source})$`); | ||
} else { | ||
cpfPattern = officialPattern; | ||
} | ||
cpfPattern = new RegExp(`^(${noPattern.source}|${cpfPattern.source})$`); | ||
if (!cpfPattern.test(cpfString)) | ||
return false; | ||
const sanitizedCPF = cpfString.replace(/\D/g, ""); | ||
return strictValidations(sanitizedCPF); | ||
}, (options == null ? void 0 : options.message) || defaultOptions.message); | ||
var strictValidations = (digits) => { | ||
if (/^(\d)\1+$/.test(digits)) | ||
return false; | ||
if (digits.length !== 11) | ||
return false; | ||
return calculateCheckDigit(digits); | ||
}; | ||
var calculateCheckDigit = (digits) => { | ||
const validation1 = [10, 9, 8, 7, 6, 5, 4, 3, 2]; | ||
const validation2 = [...validation1].slice(0, 9).concat([11]); | ||
const getRest = (sum) => { | ||
const remainder = sum % 11; | ||
return remainder < 2 ? 0 : 11 - remainder; | ||
}; | ||
const sumForCheckDigit = (digits2, weights) => { | ||
let sum = 0; | ||
for (let i = 0; i < weights.length; i++) { | ||
sum += parseInt(digits2.charAt(i)) * weights[i]; | ||
} | ||
return getRest(sum); | ||
}; | ||
const checkDigit1 = sumForCheckDigit(digits, validation1); | ||
const checkDigit2 = sumForCheckDigit(digits, validation2); | ||
if (digits === "12345678909") | ||
console.log(checkDigit1, checkDigit2); | ||
return checkDigit1 === parseInt(digits[9]) && checkDigit2 === parseInt(digits[10]); | ||
}; | ||
var cpf_default = cpf; | ||
// src/cnpj/index.ts | ||
var import_zod2 = require("zod"); | ||
var defaultOptions2 = { | ||
message: "CNPJ inv\xE1lido", | ||
strict: false | ||
}; | ||
var cnpj = (options) => import_zod2.z.custom( | ||
(val) => { | ||
const { strict } = __spreadValues(__spreadValues({}, defaultOptions2), options); | ||
const cnpjString = String(val); | ||
if (strict) | ||
return strictValidations2(cnpjString); | ||
if (cnpjString.length > 18) | ||
return false; | ||
const cnpjPattern = /^(\d{2}\.\d{3}\.\d{3}\/\d{4}\-\d{2}|\d{14})$/; | ||
if (!cnpjPattern.test(cnpjString)) | ||
return false; | ||
const sanitizedCnpj = cnpjString.replace(/\D/g, ""); | ||
return strictValidations2(sanitizedCnpj); | ||
}, | ||
{ | ||
message: (options == null ? void 0 : options.message) || defaultOptions2.message | ||
} | ||
); | ||
var strictValidations2 = (digits) => { | ||
if (/^(\d)\1+$/.test(digits)) | ||
return false; | ||
if (digits.length !== 14) | ||
return false; | ||
return calculateCheckDigit2(digits); | ||
}; | ||
var calculateCheckDigit2 = (digits) => { | ||
let dig1 = 0; | ||
let dig2 = 0; | ||
const validation = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]; | ||
const digit = parseInt(digits.charAt(12) + digits.charAt(13)); | ||
const getRest = (dig) => dig % 11 < 2 ? 0 : 11 - dig % 11; | ||
validation.map((v, i) => { | ||
dig1 += i > 0 ? parseInt(digits.charAt(i - 1)) * v : 0; | ||
dig2 += parseInt(digits.charAt(i)) * v; | ||
}); | ||
dig1 = getRest(dig1); | ||
dig2 = getRest(dig2); | ||
return dig1 * 10 + dig2 === digit; | ||
}; | ||
var cnpj_default = cnpj; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
hello | ||
cnpj, | ||
cpf | ||
}); |
{ | ||
"name": "zod-br-tax-id", | ||
"version": "0.0.3", | ||
"description": "", | ||
"version": "1.0.0", | ||
"description": "Zod Custom Objects for Brazilian Tax Ids", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.mjs", | ||
"types": "./dist/index.d.ts", | ||
"keywords": [], | ||
"keywords": [ | ||
"zod", | ||
"validation", | ||
"cpf", | ||
"cnpj", | ||
"brasil", | ||
"brazil", | ||
"tax id", | ||
"cpf validation", | ||
"cnpj validation" | ||
], | ||
"author": "Rafael Abensur", | ||
"license": "MIT", | ||
"homepage": "https://github.com/abensur/zod-br-tax-id#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/abensur/zod-br-tax-id/" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/abensur/zod-br-tax-id/issues" | ||
}, | ||
"peerDependencies": { | ||
"zod": "^3" | ||
}, | ||
"devDependencies": { | ||
@@ -16,6 +37,7 @@ "@changesets/cli": "^2.26.2", | ||
"typescript": "^5.1.6", | ||
"vitest": "^0.34.2" | ||
"vitest": "^0.34.2", | ||
"zod": "^3.22.2" | ||
}, | ||
"scripts": { | ||
"dev": "vitest", | ||
"dev": "vitest --dir src", | ||
"test": "vitest run", | ||
@@ -22,0 +44,0 @@ "build": "tsup src/index.ts --format cjs,esm --dts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12871
9
288
0
0
1
45
0
1
6
1