-
Notifications
You must be signed in to change notification settings - Fork 32
[Fix] Update RFC validation to include legacy alphabet #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,10 +80,12 @@ const nameBlacklist = new Set([ | |
| 'RUIN', | ||
| ]); | ||
|
|
||
| // Official alphabet per SAT (Anexo 20). Includes '&' and 'Ñ'. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After thinking about this, unless there is a really good reason there should be two distinct validators for
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reply! @koblas . But making all the consumers knows which one to call?, isnt that more invasive? (because they are "just" rfc). Like: 1 - Consumer would need to apply a logic to know if the rfc is "legacy" or not, then call the correct function or 2 - Consumer would call Open to the conversation, whatever you preferred I can make the change to fit that 🙌 |
||
| const checkAlphabet = '0123456789ABCDEFGHIJKLMN&OPQRSTUVWXYZ Ñ'; | ||
| // const checkAlphabetDict: Record<string, number> = checkAlphabet | ||
| // .split('') | ||
| // .reduce((acc, c, idx) => ({ ...acc, [c]: idx }), {}); | ||
|
|
||
| // Legacy alphabet (Base 36) used in older systems. | ||
| // It excludes '&' and 'Ñ'. Space is added to support padding for companies. | ||
| const checkAlphabetLegacy = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '; | ||
|
|
||
| const impl: Validator = { | ||
| name: 'Mexican Tax Number', | ||
|
|
@@ -149,34 +151,33 @@ const impl: Validator = { | |
| } | ||
|
|
||
| const [front, check] = strings.splitAt(value, -1); | ||
|
|
||
| const sum = weightedSum(front.padStart(12, ' '), { | ||
| modulus: 11, | ||
| alphabet: checkAlphabet, | ||
| weights: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], | ||
| reverse: true, | ||
| }); | ||
|
|
||
| // const sum = value | ||
| // .substr(0, value.length - 1) | ||
| // .padStart(12, ' ') | ||
| // .split('') | ||
| // .reduce( | ||
| // (acc, c, idx) => acc + (checkAlphabetDict[c] ?? 0) * (13 - idx), | ||
| // 0, | ||
| // ); | ||
| const mod = 11 - (sum % 11); | ||
| let val; | ||
| if (mod === 11) { | ||
| val = '0'; | ||
| } else if (mod === 10) { | ||
| val = 'A'; | ||
| } else { | ||
| val = String(mod); | ||
| } | ||
|
|
||
| if (check !== val) { | ||
| return { isValid: false, error: new exceptions.InvalidChecksum() }; | ||
| const paddedInput = front.padStart(12, ' '); | ||
|
|
||
| const calculateChecksum = (alphabet: string) => { | ||
| const sum = weightedSum(paddedInput, { | ||
| modulus: 11, | ||
| alphabet: alphabet, | ||
| weights: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], | ||
| reverse: true, | ||
| }); | ||
|
|
||
| const mod = 11 - (sum % 11); | ||
| if (mod === 11) return '0'; | ||
| if (mod === 10) return 'A'; | ||
| return String(mod); | ||
| }; | ||
|
|
||
| // Try with official SAT alphabet first | ||
| const valOfficial = calculateChecksum(checkAlphabet); | ||
|
|
||
| if (check !== valOfficial) { | ||
| // If it fails, try with Legacy alphabet (Base 36) | ||
| // This handles older RFCs generated without '&' or 'Ñ' support | ||
| const valLegacy = calculateChecksum(checkAlphabetLegacy); | ||
|
Comment on lines
+156
to
+176
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The const calculateChecksum = (input: string, alphabet: string) => {
const sum = weightedSum(input, {
modulus: 11,
alphabet: alphabet,
weights: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14],
reverse: true,
});
const mod = 11 - (sum % 11);
if (mod === 11) return '0';
if (mod === 10) return 'A';
return String(mod);
};
// Try with official SAT alphabet first
const valOfficial = calculateChecksum(paddedInput, checkAlphabet);
if (check !== valOfficial) {
// If it fails, try with Legacy alphabet (Base 36)
// This handles older RFCs generated without '&' or 'Ñ' support
const valLegacy = calculateChecksum(paddedInput, checkAlphabetLegacy); |
||
|
|
||
| if (check !== valLegacy) { | ||
| return { isValid: false, error: new exceptions.InvalidChecksum() }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -190,4 +191,4 @@ const impl: Validator = { | |
| }; | ||
|
|
||
| export const { name, localName, abbreviation, validate, format, compact } = | ||
| impl; | ||
| impl; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This combined assertion
result.isValid && result.compactcan lead to confusing test failure messages. Ifresult.isValidisfalse, the expression evaluates tofalse, and the test fails withexpected false to equal 'SOTO800101110', which doesn't clearly indicate the root cause. Asserting on an object containing the properties you want to check provides much clearer output on failure, making tests easier to debug.