Skip to content

Commit c654190

Browse files
feat(barcode-generator-web): add Data Matrix config model and validation
Adds DataMatrixTypeConfig to the BarcodeConfig union with a format === "DataMatrix" branch in barcodeConfig(), plus a DataMatrix value check and a loose GS1 Application Identifier syntax validator. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 2178c12 commit c654190

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

packages/pluggableWidgets/barcode-generator-web/src/config/Barcode.config.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ export interface QRCodeTypeConfig extends CodeBaseTypeConfig {
5555
};
5656
}
5757

58-
export type BarcodeConfig = BarcodeTypeConfig | QRCodeTypeConfig;
58+
/** Configuration for Data Matrix rendering */
59+
export interface DataMatrixTypeConfig extends CodeBaseTypeConfig {
60+
type: "datamatrix";
61+
size: number;
62+
gs1Mode: boolean;
63+
shape: "square" | "rectangle";
64+
}
65+
66+
export type BarcodeConfig = BarcodeTypeConfig | QRCodeTypeConfig | DataMatrixTypeConfig;
5967

6068
export function barcodeConfig(props: BarcodeGeneratorContainerProps): BarcodeConfig {
6169
const codeValue = props.codeValue?.value ?? "";
@@ -77,6 +85,16 @@ export function barcodeConfig(props: BarcodeGeneratorContainerProps): BarcodeCon
7785
downloadButton: downloadButtonConfig
7886
};
7987

88+
if (format === "DataMatrix") {
89+
return {
90+
type: "datamatrix",
91+
...baseConfig,
92+
size: props.dmSize ?? 128,
93+
gs1Mode: props.dmGs1Mode ?? false,
94+
shape: props.dmShape ?? "square"
95+
};
96+
}
97+
8098
if (format === "QRCode") {
8199
return {
82100
type: "qrcode",

packages/pluggableWidgets/barcode-generator-web/src/config/validation.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ export function validateBarcodeValue(format: CustomCodeFormatEnum | CodeFormatEn
114114
return { valid: false, message: "CODE93 should not contain control characters." };
115115
}
116116
return { valid: true };
117+
case "DataMatrix":
118+
// DataMatrix: encoder handles the heavy lifting; guard extremely long static values
119+
if (value.length > 2000) {
120+
return {
121+
valid: false,
122+
message: "The Data Matrix value is very long; consider a shorter value or a dynamic attribute."
123+
};
124+
}
125+
return { valid: true };
117126
case "QRCode":
118127
// QRCode: accepts most characters, but warn for extremely long static values
119128
if (value.length > 1200) {
@@ -140,6 +149,33 @@ export function validateBarcodeValue(format: CustomCodeFormatEnum | CodeFormatEn
140149
}
141150
}
142151

152+
/**
153+
* Loosely validate GS1 Data Matrix Application Identifier syntax.
154+
* Expects human-readable AI form, e.g. `(01)09501101020917(17)261231(10)ABC123`.
155+
* bwip-js is the source of truth for encoding; this catches obvious structural errors early.
156+
*/
157+
export function validateGs1DataMatrixValue(value: string): ValidationResult {
158+
if (!value) {
159+
return { valid: true };
160+
}
161+
162+
// Must start with an application identifier and contain only balanced (nn) groups with data.
163+
if (!/^(\(\d{2,4}\)[^(]*)+$/.test(value)) {
164+
return {
165+
valid: false,
166+
message:
167+
"GS1 Data Matrix expects Application Identifier syntax, e.g. (01)09501101020917(17)261231(10)ABC123."
168+
};
169+
}
170+
171+
// Every AI group must carry at least one data character.
172+
if (/\(\d{2,4}\)(?=\(|$)/.test(value)) {
173+
return { valid: false, message: "Each GS1 Application Identifier must be followed by data." };
174+
}
175+
176+
return { valid: true };
177+
}
178+
143179
/** Validate addon (EAN-5 / EAN-2) values. */
144180
export function validateAddonValue(addonFormat: AddonFormatEnum | null | undefined, value: string): ValidationResult {
145181
if (!addonFormat || addonFormat === "None") {

0 commit comments

Comments
 (0)