fix(bg/egn): validate the embedded birth date#158
Conversation
The first six digits of a Bulgarian EGN are a birth date whose month
encodes the century (41-52 -> 2000s, 21-32 -> 1800s, 01-12 -> 1900s), but
validate() only checked the length and the check digit. A number with an
impossible date and a correct check digit was accepted:
validate('2992070971') // month 92 -> reported valid
python-stdnum rejects these as InvalidComponent. Decode the century from
the month and validate the date, matching the reference. Verified against
python-stdnum: 0 disagreements over 50k random 10-digit inputs (was: JS
accepted invalid-date numbers python rejects).
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Bulgarian EGN validation logic by introducing strict date verification. Previously, the validator only checked length, digit format, and the checksum, which allowed EGNs with valid checksums but impossible birth dates to pass. The changes now decode the century-encoded month and validate the full date before performing the checksum, ensuring higher data integrity. Highlights
New Features🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces birth date validation to the Bulgarian EGN validator. It extracts the birth date from the first six digits, decodes the century from the month value, and validates the resulting date using isValidDate, returning an InvalidComponent error for invalid dates. Corresponding unit tests have also been added. Feedback was provided to improve the readability of the century calculation by directly assigning the absolute century offset rather than using relative adjustments.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Set each branch's century explicitly (2000s/1800s/1900s) instead of starting at 1900 and adjusting by +/-100, so the code mirrors the documented mapping and the reference implementation. Behaviour is unchanged.
Problem
The first six digits of a Bulgarian EGN are a birth date whose month encodes the century (
41-52→ 2000s,21-32→ 1800s,01-12→ 1900s).validate()only checked the length, that the value is all digits, and the check digit — it never validated the date. A number with an impossible date but a correct check digit was accepted:python-stdnum(the reference this library tracks) rejects these asInvalidComponent, and its own docstring uses8019010008as an "invalid date" example.Fix
Decode the century from the month and validate the date with the existing
isValidDatehelper, before the check-digit step (matching the reference's order). This mirrorspython-stdnum'sget_birth_date.Verified differentially against
python-stdnumover 50,000 random 10-digit inputs: 0 disagreements after the change (before, JS accepted invalid-date numbers that python rejects).Tests
Added two cases to
bg/egn.spec.ts: an impossible month with a valid check digit (2992070971) and python-stdnum's documented invalid-date example (8019010008), both expectingInvalidComponent. They fail before the change and pass after. Full suite is green except one pre-existing, unrelatedmx/curpdate failure present onmain.eslint+prettier --checkclean.