@open-dpp/dto 2.6.0 → 2.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/aas/submodel-base/create-group-from-column-schema.d.ts +61 -0
- package/dist/aas/submodel-base/create-group-from-column-schema.d.ts.map +1 -0
- package/dist/aas/submodel-base/create-group-from-column-schema.js +9 -0
- package/dist/activity-history/activity-history.dto.d.ts +47 -28
- package/dist/activity-history/activity-history.dto.d.ts.map +1 -1
- package/dist/activity-history/activity-types.dto.d.ts +10 -8
- package/dist/activity-history/activity-types.dto.d.ts.map +1 -1
- package/dist/activity-history/activity-types.dto.js +5 -4
- package/dist/activity-history/change-event-types.dto.d.ts +8 -0
- package/dist/activity-history/change-event-types.dto.d.ts.map +1 -1
- package/dist/activity-history/change-event-types.dto.js +4 -0
- package/dist/api-version.dto.d.ts +14 -0
- package/dist/api-version.dto.d.ts.map +1 -0
- package/dist/api-version.dto.js +12 -0
- package/dist/digital-product-document/digital-product-document.dto.d.ts +11 -0
- package/dist/digital-product-document/digital-product-document.dto.d.ts.map +1 -0
- package/dist/digital-product-document/digital-product-document.dto.js +9 -0
- package/dist/digital-product-document/digital-product-document.schemas.d.ts.map +1 -0
- package/dist/index.d.ts +16 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -1
- package/dist/organizations/member.dto.d.ts +8 -2
- package/dist/organizations/member.dto.d.ts.map +1 -1
- package/dist/organizations/member.dto.js +7 -1
- package/dist/passports/passport.dto.js +1 -1
- package/dist/permalinks/permalink.dto.js +1 -1
- package/dist/presentation-configurations/presentation-configuration.dto.d.ts +0 -9
- package/dist/presentation-configurations/presentation-configuration.dto.d.ts.map +1 -1
- package/dist/presentation-configurations/presentation-configuration.dto.js +5 -9
- package/dist/shared/filter.dto.js +1 -1
- package/dist/templates/template.dto.js +1 -1
- package/dist/unique-product-identifiers/gs1/gs1-ai-constants.d.ts +574 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-constants.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-constants.js +570 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-description.d.ts +15 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-description.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-description.js +47 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-i18n.d.ts +27 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-i18n.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-i18n.js +523 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-table.d.ts +47 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-table.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-ai-table.js +4363 -0
- package/dist/unique-product-identifiers/gs1/gs1-data-attributes.dto.d.ts +540 -0
- package/dist/unique-product-identifiers/gs1/gs1-data-attributes.dto.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-data-attributes.dto.js +62 -0
- package/dist/unique-product-identifiers/gs1/gs1-digital-link.d.ts +158 -0
- package/dist/unique-product-identifiers/gs1/gs1-digital-link.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-digital-link.js +321 -0
- package/dist/unique-product-identifiers/gs1/gs1-identity.dto.d.ts +39 -0
- package/dist/unique-product-identifiers/gs1/gs1-identity.dto.d.ts.map +1 -0
- package/dist/unique-product-identifiers/gs1/gs1-identity.dto.js +40 -0
- package/dist/unique-product-identifiers/unique-product-identifier-type.d.ts +36 -0
- package/dist/unique-product-identifiers/unique-product-identifier-type.d.ts.map +1 -0
- package/dist/unique-product-identifiers/unique-product-identifier-type.js +27 -0
- package/dist/users/create-user.dto.d.ts +2 -2
- package/dist/users/create-user.dto.js +2 -2
- package/dist/users/me.dto.d.ts +24 -0
- package/dist/users/me.dto.d.ts.map +1 -0
- package/dist/users/me.dto.js +10 -0
- package/dist/users/pending-email-change.dto.d.ts +7 -0
- package/dist/users/pending-email-change.dto.d.ts.map +1 -0
- package/dist/users/pending-email-change.dto.js +8 -0
- package/dist/users/request-email-change.dto.d.ts +7 -0
- package/dist/users/request-email-change.dto.d.ts.map +1 -0
- package/dist/users/request-email-change.dto.js +8 -0
- package/dist/users/update-profile.dto.d.ts +11 -0
- package/dist/users/update-profile.dto.d.ts.map +1 -0
- package/dist/users/update-profile.dto.js +12 -0
- package/dist/users/user.dto.d.ts +16 -9
- package/dist/users/user.dto.d.ts.map +1 -1
- package/dist/users/user.dto.js +14 -1
- package/package.json +7 -5
- package/src/aas/submodel-base/create-group-from-column-schema.ts +9 -0
- package/src/activity-history/activity-types.dto.ts +5 -4
- package/src/activity-history/change-event-types.dto.ts +4 -0
- package/src/api-version.dto.ts +13 -0
- package/src/digital-product-document/digital-product-document.dto.ts +8 -0
- package/src/index.ts +16 -1
- package/src/organizations/member.dto.ts +11 -2
- package/src/passports/passport.dto.ts +1 -1
- package/src/permalinks/permalink.dto.ts +1 -1
- package/src/presentation-configurations/presentation-configuration.dto.spec.ts +2 -2
- package/src/presentation-configurations/presentation-configuration.dto.ts +4 -11
- package/src/shared/filter.dto.ts +1 -1
- package/src/shared/permalink-base-url.schema.spec.ts +8 -6
- package/src/templates/template.dto.ts +1 -1
- package/src/unique-product-identifiers/gs1/gs1-ai-constants.spec.ts +99 -0
- package/src/unique-product-identifiers/gs1/gs1-ai-constants.ts +581 -0
- package/src/unique-product-identifiers/gs1/gs1-ai-description.spec.ts +64 -0
- package/src/unique-product-identifiers/gs1/gs1-ai-description.ts +47 -0
- package/src/unique-product-identifiers/gs1/gs1-ai-i18n.ts +530 -0
- package/src/unique-product-identifiers/gs1/gs1-ai-table.spec.ts +86 -0
- package/src/unique-product-identifiers/gs1/gs1-ai-table.ts +4389 -0
- package/src/unique-product-identifiers/gs1/gs1-data-attributes.dto.spec.ts +28 -0
- package/src/unique-product-identifiers/gs1/gs1-data-attributes.dto.ts +67 -0
- package/src/unique-product-identifiers/gs1/gs1-digital-link.spec.ts +126 -0
- package/src/unique-product-identifiers/gs1/gs1-digital-link.ts +368 -0
- package/src/unique-product-identifiers/gs1/gs1-identity.dto.ts +51 -0
- package/src/unique-product-identifiers/unique-product-identifier-type.ts +31 -0
- package/src/users/create-user.dto.spec.ts +80 -0
- package/src/users/create-user.dto.ts +2 -2
- package/src/users/me.dto.spec.ts +76 -0
- package/src/users/me.dto.ts +10 -0
- package/src/users/pending-email-change.dto.spec.ts +47 -0
- package/src/users/pending-email-change.dto.ts +8 -0
- package/src/users/request-email-change.dto.spec.ts +60 -0
- package/src/users/request-email-change.dto.ts +8 -0
- package/src/users/update-profile.dto.spec.ts +93 -0
- package/src/users/update-profile.dto.ts +12 -0
- package/src/users/user.dto.spec.ts +70 -0
- package/src/users/user.dto.ts +15 -9
- package/dist/shared/digital-product-document.schemas.d.ts.map +0 -1
- /package/dist/{shared → digital-product-document}/digital-product-document.schemas.d.ts +0 -0
- /package/dist/{shared → digital-product-document}/digital-product-document.schemas.js +0 -0
- /package/src/{shared → digital-product-document}/digital-product-document.schemas.ts +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { describe, expect, it } from "@jest/globals";
|
|
2
|
+
import { Gs1DataAttributesSchema } from "./gs1-data-attributes.dto";
|
|
3
|
+
|
|
4
|
+
describe("Gs1DataAttributesSchema", () => {
|
|
5
|
+
it("accepts a valid map and the empty map", () => {
|
|
6
|
+
expect(Gs1DataAttributesSchema.safeParse({ "17": "251231", "90": "ABC" }).success).toBe(true);
|
|
7
|
+
expect(Gs1DataAttributesSchema.safeParse({}).success).toBe(true);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("rejects unknown AI keys with a compact message", () => {
|
|
11
|
+
const result = Gs1DataAttributesSchema.safeParse({ "9999": "x" });
|
|
12
|
+
expect(result.success).toBe(false);
|
|
13
|
+
if (!result.success) {
|
|
14
|
+
expect(result.error.issues[0]?.message).toBe('"9999" is not a known GS1 data-attribute AI');
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("reports only the first invalid value (bounds worst-case validation cost)", () => {
|
|
19
|
+
// Each value check may cost ~100ms on adversarial input (see
|
|
20
|
+
// GS1_DATA_ATTRIBUTE_MAX_LENGTH); reporting every failure would let one
|
|
21
|
+
// request multiply that by the number of entries.
|
|
22
|
+
const result = Gs1DataAttributesSchema.safeParse({ "11": "999999", "17": "999999" });
|
|
23
|
+
expect(result.success).toBe(false);
|
|
24
|
+
if (!result.success) {
|
|
25
|
+
expect(result.error.issues.filter((issue) => issue.code === "custom")).toHaveLength(1);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { Gs1DataAttributeAi } from "./gs1-ai-constants";
|
|
3
|
+
import { isValidGs1DataAttributeValue } from "./gs1-digital-link";
|
|
4
|
+
|
|
5
|
+
/** Cap on how much of an unknown AI key is echoed back in an error message. */
|
|
6
|
+
const MAX_ECHOED_KEY_LENGTH = 50;
|
|
7
|
+
|
|
8
|
+
const unknownAiMessage = (input: unknown): string => {
|
|
9
|
+
const key = String(input);
|
|
10
|
+
const echoed =
|
|
11
|
+
key.length > MAX_ECHOED_KEY_LENGTH ? `${key.slice(0, MAX_ECHOED_KEY_LENGTH)}…` : key;
|
|
12
|
+
return `"${echoed}" is not a known GS1 data-attribute AI`;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Zod schema for a single GS1 data-attribute Application Identifier key.
|
|
17
|
+
*
|
|
18
|
+
* The custom error map replaces zod's default enum message, which would join
|
|
19
|
+
* every permitted AI into one multi-kilobyte string.
|
|
20
|
+
*/
|
|
21
|
+
const Gs1DataAttributeAiSchema = z.enum(Gs1DataAttributeAi, {
|
|
22
|
+
error: (issue) => unknownAiMessage(issue.input),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Zod schema for a map of GS1 data-attribute Application Identifiers to their values.
|
|
27
|
+
*
|
|
28
|
+
* Keys are constrained to the known GS1 data-attribute AIs (type 'D' in the
|
|
29
|
+
* vendored AI table — i.e. not a primary identifier like "01" or a key
|
|
30
|
+
* qualifier like "10"/"21") via {@link Gs1DataAttributeAi}, so the inferred
|
|
31
|
+
* type is `Partial<Record<Gs1DataAttributeAi, string>>`. Each value must
|
|
32
|
+
* satisfy the format/length rules for the corresponding AI.
|
|
33
|
+
*
|
|
34
|
+
* An empty map `{}` is accepted.
|
|
35
|
+
* Non-object input (array, string, null, undefined) is rejected by the base
|
|
36
|
+
* `z.partialRecord` schema before the custom check runs.
|
|
37
|
+
*
|
|
38
|
+
* Error reporting is keys-first: when a map contains both an unknown AI and an
|
|
39
|
+
* invalid value for a valid AI, only the unknown-key issue is reported (the
|
|
40
|
+
* value check runs only once the base parse succeeds). Among invalid values,
|
|
41
|
+
* only the first (in key order) is reported: a single value check can cost
|
|
42
|
+
* ~100ms on adversarial input (see GS1_DATA_ATTRIBUTE_MAX_LENGTH), so checking
|
|
43
|
+
* every entry would let one request multiply that by the number of entries.
|
|
44
|
+
*
|
|
45
|
+
* Pure module: no DOM or Node-only globals required at import time.
|
|
46
|
+
*/
|
|
47
|
+
export const Gs1DataAttributesSchema = z
|
|
48
|
+
.partialRecord(Gs1DataAttributeAiSchema, z.string(), {
|
|
49
|
+
error: (issue) => (issue.code === "invalid_key" ? unknownAiMessage(issue.input) : undefined),
|
|
50
|
+
})
|
|
51
|
+
.check((ctx) => {
|
|
52
|
+
for (const [ai, value] of Object.entries(ctx.value)) {
|
|
53
|
+
if (!isValidGs1DataAttributeValue(ai, value)) {
|
|
54
|
+
ctx.issues.push({
|
|
55
|
+
code: "custom",
|
|
56
|
+
input: ctx.value,
|
|
57
|
+
message: `value for AI "${ai}" is invalid`,
|
|
58
|
+
path: [ai],
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
.meta({ id: "Gs1DataAttributes" });
|
|
65
|
+
|
|
66
|
+
/** The inferred TypeScript type for a validated GS1 data-attributes map. */
|
|
67
|
+
export type Gs1DataAttributes = z.infer<typeof Gs1DataAttributesSchema>;
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { describe, expect, it } from "@jest/globals";
|
|
2
|
+
import { GS1_AI_TABLE, type Gs1AiTableEntry } from "./gs1-ai-table";
|
|
3
|
+
import {
|
|
4
|
+
buildGs1DataAttributeQuery,
|
|
5
|
+
buildGs1DigitalLink,
|
|
6
|
+
Cset82ComponentInputSchema,
|
|
7
|
+
Cset82ComponentSchema,
|
|
8
|
+
GS1_DATA_ATTRIBUTE_MAX_LENGTH,
|
|
9
|
+
isGs1DataAttributeAi,
|
|
10
|
+
isValidCset82Component,
|
|
11
|
+
isValidGs1DataAttributeValue,
|
|
12
|
+
} from "./gs1-digital-link";
|
|
13
|
+
|
|
14
|
+
const TABLE: Readonly<Record<string, Gs1AiTableEntry>> = GS1_AI_TABLE;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Sum of the upper bounds of every quantifier in a regex fragment (`{n}` -> n,
|
|
18
|
+
* `{n,m}` -> m). For the vendored type-'D' fragments — a single quantified class,
|
|
19
|
+
* or a short concatenation of them — this is the maximum length of a raw
|
|
20
|
+
* (branch-A) value the regex accepts, i.e. the GS1 "X..N" character ceiling.
|
|
21
|
+
*/
|
|
22
|
+
const maxRawValueLength = (regex: string): number => {
|
|
23
|
+
let sum = 0;
|
|
24
|
+
for (const [, single, upper] of regex.matchAll(/\{(\d+)(?:,(\d+))?\}/g)) {
|
|
25
|
+
sum += Number(upper ?? single);
|
|
26
|
+
}
|
|
27
|
+
return sum;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const dataAttributeEntries = Object.values(TABLE).filter((e) => e.type === "D");
|
|
31
|
+
|
|
32
|
+
describe("isValidGs1DataAttributeValue — length guard (ReDoS)", () => {
|
|
33
|
+
it("rejects an over-long ambiguous value fast instead of backtracking", () => {
|
|
34
|
+
// AI 7256 ("Full name of person") uses the (single-char | %HH%HH){1,90}
|
|
35
|
+
// fragment. Unguarded, ~120 chars of "%AA" pinned a core for seconds; the
|
|
36
|
+
// length guard must reject this in constant time.
|
|
37
|
+
const attack = "%AA".repeat(2000); // 6000 chars, ambiguous, cannot match
|
|
38
|
+
const start = process.hrtime.bigint();
|
|
39
|
+
const result = isValidGs1DataAttributeValue("7256", attack);
|
|
40
|
+
const elapsedMs = Number(process.hrtime.bigint() - start) / 1e6;
|
|
41
|
+
|
|
42
|
+
expect(result).toBe(false);
|
|
43
|
+
expect(elapsedMs).toBeLessThan(250);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("still accepts a valid value at the maximum allowed length", () => {
|
|
47
|
+
const maxValue = "A".repeat(GS1_DATA_ATTRIBUTE_MAX_LENGTH);
|
|
48
|
+
expect(isValidGs1DataAttributeValue("7256", maxValue)).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("rejects a value one character past the cap", () => {
|
|
52
|
+
const tooLong = "A".repeat(GS1_DATA_ATTRIBUTE_MAX_LENGTH + 1);
|
|
53
|
+
expect(isValidGs1DataAttributeValue("7256", tooLong)).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("still validates normal values by format (empty and content rules intact)", () => {
|
|
57
|
+
expect(isValidGs1DataAttributeValue("17", "251231")).toBe(true); // valid YYMMDD
|
|
58
|
+
expect(isValidGs1DataAttributeValue("17", "259999")).toBe(false); // impossible date
|
|
59
|
+
expect(isValidGs1DataAttributeValue("17", "")).toBe(false); // empty
|
|
60
|
+
expect(isValidGs1DataAttributeValue("01", "04006381333931")).toBe(false); // not a data attribute
|
|
61
|
+
expect(isValidGs1DataAttributeValue("9999", "x")).toBe(false); // unknown AI
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("keeps the cap >= the longest value any data-attribute AI accepts", () => {
|
|
65
|
+
// Invariant that survives table regeneration: if GS1 ever ships a longer AI,
|
|
66
|
+
// this fails loudly so the cap (and the ReDoS reasoning) gets revisited.
|
|
67
|
+
for (const entry of dataAttributeEntries) {
|
|
68
|
+
expect(maxRawValueLength(entry.regex)).toBeLessThanOrEqual(GS1_DATA_ATTRIBUTE_MAX_LENGTH);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe("dot-segment batch/serial values", () => {
|
|
74
|
+
// "." and ".." are CSET-82 characters, but as full path segments every RFC
|
|
75
|
+
// 3986 normalizer (browsers, new URL(), servers) collapses them before the
|
|
76
|
+
// resolver sees the path — the link would silently resolve to a different
|
|
77
|
+
// identity. Percent-encoding does not help ("%2E" forms are collapsed too).
|
|
78
|
+
it("rejects '.' and '..' as components, keeps other dotted values", () => {
|
|
79
|
+
expect(isValidCset82Component(".")).toBe(false);
|
|
80
|
+
expect(isValidCset82Component("..")).toBe(false);
|
|
81
|
+
expect(isValidCset82Component("...")).toBe(true);
|
|
82
|
+
expect(isValidCset82Component("1.2.3")).toBe(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("rejects '.' and '..' via the write-boundary schemas", () => {
|
|
86
|
+
expect(Cset82ComponentSchema.safeParse(".").success).toBe(false);
|
|
87
|
+
expect(Cset82ComponentSchema.safeParse("..").success).toBe(false);
|
|
88
|
+
expect(Cset82ComponentInputSchema.safeParse("..").success).toBe(false);
|
|
89
|
+
expect(Cset82ComponentSchema.safeParse("v1.2").success).toBe(true);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("buildGs1DigitalLink refuses dot-segment batch/serial", () => {
|
|
93
|
+
const gtin = "04006381333931";
|
|
94
|
+
expect(() => buildGs1DigitalLink("https://r.example", { gtin, batch: ".." })).toThrow();
|
|
95
|
+
expect(() => buildGs1DigitalLink("https://r.example", { gtin, serial: "." })).toThrow();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
describe("RFC 3986 canonical encoding", () => {
|
|
100
|
+
// encodeURIComponent leaves the sub-delims !'()* raw; all four are CSET-82
|
|
101
|
+
// characters, so links would vary by URL library without post-escaping.
|
|
102
|
+
it("percent-encodes !'()* in batch/serial path segments", () => {
|
|
103
|
+
const link = buildGs1DigitalLink("https://r.example", {
|
|
104
|
+
gtin: "04006381333931",
|
|
105
|
+
serial: "A!'()*B",
|
|
106
|
+
});
|
|
107
|
+
expect(link).toBe("https://r.example/01/04006381333931/21/A%21%27%28%29%2AB");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("percent-encodes !'()* in data-attribute query values", () => {
|
|
111
|
+
// AI 90 (X..30, CSET-82) accepts all four characters raw.
|
|
112
|
+
expect(buildGs1DataAttributeQuery({ "90": "A!'()*B" })).toBe("?90=A%21%27%28%29%2AB");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("non-string input guards (plain-JS callers)", () => {
|
|
117
|
+
it("isGs1DataAttributeAi rejects non-string AI keys", () => {
|
|
118
|
+
expect(isGs1DataAttributeAi(17 as unknown as string)).toBe(false);
|
|
119
|
+
expect(isGs1DataAttributeAi(undefined as unknown as string)).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("isValidGs1DataAttributeValue rejects non-string AI or value", () => {
|
|
123
|
+
expect(isValidGs1DataAttributeValue(17 as unknown as string, "251231")).toBe(false);
|
|
124
|
+
expect(isValidGs1DataAttributeValue("17", 251231 as unknown as string)).toBe(false);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { canonicaliseBaseUrl } from "../../shared/permalink-base-url.schema";
|
|
3
|
+
import { Gs1DataAttributeAi, Gs1KeyAi, Gs1QualifierAi } from "./gs1-ai-constants";
|
|
4
|
+
import { GS1_AI_TABLE } from "./gs1-ai-table";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Zero-dependency GS1 Digital Link helpers, shared by the client and the server.
|
|
8
|
+
*
|
|
9
|
+
* Scope: a GTIN (manufacturer-supplied — open-dpp never mints GS1 identifiers —
|
|
10
|
+
* mod-10 validated, stored normalized to GTIN-14) plus an optional batch (AI `10`)
|
|
11
|
+
* and/or serial (AI `21`), each restricted to GS1's CSET-82 character set and
|
|
12
|
+
* ≤ 20 characters. Granularity is implied by which keys are present.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Valid raw GTIN lengths accepted as input (GTIN-8/12/13/14). */
|
|
16
|
+
const VALID_GTIN_LENGTHS = new Set([8, 12, 13, 14]);
|
|
17
|
+
|
|
18
|
+
/** Length of a normalized GTIN. */
|
|
19
|
+
export const GTIN14_LENGTH = 14;
|
|
20
|
+
|
|
21
|
+
const DIGITS_ONLY = /^[0-9]+$/;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Validate a GTIN's trailing mod-10 check digit.
|
|
25
|
+
*
|
|
26
|
+
* Uses the standard GS1 algorithm: starting from the rightmost data digit
|
|
27
|
+
* (i.e. excluding the check digit), apply alternating weights 3,1,3,1,…; the
|
|
28
|
+
* check digit is the value that rounds the weighted sum up to a multiple of ten.
|
|
29
|
+
*
|
|
30
|
+
* @param gtin - a digits-only string of any length (≥ 2)
|
|
31
|
+
* @returns true when the final digit is the correct check digit
|
|
32
|
+
*/
|
|
33
|
+
export function isValidGtinCheckDigit(gtin: string): boolean {
|
|
34
|
+
if (!DIGITS_ONLY.test(gtin) || gtin.length < 2) {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
const digits = gtin.split("").map((d) => Number.parseInt(d, 10));
|
|
38
|
+
const checkDigit = digits[digits.length - 1];
|
|
39
|
+
const body = digits.slice(0, -1);
|
|
40
|
+
|
|
41
|
+
const sum = body.reduce((acc, digit, i) => {
|
|
42
|
+
const positionFromRight = body.length - 1 - i;
|
|
43
|
+
const weight = positionFromRight % 2 === 0 ? 3 : 1;
|
|
44
|
+
return acc + digit * weight;
|
|
45
|
+
}, 0);
|
|
46
|
+
const computed = (10 - (sum % 10)) % 10;
|
|
47
|
+
return computed === checkDigit;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Normalize a manufacturer-supplied GTIN to its canonical GTIN-14 form.
|
|
52
|
+
*
|
|
53
|
+
* Trims surrounding whitespace, validates that the value is a digits-only string
|
|
54
|
+
* of a permitted GTIN length with a correct mod-10 check digit, then left-pads
|
|
55
|
+
* with zeros to 14 digits.
|
|
56
|
+
*
|
|
57
|
+
* @throws Error with a descriptive message on invalid characters, length, or check digit.
|
|
58
|
+
*/
|
|
59
|
+
export function normalizeToGtin14(rawGtin: string): string {
|
|
60
|
+
const gtin = rawGtin.trim();
|
|
61
|
+
if (!DIGITS_ONLY.test(gtin)) {
|
|
62
|
+
throw new Error("GTIN must contain digits only");
|
|
63
|
+
}
|
|
64
|
+
if (!VALID_GTIN_LENGTHS.has(gtin.length)) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
`GTIN must have a valid length (8, 12, 13, or 14 digits); received ${gtin.length}`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
if (!isValidGtinCheckDigit(gtin)) {
|
|
70
|
+
throw new Error("GTIN has an invalid check digit");
|
|
71
|
+
}
|
|
72
|
+
return gtin.padStart(GTIN14_LENGTH, "0");
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Zod schema for raw GTIN *input*: accepts any valid GTIN length and transforms
|
|
77
|
+
* it into a normalized, check-digit-valid GTIN-14. Use this at write boundaries
|
|
78
|
+
* (backoffice form, create/update request).
|
|
79
|
+
*/
|
|
80
|
+
export const GtinInputSchema = z.string().transform((value, ctx) => {
|
|
81
|
+
try {
|
|
82
|
+
return normalizeToGtin14(value);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
ctx.addIssue({
|
|
85
|
+
code: "custom",
|
|
86
|
+
message: error instanceof Error ? error.message : "Invalid GTIN",
|
|
87
|
+
});
|
|
88
|
+
return z.NEVER;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Zod schema for an already-normalized GTIN-14 (exactly 14 digits, valid check
|
|
94
|
+
* digit). Use this where the value is expected to be canonical (stored value,
|
|
95
|
+
* domain reconstruction, DTO output).
|
|
96
|
+
*/
|
|
97
|
+
export const Gtin14Schema = z
|
|
98
|
+
.string()
|
|
99
|
+
.length(GTIN14_LENGTH, "GTIN-14 must be exactly 14 digits")
|
|
100
|
+
.regex(DIGITS_ONLY, "GTIN-14 must contain digits only")
|
|
101
|
+
.refine(isValidGtinCheckDigit, { message: "GTIN-14 has an invalid check digit" });
|
|
102
|
+
|
|
103
|
+
export type Gtin14 = z.infer<typeof Gtin14Schema>;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Maximum length of an alphanumeric GS1 component (batch `10`, serial `21`).
|
|
107
|
+
* GS1 General Specifications cap these AIs at 20 characters.
|
|
108
|
+
*/
|
|
109
|
+
export const GS1_CSET82_MAX_LENGTH = 20;
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Upper bound on the length of a GS1 data-attribute value, enforced *before* the
|
|
113
|
+
* per-AI regex runs.
|
|
114
|
+
*
|
|
115
|
+
* Some data-attribute regexes (the "X..90" AIs — full name 7256, company-internal
|
|
116
|
+
* 91-99, etc.) alternate a single-char class with a `%HH%HH` percent block, and
|
|
117
|
+
* `%` matches both branches. On a long ambiguous input that ultimately fails, that
|
|
118
|
+
* ambiguity makes the backtracking blow up super-linearly (a ~120-char value can
|
|
119
|
+
* pin a core for seconds). 90 is the largest quantifier bound across every type-'D'
|
|
120
|
+
* AI in the vendored table, so no valid raw value is longer — values are validated
|
|
121
|
+
* in raw form here; percent-encoding happens later in buildGs1DataAttributeQuery.
|
|
122
|
+
* gs1-digital-link.spec.ts asserts this bound stays >= the table's true maximum.
|
|
123
|
+
*/
|
|
124
|
+
export const GS1_DATA_ATTRIBUTE_MAX_LENGTH = 90;
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* GS1 CSET-82: the 82 characters permitted in alphanumeric AI values. Beyond the
|
|
128
|
+
* 62 alphanumerics (0-9, A-Z, a-z) it adds these 20 special characters:
|
|
129
|
+
* `! " % & ' ( ) * + , - . / : ; < = > ? _`. Anchored, so the whole string must
|
|
130
|
+
* consist only of CSET-82 characters.
|
|
131
|
+
*/
|
|
132
|
+
const CSET82 = /^[0-9A-Za-z!"%&'()*+,\-./:;<=>?_]+$/;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* "." and ".." are CSET-82, but as full URL path segments every RFC 3986
|
|
136
|
+
* normalizer (browsers, `new URL()`, most servers) collapses them before the
|
|
137
|
+
* resolver sees the path, so a dot-only batch/serial would silently rewrite
|
|
138
|
+
* the link and resolve to a different identity. Rejected outright —
|
|
139
|
+
* percent-encoding does not help ("%2E" forms are collapsed too).
|
|
140
|
+
*/
|
|
141
|
+
const DOT_SEGMENT = /^\.{1,2}$/;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Validate a single alphanumeric GS1 component (batch / serial) against CSET-82
|
|
145
|
+
* and the ≤ 20 character limit. Empty is invalid (an absent component is modeled
|
|
146
|
+
* by omitting it, not by an empty string), as are the URL dot-segments "." / "..".
|
|
147
|
+
*/
|
|
148
|
+
export function isValidCset82Component(value: string): boolean {
|
|
149
|
+
return (
|
|
150
|
+
value.length >= 1 &&
|
|
151
|
+
value.length <= GS1_CSET82_MAX_LENGTH &&
|
|
152
|
+
CSET82.test(value) &&
|
|
153
|
+
!DOT_SEGMENT.test(value)
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Zod schema for a batch / serial component: a non-empty CSET-82 string of at most
|
|
159
|
+
* 20 characters, excluding the URL dot-segments "." / "..". Use this at write
|
|
160
|
+
* boundaries (backoffice form, create/update).
|
|
161
|
+
*/
|
|
162
|
+
export const Cset82ComponentSchema = z
|
|
163
|
+
.string()
|
|
164
|
+
.min(1, "must not be empty")
|
|
165
|
+
.max(GS1_CSET82_MAX_LENGTH, `must be at most ${GS1_CSET82_MAX_LENGTH} characters`)
|
|
166
|
+
.regex(CSET82, "must contain only GS1 CSET-82 characters")
|
|
167
|
+
.refine((value) => !DOT_SEGMENT.test(value), { message: 'must not be "." or ".."' });
|
|
168
|
+
|
|
169
|
+
export type Cset82Component = z.infer<typeof Cset82ComponentSchema>;
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Write-boundary schema for an optional batch / serial. Trims surrounding
|
|
173
|
+
* whitespace and coerces a blank value to `undefined` ("clear this component"); a
|
|
174
|
+
* non-blank value must satisfy CSET-82 and the length cap. Use this on request
|
|
175
|
+
* bodies where omitting or blanking a component means "no batch / serial".
|
|
176
|
+
*/
|
|
177
|
+
export const Cset82ComponentInputSchema = z
|
|
178
|
+
.string()
|
|
179
|
+
.transform((value) => value.trim())
|
|
180
|
+
.transform((value) => (value.length === 0 ? undefined : value))
|
|
181
|
+
.pipe(Cset82ComponentSchema.optional());
|
|
182
|
+
|
|
183
|
+
/** The GS1 Application Identifier for a GTIN. Compat alias for {@link Gs1KeyAi.GLOBAL_TRADE_ITEM_NUMBER}. */
|
|
184
|
+
export const GS1_AI_GTIN = Gs1KeyAi.GLOBAL_TRADE_ITEM_NUMBER;
|
|
185
|
+
/** The GS1 Application Identifier for a batch / lot number. Compat alias for {@link Gs1QualifierAi.BATCH_OR_LOT_NUMBER}. */
|
|
186
|
+
export const GS1_AI_BATCH = Gs1QualifierAi.BATCH_OR_LOT_NUMBER;
|
|
187
|
+
/** The GS1 Application Identifier for a serial number. Compat alias for {@link Gs1QualifierAi.SERIAL_NUMBER}. */
|
|
188
|
+
export const GS1_AI_SERIAL = Gs1QualifierAi.SERIAL_NUMBER;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Type guard: true when `ai` is a known GS1 data-attribute (non-key) Application
|
|
192
|
+
* Identifier — i.e. `type === 'D'` in the vendored GS1 AI table.
|
|
193
|
+
*
|
|
194
|
+
* Returns false for primary identifiers (type I), key qualifiers (type Q),
|
|
195
|
+
* unknown AIs, and any non-string/empty input.
|
|
196
|
+
*/
|
|
197
|
+
export function isGs1DataAttributeAi(ai: string): ai is Gs1DataAttributeAi {
|
|
198
|
+
// typeof guard: a plain-JS caller passing e.g. 17 would coerce to "17" on lookup.
|
|
199
|
+
return typeof ai === "string" && GS1_AI_TABLE[ai]?.type === "D";
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Returns true when `value` is a valid value for the given GS1 data-attribute
|
|
204
|
+
* Application Identifier `ai`.
|
|
205
|
+
*
|
|
206
|
+
* Validates by:
|
|
207
|
+
* 1. Looking up `ai` in the vendored GS1 AI table; returns false if absent or
|
|
208
|
+
* if the entry is not a data attribute (type !== 'D').
|
|
209
|
+
* 2. Rejecting an empty `value` (length must be ≥ 1).
|
|
210
|
+
* 3. Compiling an anchored `RegExp` from the table entry's `regex` fragment.
|
|
211
|
+
*
|
|
212
|
+
* Pure function, no I/O, no mutation.
|
|
213
|
+
*/
|
|
214
|
+
export function isValidGs1DataAttributeValue(ai: string, value: string): boolean {
|
|
215
|
+
// typeof guards: plain-JS callers passing numbers would coerce on lookup/test.
|
|
216
|
+
if (typeof ai !== "string" || typeof value !== "string") {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
const entry = GS1_AI_TABLE[ai];
|
|
220
|
+
if (!entry || entry.type !== "D") {
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
// Reject empty and over-long values before compiling/running the regex: the
|
|
224
|
+
// per-AI regex can backtrack catastrophically on long ambiguous input, and no
|
|
225
|
+
// valid value exceeds the cap. See GS1_DATA_ATTRIBUTE_MAX_LENGTH.
|
|
226
|
+
if (value.length < 1 || value.length > GS1_DATA_ATTRIBUTE_MAX_LENGTH) {
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
return new RegExp("^(?:" + entry.regex + ")$").test(value);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Percent-encode a GS1 component value for a URI. `encodeURIComponent` plus the
|
|
234
|
+
* four RFC 3986 sub-delims it leaves raw (`!'()*`) — all CSET-82 characters —
|
|
235
|
+
* so the emitted link is canonical and stable across URL libraries.
|
|
236
|
+
*/
|
|
237
|
+
function encodeRfc3986(value: string): string {
|
|
238
|
+
return encodeURIComponent(value).replace(
|
|
239
|
+
/[!'()*]/g,
|
|
240
|
+
(char) => `%${char.charCodeAt(0).toString(16).toUpperCase()}`,
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Build the canonical GS1 data-attribute query string from a map of validated AI → value pairs.
|
|
246
|
+
*
|
|
247
|
+
* - Returns `''` when `attributes` is empty, null, or undefined.
|
|
248
|
+
* - Iterates keys in ascending ASCII (lexicographic) order for deterministic output.
|
|
249
|
+
* - Validates each AI via `isGs1DataAttributeAi` and each value via `isValidGs1DataAttributeValue`;
|
|
250
|
+
* throws `Error` on the first invalid entry.
|
|
251
|
+
* - Percent-encodes values RFC 3986-canonically (including `!'()*`).
|
|
252
|
+
* - Prefixes the result with `'?'` only when at least one pair is present.
|
|
253
|
+
*
|
|
254
|
+
* Pure function, no I/O, no mutation.
|
|
255
|
+
*/
|
|
256
|
+
export function buildGs1DataAttributeQuery(
|
|
257
|
+
attributes: Record<string, string> | null | undefined,
|
|
258
|
+
): string {
|
|
259
|
+
// Use Object.entries (value typed as string) rather than keys + index access, so the
|
|
260
|
+
// helper type-checks under a consumer with noUncheckedIndexedAccess (e.g. apps/client,
|
|
261
|
+
// which type-checks this source directly). Sort ascending by AI for a canonical query.
|
|
262
|
+
const pairs = Object.entries(attributes ?? {})
|
|
263
|
+
.sort(([a], [b]) => (a < b ? -1 : 1))
|
|
264
|
+
.map(([ai, value]) => {
|
|
265
|
+
if (!isGs1DataAttributeAi(ai)) {
|
|
266
|
+
throw new Error(`"${ai}" is not a known GS1 data-attribute AI`);
|
|
267
|
+
}
|
|
268
|
+
if (!isValidGs1DataAttributeValue(ai, value)) {
|
|
269
|
+
throw new Error(`value for AI "${ai}" is invalid: "${value}"`);
|
|
270
|
+
}
|
|
271
|
+
return `${ai}=${encodeRfc3986(value)}`;
|
|
272
|
+
});
|
|
273
|
+
return pairs.length === 0 ? "" : "?" + pairs.join("&");
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export interface Gs1DigitalLinkParts {
|
|
277
|
+
/** A GTIN; normalized to GTIN-14 before assembly. */
|
|
278
|
+
gtin: string;
|
|
279
|
+
/** Optional batch / lot (AI `10`), CSET-82, ≤ 20 chars. */
|
|
280
|
+
batch?: string | null;
|
|
281
|
+
/** Optional serial (AI `21`), CSET-82, ≤ 20 chars. */
|
|
282
|
+
serial?: string | null;
|
|
283
|
+
/**
|
|
284
|
+
* Optional GS1 data-attribute map (non-key AIs only). When set, the pairs are
|
|
285
|
+
* sorted in ascending AI order, validated, and appended as a query string after
|
|
286
|
+
* the last path segment. Pass `null` or omit to emit no query string.
|
|
287
|
+
*/
|
|
288
|
+
dataAttributes?: Record<string, string> | null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Normalize an optional batch/serial input to either a validated component or
|
|
293
|
+
* `undefined`. Trims surrounding whitespace; an empty/blank value is treated as
|
|
294
|
+
* absent.
|
|
295
|
+
*
|
|
296
|
+
* @throws Error when a non-empty value violates CSET-82 or the length cap.
|
|
297
|
+
*/
|
|
298
|
+
function normalizeOptionalComponent(
|
|
299
|
+
value: string | null | undefined,
|
|
300
|
+
label: string,
|
|
301
|
+
): string | undefined {
|
|
302
|
+
const trimmed = value?.trim();
|
|
303
|
+
if (!trimmed) {
|
|
304
|
+
return undefined;
|
|
305
|
+
}
|
|
306
|
+
if (!isValidCset82Component(trimmed)) {
|
|
307
|
+
throw new Error(
|
|
308
|
+
`${label} must be a GS1 CSET-82 string of at most ${GS1_CSET82_MAX_LENGTH} characters and not "." or ".."`,
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
return trimmed;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Build the uncompressed, canonical GS1 Digital Link for a GS1 identity.
|
|
316
|
+
*
|
|
317
|
+
* Emits the GTIN path and, when present, appends the batch (AI `10`) and serial
|
|
318
|
+
* (AI `21`) segments in canonical GS1 order `01 -> 10 -> 21`. A blank batch/serial
|
|
319
|
+
* is treated as absent. The resolver base is canonicalised (host lowercased,
|
|
320
|
+
* trailing slash dropped) and any path it carries is preserved; batch/serial
|
|
321
|
+
* values are percent-encoded for the path.
|
|
322
|
+
*
|
|
323
|
+
* @throws Error when the GTIN is invalid or a batch/serial violates CSET-82 /
|
|
324
|
+
* length / the dot-segment rule.
|
|
325
|
+
*/
|
|
326
|
+
export function buildGs1DigitalLink(resolverBase: string, parts: Gs1DigitalLinkParts): string {
|
|
327
|
+
const gtin14 = normalizeToGtin14(parts.gtin);
|
|
328
|
+
const batch = normalizeOptionalComponent(parts.batch, "Batch");
|
|
329
|
+
const serial = normalizeOptionalComponent(parts.serial, "Serial");
|
|
330
|
+
const base = canonicaliseBaseUrl(resolverBase);
|
|
331
|
+
|
|
332
|
+
let url = `${base}/${GS1_AI_GTIN}/${gtin14}`;
|
|
333
|
+
if (batch !== undefined) {
|
|
334
|
+
url += `/${GS1_AI_BATCH}/${encodeRfc3986(batch)}`;
|
|
335
|
+
}
|
|
336
|
+
if (serial !== undefined) {
|
|
337
|
+
url += `/${GS1_AI_SERIAL}/${encodeRfc3986(serial)}`;
|
|
338
|
+
}
|
|
339
|
+
url += buildGs1DataAttributeQuery(parts.dataAttributes);
|
|
340
|
+
return url;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/**
|
|
344
|
+
* Format a GS1 identity as its human-readable GS1 element string, e.g.
|
|
345
|
+
* `(01) 04006381333931 (10) LOT-42 (21) SN-001`.
|
|
346
|
+
*
|
|
347
|
+
* Each present Application Identifier is rendered as its parenthesised AI followed
|
|
348
|
+
* by the value, in canonical GS1 order `01 -> 10 -> 21`. Unlike the Digital Link,
|
|
349
|
+
* the element string is for human display only, so batch/serial values are emitted
|
|
350
|
+
* verbatim (no percent-encoding). A blank batch/serial is treated as absent.
|
|
351
|
+
*
|
|
352
|
+
* @throws Error when the GTIN is invalid or a batch/serial violates CSET-82 /
|
|
353
|
+
* length / the dot-segment rule.
|
|
354
|
+
*/
|
|
355
|
+
export function formatGs1ElementString(parts: Gs1DigitalLinkParts): string {
|
|
356
|
+
const gtin14 = normalizeToGtin14(parts.gtin);
|
|
357
|
+
const batch = normalizeOptionalComponent(parts.batch, "Batch");
|
|
358
|
+
const serial = normalizeOptionalComponent(parts.serial, "Serial");
|
|
359
|
+
|
|
360
|
+
const segments = [`(${GS1_AI_GTIN}) ${gtin14}`];
|
|
361
|
+
if (batch !== undefined) {
|
|
362
|
+
segments.push(`(${GS1_AI_BATCH}) ${batch}`);
|
|
363
|
+
}
|
|
364
|
+
if (serial !== undefined) {
|
|
365
|
+
segments.push(`(${GS1_AI_SERIAL}) ${serial}`);
|
|
366
|
+
}
|
|
367
|
+
return segments.join(" ");
|
|
368
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
Cset82ComponentInputSchema,
|
|
4
|
+
Cset82ComponentSchema,
|
|
5
|
+
Gtin14Schema,
|
|
6
|
+
GtinInputSchema,
|
|
7
|
+
} from "./gs1-digital-link";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The persisted GS1 identity value object carried by a `GS1` UPI.
|
|
11
|
+
*
|
|
12
|
+
* A GTIN (stored normalized to GTIN-14) plus an optional batch (AI `10`) and/or
|
|
13
|
+
* serial (AI `21`), each a CSET-82 string of at most 20 characters. Granularity
|
|
14
|
+
* is implied by which keys are present: GTIN ⇒ model, +batch ⇒ batch, +serial ⇒ item.
|
|
15
|
+
*/
|
|
16
|
+
export const Gs1IdentityDtoSchema = z
|
|
17
|
+
.object({
|
|
18
|
+
gtin: Gtin14Schema,
|
|
19
|
+
batch: Cset82ComponentSchema.nullish(),
|
|
20
|
+
serial: Cset82ComponentSchema.nullish(),
|
|
21
|
+
})
|
|
22
|
+
.meta({ id: "Gs1Identity" });
|
|
23
|
+
|
|
24
|
+
export type Gs1IdentityDto = z.infer<typeof Gs1IdentityDtoSchema>;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Request body to set a passport's GS1 identity. Accepts a raw GTIN of any valid
|
|
28
|
+
* length (normalized to GTIN-14) and an optional batch / serial. An empty-string
|
|
29
|
+
* batch/serial is treated as "clear this component".
|
|
30
|
+
*/
|
|
31
|
+
export const Gs1IdentityRequestSchema = z
|
|
32
|
+
.object({
|
|
33
|
+
gtin: GtinInputSchema,
|
|
34
|
+
batch: Cset82ComponentInputSchema.optional(),
|
|
35
|
+
serial: Cset82ComponentInputSchema.optional(),
|
|
36
|
+
})
|
|
37
|
+
.meta({ id: "Gs1IdentityRequest" });
|
|
38
|
+
|
|
39
|
+
export type Gs1IdentityRequest = z.input<typeof Gs1IdentityRequestSchema>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Response describing a passport's GS1 identity plus the server-assembled,
|
|
43
|
+
* uncompressed GS1 Digital Link that a Data Carrier (QR) should encode.
|
|
44
|
+
*/
|
|
45
|
+
export const Gs1IdentityResponseSchema = Gs1IdentityDtoSchema.extend({
|
|
46
|
+
uuid: z.uuid(),
|
|
47
|
+
referenceId: z.uuid(),
|
|
48
|
+
digitalLink: z.url(),
|
|
49
|
+
}).meta({ id: "Gs1IdentityResponse" });
|
|
50
|
+
|
|
51
|
+
export type Gs1IdentityResponse = z.infer<typeof Gs1IdentityResponseSchema>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Discriminates the kind of identifier carried by a UniqueProductIdentifier.
|
|
5
|
+
*
|
|
6
|
+
* - `OPEN_DPP_UUID` — system-generated UPI; no external identity data.
|
|
7
|
+
* - `GS1` — GS1 identity: GTIN-14 + optional batch/serial.
|
|
8
|
+
* - `GTIN` — GTIN/EAN read-only system row; not creatable via the API.
|
|
9
|
+
* - `EAN` — EAN read-only system row; not creatable via the API.
|
|
10
|
+
*/
|
|
11
|
+
export const UniqueProductIdentifierType = {
|
|
12
|
+
OPEN_DPP_UUID: "OPEN_DPP_UUID",
|
|
13
|
+
GS1: "GS1",
|
|
14
|
+
GTIN: "GTIN",
|
|
15
|
+
EAN: "EAN",
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export const UniqueProductIdentifierTypeSchema = z.enum(UniqueProductIdentifierType);
|
|
19
|
+
|
|
20
|
+
export type UniqueProductIdentifierTypeValue = z.infer<typeof UniqueProductIdentifierTypeSchema>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The GS1 granularity level implied by which key qualifiers are present on a UPI.
|
|
24
|
+
*
|
|
25
|
+
* - `model` — bare GTIN only (product model)
|
|
26
|
+
* - `batch` — GTIN + batch/lot (AI `10`)
|
|
27
|
+
* - `item` — GTIN + serial (AI `21`), optionally also batch
|
|
28
|
+
*/
|
|
29
|
+
export const Gs1GranularitySchema = z.enum(["model", "batch", "item"]);
|
|
30
|
+
|
|
31
|
+
export type Gs1Granularity = z.infer<typeof Gs1GranularitySchema>;
|