Skip to content

fix(bg/egn): validate the embedded birth date#158

Merged
koblas merged 2 commits into
koblas:mainfrom
spokodev:fix/bg-egn-validate-birth-date
Jun 22, 2026
Merged

fix(bg/egn): validate the embedded birth date#158
koblas merged 2 commits into
koblas:mainfrom
spokodev:fix/bg-egn-validate-birth-date

Conversation

@spokodev

Copy link
Copy Markdown
Contributor

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:

validate('2992070971'); // month "92" -> { isValid: true }

python-stdnum (the reference this library tracks) rejects these as InvalidComponent, and its own docstring uses 8019010008 as an "invalid date" example.

Fix

Decode the century from the month and validate the date with the existing isValidDate helper, before the check-digit step (matching the reference's order). This mirrors python-stdnum's get_birth_date.

Verified differentially against python-stdnum over 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 expecting InvalidComponent. They fail before the change and pass after. Full suite is green except one pre-existing, unrelated mx/curp date failure present on main. eslint + prettier --check clean.

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).
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Date Validation: Implemented birth date validation for Bulgarian EGNs by decoding the century from the month component and verifying the date using the existing isValidDate helper.
  • Improved Accuracy: Ensures that EGNs with impossible dates are correctly rejected, even if they pass the checksum validation, aligning the library's behavior with python-stdnum.
  • Test Coverage: Added regression tests for invalid date scenarios, including impossible months and specific cases documented by the reference implementation.
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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/bg/egn.ts Outdated
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.
@koblas koblas merged commit 692835d into koblas:main Jun 22, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants