Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/estonian-future-birth-date.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stll/stdnum": patch
---

Reject Estonian personal ID codes (isikukood) whose birth date is in the future, as Danish CPR and Norwegian birth numbers already are.
4 changes: 2 additions & 2 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 58 additions & 20 deletions crates/stdnum-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,32 +808,32 @@ fn validate_dk_vat(value: &str) -> bool {
}

fn validate_ee_ik(value: &str) -> bool {
ee_ik_valid_on(value, current_date())
}

/// Whether `value` is a valid isikukood on `today`: a person cannot be
/// registered before their birth date.
fn ee_ik_valid_on(value: &str, today: (u32, u32, u32)) -> bool {
ee_ik_birth_date(value).is_some_and(|birth_date| birth_date <= today)
}

/// Birth date of a structurally valid isikukood with a correct check digit.
fn ee_ik_birth_date(value: &str) -> Option<(u32, u32, u32)> {
let compact = compact_without(value, &[' ', '-']);
let Ok(digits) = <[u32; 11]>::try_from(decimal_digits_strict(&compact))
else {
return false;
};
let Some(gender_century) = digits.first().copied() else {
return false;
};
let century: u32 = match gender_century {
let digits = <[u32; 11]>::try_from(decimal_digits_strict(&compact)).ok()?;
let century: u32 = match digits.first().copied()? {
1 | 2 => 1800,
3 | 4 => 1900,
5 | 6 => 2000,
_ => return false,
};
let Some(yy) = two_digit_number(&digits, 1) else {
return false;
};
let Some(month) = two_digit_number(&digits, 3) else {
return false;
};
let Some(day) = two_digit_number(&digits, 5) else {
return false;
_ => return None,
};
valid_date(century.saturating_add(yy), month, day)
let year = century.saturating_add(two_digit_number(&digits, 1)?);
let month = two_digit_number(&digits, 3)?;
let day = two_digit_number(&digits, 5)?;
(valid_date(year, month, day)
&& two_pass_personal_check(digits.get(..10).unwrap_or(&[]))
== digits.get(10).copied()
== digits.get(10).copied())
.then_some((year, month, day))
}

fn validate_ee_vat(value: &str) -> bool {
Expand Down Expand Up @@ -2611,6 +2611,44 @@ mod tests {
));
}

#[test]
fn estonian_personal_id_rejects_future_birth_dates() {
use super::ee_ik_valid_on;
use crate::{ValidationError, validators::ee::ik};

let today = (2026, 11, 18);
// Born today (century digit 6: 2000s) is valid, tomorrow is not.
assert!(ee_ik_valid_on("62611180000", today));
assert!(ee_ik_valid_on("52611180003", today));
assert!(!ee_ik_valid_on("62611190007", today));
assert!(!ee_ik_valid_on("52611190006", today));
assert!(ee_ik_valid_on("62611190007", (2026, 11, 19)));
// Later years of the current century are in the future.
assert!(!ee_ik_valid_on("62701010000", today));
assert!(!ee_ik_valid_on("69912310002", today));
// Earlier centuries are always in the past.
assert!(ee_ik_valid_on("19912310008", today));
assert!(ee_ik_valid_on("40001010005", today));
assert!(ee_ik_valid_on("36805280109", today));
assert!(ee_ik_valid_on("50002290002", today));
// A future date does not rescue a bad check digit.
assert!(!ee_ik_valid_on("19912310009", today));

// 2094-03-06 with a correct check digit.
assert_eq!(
ik::validate("59403068580"),
Err(ValidationError::InvalidComponent(
"Estonian personal ID contains a birth date in the future",
))
);
assert!(!validate_named_id("ee.ik", "59403068580"));
assert_eq!(ik::validate("36805280109"), Ok("36805280109".to_owned()));
assert!(matches!(
ik::validate("59403068581"),
Err(ValidationError::InvalidChecksum(_))
));
}

#[test]
fn rejects_unknown_validators() {
assert!(!validate_named_id("unknown", "111000025"));
Expand Down
12 changes: 12 additions & 0 deletions crates/stdnum-core/src/validators/legacy_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,18 @@ fn validate_for(id: &str, value: &str) -> ValidationResult {
}
}
let compact = compact_for(id, value);
if id == "ee.ik" {
// Decide acceptance and the error kind from one date reading.
if let Some(birth_date) = crate::ee_ik_birth_date(&compact) {
return if birth_date <= crate::current_date() {
Ok(compact)
} else {
Err(ValidationError::InvalidComponent(
"Estonian personal ID contains a birth date in the future",
))
};
}
}
if raw_is_valid(id, &compact) {
return Ok(compact);
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"performance:oracles": "node scripts/oracle-performance-gate.mjs",
"codegen": "bun scripts/generate-public-surface.ts",
"codegen:check": "bun scripts/generate-public-surface.ts --check",
"oracle": "bun scripts/oracle.ts --mode=gate",
"oracle:gate": "bun scripts/oracle.ts --mode=gate",
"oracle:survey": "bun scripts/oracle.ts --mode=survey",
"oracle": "TZ=UTC bun scripts/oracle.ts --mode=gate",
"oracle:gate": "TZ=UTC bun scripts/oracle.ts --mode=gate",
"oracle:survey": "TZ=UTC bun scripts/oracle.ts --mode=survey",
"rust:fmt": "cargo ci-fmt",
"rust:fmt:check": "cargo ci-fmt",
"rust:lint": "cargo ci-clippy",
Expand Down Expand Up @@ -71,7 +71,7 @@
"oxlint": "1.93.0",
"oxlint-tsgolint": "7.0.2006",
"rut.js": "^2.1.0",
"stdnum": "^1.15.0",
"stdnum": "^1.16.0",
Comment thread
jan-kubica marked this conversation as resolved.
Resolved
"tsdown": "0.23.7",
"validate-polish": "^2.1.40",
"vite": "8.3.7"
Expand Down
3 changes: 3 additions & 0 deletions scripts/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,9 @@ const SURVEY_ONLY_ENTRIES = new Set([
"python-stdnum:br.cpf",
"python-stdnum:cn.ric",
"python-stdnum:cz.rc",
// python-stdnum accepts isikukood birth dates after
// the current date; we reject them, as stdnum-js does.
"python-stdnum:ee.ik",
"python-stdnum:do_.rnc",
"python-stdnum:id.npwp",
"python-stdnum:lv.vat",
Expand Down