fix(barcode): validate EAN-13/8, UPC-A, ITF-14 before bwip-js — front…#22
Merged
simonabler merged 1 commit intomasterfrom Mar 11, 2026
Merged
fix(barcode): validate EAN-13/8, UPC-A, ITF-14 before bwip-js — front…#22simonabler merged 1 commit intomasterfrom
simonabler merged 1 commit intomasterfrom
Conversation
…end + backend
Problem: bwip-js threw opaque internal errors (bwipp.ean13badLength,
bwipp.ean13badCheckDigit) that escaped as unhandled 500s with a Content-Type
mismatch warning. The frontend had no format validation and fired requests on
every keystroke regardless of digit count.
Backend (barcodes.service.ts):
New private validateStandardText(type, text) method called at the top of
both toPng() and toSvg() before bwip-js is invoked:
EAN-13: must match /^\d{12,13}$/, check digit verified if 13 digits given
EAN-8: must match /^\d{7,8}$/, check digit verified if 8 digits given
UPC-A: must match /^\d{11,12}$/, check digit verified if 12 digits given
ITF-14: must match /^\d{13,14}$/
CODE128 / CODE39 / PDF417 / DATAMATRIX: no pre-validation (bwip handles)
Throws BadRequestException with a clear, human-readable message.
Private eanCheckDigit() / eanCheckDigitValid() helpers implement the
standard Luhn-style EAN weight (odd×1, even×3).
Frontend (barcode-editor-item.component.ts/.html):
New barcodeTextValidator(getType) ValidatorFn mirrors the backend rules.
Applied via addValidators() on the text control; re-runs when type changes.
Invalid requests are blocked (EMPTY returned from switchMap).
Validation error shown inline below the text input (is-invalid + feedback).
API errors from catchError are also surfaced via errorMsg.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…end + backend
Problem: bwip-js threw opaque internal errors (bwipp.ean13badLength, bwipp.ean13badCheckDigit) that escaped as unhandled 500s with a Content-Type mismatch warning. The frontend had no format validation and fired requests on every keystroke regardless of digit count.
Backend (barcodes.service.ts):
New private validateStandardText(type, text) method called at the top of
both toPng() and toSvg() before bwip-js is invoked:
EAN-13: must match /^\d{12,13}$/, check digit verified if 13 digits given
EAN-8: must match /^\d{7,8}$/, check digit verified if 8 digits given
UPC-A: must match /^\d{11,12}$/, check digit verified if 12 digits given
ITF-14: must match /^\d{13,14}$/
CODE128 / CODE39 / PDF417 / DATAMATRIX: no pre-validation (bwip handles)
Throws BadRequestException with a clear, human-readable message.
Private eanCheckDigit() / eanCheckDigitValid() helpers implement the
standard Luhn-style EAN weight (odd×1, even×3).
Frontend (barcode-editor-item.component.ts/.html):
New barcodeTextValidator(getType) ValidatorFn mirrors the backend rules.
Applied via addValidators() on the text control; re-runs when type changes.
Invalid requests are blocked (EMPTY returned from switchMap).
Validation error shown inline below the text input (is-invalid + feedback).
API errors from catchError are also surfaced via errorMsg.