Import API: strong identifiers accept any non-empty string, so a junk ISBN validates
Feature Request
Problem / Opportunity
import_validator checks that fields are present and non-empty. It does not check that they mean anything — the docstring says so outright:
This does not verify data is sane. See https://github.com/internetarchive/openlibrary/issues/9440.
That is fine for most fields. It is a problem for the strong identifiers, because they are what buys a record its exemption from being complete.
CompleteBook requires title + source_records + authors + publishers + publish_date. StrongIdentifierBook waives authors, publishers and publish_date in exchange for "at least one of isbn_10, isbn_13, lccn". But those are typed NonEmptyList[NonEmptyStr], with no format rule. So in practice the exemption is "a title and any non-empty string".
Against current master:
>>> from openlibrary.plugins.importapi.import_validator import import_validator
>>> v = import_validator()
>>> v.validate({"title": "T", "source_records": ["amazon:1"], "isbn_13": ["banana"]})
True
>>> v.validate({"title": "T", "source_records": ["amazon:1"], "isbn_13": ["5"]})
True
>>> v.validate({"title": "T", "source_records": ["amazon:1"], "isbn_10": ["9" * 99]})
True
>>> v.validate({"title": "T", "source_records": ["amazon:1"], "lccn": ["!!!"]})
True
Each of those creates a catalogue record carrying an identifier that is not an identifier. That matters more than a bad free-text field, because identifiers are what add_book matches and merges on — a junk isbn_13 is a key that can collide or block a real match later, and it is invisible to a librarian reading the record.
This is the follow-up that was explicitly deferred out of #14276. The scope question raised there — "Should StrongIdentifierBook gain ISBN length/format validation as part of this issue, or strictly as the separate follow-up the issue proposes? This is a scope call for a maintainer" — was never answered. This issue is that call.
Scope
In scope: format validation for isbn_10, isbn_13, lccn on StrongIdentifierBook.
Deliberately out of scope, each already having a home:
| where it lives | |
|---|---|
publish_date accepting "banana" / "3025" |
#7900 |
| null bytes in titles breaking rendering and dumps | #6468 |
| the general "validate import input" umbrella | #5833 (@scottbarnes) |
This is a narrow slice of #5833, not an attempt to take it.
Open questions — these are the decision, and I'd rather not guess
- Reject or drop? If a record arrives with
isbn_13: ["banana"], do we reject the whole record, or drop the bad field and let the record through if it still validates another way? Dropping keeps imports flowing; rejecting keeps the catalogue clean. This is the main call. - How strict?
normalize_isbnstrips hyphens and spacing but does not check the check digit, so9781737408803passes it while being an invalid ISBN. Length-and-shape only, or check digits too? Check digits reject more real-world data, some of which is genuinely in print with a typo'd ISBN. - LCCN has no normalizer in the codebase comparable to the ISBN helpers. Is a format rule for it wanted here, or should this issue be ISBN-only?
- Backward compatibility. This rejects records that import successfully today, across every source — IA, partners, BWB, Gutenberg, bots. That is the real risk, not the code.
Proposal
Given (4), I'd suggest measuring before enforcing:
- Phase 1 — count it. Add the check in report-only form and run it over recent import batches to answer "how many records currently arrive with a malformed strong identifier, and from which sources?" Nobody has that number right now, and the answer decides whether (1) is even a difficult question. If it's 0.1%, reject. If it's 20% from one partner, the conversation is with that partner, not with the validator.
- Phase 2 — enforce, once there's a number and a decision on (1)–(3).
The code for phase 2 is small. get_isbn_field() in #14349 already decides an ISBN's field from its canonical form and returns a pair, so the validator rule is a thin wrapper over it rather than new parsing logic.
I'm happy to do both phases, but I'm not going to write phase 2 before there's a decision on (1), since it changes behaviour for every import source.
Breakdown
Implementation Details (for maintainers)
Related files
openlibrary/plugins/importapi/import_validator.py—StrongIdentifierBook,STRONG_IDENTIFIERSopenlibrary/utils/isbn.py—normalize_isbn,get_isbn_10_and_13, andget_isbn_field(added in #14349, unmerged)openlibrary/plugins/importapi/tests/test_import_validator.py
Requirements Checklist
- [ ] Decide reject-vs-drop (open question 1)
- [ ] Decide length-only vs check digit (open question 2)
- [ ] Decide whether LCCN is in scope (open question 3)
- [ ] Phase 1: report-only check + a count over recent import batches, by source
- [ ] Phase 2: the validator rule, with tests covering each identifier and each decision above
Stakeholders
@mekarpeles — Lead on #14276, where this was deferred and the scope call was left open
@scottbarnes — Lead on #5833, the umbrella this is a slice of; flagging so this doesn't cut across what you have planned there
Thank you @MohitSAGAR11 for submitting this issue!
⚠️ Contributors, this issue will be ready to work on once:
- [ ] Triage — maintainers will add
Priority: *andLead: @* - [ ] Needs: Staff Decision — reject-vs-drop for a malformed strong identifier, how strict the format check should be (length/shape only vs. check digit), and whether LCCN is in scope alongside ISBN
Context
- The claim that
import_validatorperforms no format checking on strong identifiers is confirmed by readingStrongIdentifierBook—isbn_10,isbn_13, andlccnare typedNonEmptyList[NonEmptyStr]with no format rule, and the class never imports anything fromopenlibrary/utils/isbn.pyoropenlibrary/utils/lccn.py. git blameonStrongIdentifierBookshows the class was authored by Scott Barnes (00bec1e7c, 2024-08-08), and he isLead: @scottbarneson both the umbrella issue #5833 and the related #7900.- Open question 3 ("LCCN has no normalizer in the codebase comparable to the ISBN helpers") does not hold up —
normalize_lccn()already exists and validates LCCN shape against a regex for the format described at the LOC namespace spec, returningNonefor anything that does not match. It is already used downstream innormalize_record_bibids(). - For open question 2 (check digit strictness), the check-digit math already exists in the codebase —
check_digit_10andcheck_digit_13— so a check-digit-strict rule would also be a thin wrapper, not new parsing logic, same as the shape-only option. - One nuance on the "junk identifier becomes a matching key" risk:
normalize_record_bibids()(linked above) runs downstream ofimport_validator().validate()in theadd_bookflow (called fromopenlibrary/catalog/add_book/__init__.py:865, after the validator gate inopenlibrary/plugins/importapi/import_edition_builder.py:141/openlibrary/plugins/importapi/code.py:475) and drops anyisbn/lccnentry wherenormalize_isbn/normalize_lccnreturn falsy. The regex innormalize_lccnwould catch thelccn: ["!!!"]case from the issue body at this later stage. However, the docstring onnormalize_isbnis explicit that it does not validate length, so theisbn_13: ["5"]andisbn_10: ["9"*99]repro cases from the issue body would survive this downstream filter unchanged — the validator gap described here is real for those two cases even accounting for later cleanup. - #14349 (open, unmerged) adds
get_isbn_field()toopenlibrary/utils/isbn.pyand is referenced correctly as prerequisite/related work — it does not touchimport_validator.pyitself. - No duplicate or prior-attempt issue was found via search; #14276, #9440, #7900, #6468, and #5833 are all referenced accurately against their current state/labels.
Issue Assessment
Action items
- [x] Does a normalizer analogous to the ISBN helpers exist for LCCN (open question 3)? (Answer: Yes —
normalize_lccn()inopenlibrary/utils/lccn.pyalready does shape validation. The remaining call for a maintainer is only whether to wire it intoStrongIdentifierBookthe same wayget_isbn_field()would be wired for ISBNs, not whether to build it.) - [ ] Reject vs. drop for a malformed strong identifier — requires a maintainer decision (open question 1).
- [ ] Shape-only vs. check-digit strictness — requires a maintainer decision (open question 2); note both options are equally cheap to implement per the Context section above.
🔔 Stakeholders
@scottbarnes — authored StrongIdentifierBook (per git blame) and is Lead on both #5833 (the validation umbrella this is scoped out of) and #7900 (the sibling publish_date validation issue)
Full triage checklist
- [x] Triage — correct labels applied or removed
- [x] Problem / opportunity — problem is clear, audience identified, actionable
- [x] Scope — issue is well-scoped; does not try to do too many things
- [x] Justification — reasoning and impact stated; sufficient for leads to prioritize
- [ ] Success & testing criteria — no explicit "done" definition beyond the requirements checklist; the report-only output format for Phase 1 is not specified
- [x] Proposal & breakdown — concrete plan present; checklist actionable for a contributor
- [ ] If bug: environment & repro steps — N/A, this is a validation-logic gap with an inline Python repro, not an environment-dependent bug
- [x] Risks, concerns, open questions — identified and surfaced
- [x] Richy context — relevant code, commits, PRs reviewed; no duplicate found
- [x] References — permalinks and issue cross-references present
[!NOTE] This comment was automatically generated by Richy, Open Library's issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.
From a prospective importer's side (BiblioGenius, a personal library app that aims to contribute missing records to Open Library through batch imports), on questions 1 and 2:
-
Check digits, at least for ISBN. A strong identifier is what exempts a record from authors, publishers and publish_date, so a length-only rule still lets through any 10 or 13 digit string. We hit this on CSV imports: an export put a site's internal reference (SI1035858976) in the ISBN column, and once the letters were stripped its ten digits passed for an ISBN-10. Only the checksum caught it. The helpers are already in
openlibrary/utils/isbn.py. -
Drop the field rather than reject the record, and say so in the batch result so the submitter can fix it. Real catalogues carry mistyped ISBNs: in the reading lists we compile by hand and ship with the app, five ISBNs failed the check digit while the rest of each record was fine. Three were a single digit off the real number: 9782253059536 for 9782253059530, 9781594200318 for 9781594200311, 9782874950317 for 9782874950117. A record that only qualified through the bad ISBN would then fall back to the CompleteBook rule and fail, which seems the right outcome. It would also match what CompleteBook already does with known bad authors and dates, and the record still goes through the threshold match on title, authors, publisher and date, like any record without an ISBN.