Skip to content

add isbn10 and isbn13 validators to Edition model#3573

Merged
mouse-reeve merged 8 commits into
bookwyrm-social:mainfrom
ilkka-ollakka:tweak/validate_isbn_in_model
Jun 23, 2025
Merged

add isbn10 and isbn13 validators to Edition model#3573
mouse-reeve merged 8 commits into
bookwyrm-social:mainfrom
ilkka-ollakka:tweak/validate_isbn_in_model

Conversation

@ilkka-ollakka

@ilkka-ollakka ilkka-ollakka commented May 2, 2025

Copy link
Copy Markdown
Contributor

Description

Checks if isbns looks valid when manually editing book before it goes to database. This allows the form inform if isbn values look something else than what bookwyrm expects them to be.

What type of Pull Request is this?

  • Bug Fix
  • Enhancement
  • Plumbing / Internals / Dependencies
  • Refactor

Does this PR change settings or dependencies, or break something?

  • This PR changes or adds default settings, configuration, or .env values
  • This PR changes or adds dependencies
  • This PR introduces other breaking changes

Details of breaking or configuration changes (if any of above checked)

Documentation

  • New or amended documentation will be required if this PR is merged
  • I have created a matching pull request in the Documentation repository
  • I intend to create a matching pull request in the Documentation repository after this PR is merged

Tests

  • My changes do not need new tests
  • All tests I have added are passing
  • I have written tests but need help to make them pass
  • I have not written tests and need help to write them

@ilkka-ollakka ilkka-ollakka force-pushed the tweak/validate_isbn_in_model branch 2 times, most recently from ad1956e to b8d4b62 Compare May 2, 2025 18:18
Comment thread bookwyrm/models/book.py Outdated
@dato

dato commented May 18, 2025

Copy link
Copy Markdown
Contributor

Hello!, and thank you for your PR.

Regarding unit tests:

  • I have not written tests and need help to write them

I’ve pushed a branch with unit tests for your changes (diff can be seen here; I could create a PR if you’d like).

Some of the tests are commented out, since they would fail with the current implementation. Starting with the least complex, these are:

  1. validate_isbn13 properly strips dashes from the value, but validate_isbn10 doesn’t.

  2. validate_isbn10 doesn’t detect invalid checksum characters, e.g. 012345678Z (very minor; could be ignored).

  3. ISBNs include a “check digit” that guards against typos (brief summary: the last digit is always ‘computed’ from the rest of the ISBN, so it can be used to detect whether a mistake was made when typing the number).

    It would be nice, I think, if form validation detected this as well, in addition to malformed values. Wikipedia describes check digits in ISBNs, and our isbn_10_to_13 and isbn_13_to_10 functions include code to compute it.

@ilkka-ollakka

Copy link
Copy Markdown
Contributor Author

Hi,

Thanks for the test-examples and comments, those help a lot! Actually looking the models/book.py file, I could validate isbn10 by converting it to isbn13 and back and check result to match. For isbn13 similar should work if I just extend isbn13_to_10 also accept '979' prefix.

But please open PR for those tests and I'll take them in and work on those failing cases you spotted.

@ilkka-ollakka

Copy link
Copy Markdown
Contributor Author

Realised that I can make the PR with your link directly, so took it in, thanks!

@mouse-reeve mouse-reeve left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the ISBN 13 value showing the ISBN 10 message issue is complex to resolve, I think I reasonable solution would be just to always give the same generic error message. Otherwise this looks good!

Comment thread bookwyrm/models/book.py Outdated
Comment thread bookwyrm/models/book.py Outdated
@ilkka-ollakka ilkka-ollakka force-pushed the tweak/validate_isbn_in_model branch from 688292c to d18eea4 Compare June 8, 2025 15:07

@mouse-reeve mouse-reeve left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Works great, thank you!

@mouse-reeve mouse-reeve merged commit 7488b97 into bookwyrm-social:main Jun 23, 2025
10 checks passed
@ilkka-ollakka ilkka-ollakka deleted the tweak/validate_isbn_in_model branch June 24, 2025 08:46
@hughrun hughrun added the fix To label PRs that fix bugs label Aug 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix To label PRs that fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An invalid ISBN causes the book page to crash (quite problematic)

5 participants