@open-dpp/dto 0.5.0 → 1.0.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/element-classifications.d.ts +6 -0
- package/dist/aas/element-classifications.d.ts.map +1 -0
- package/dist/aas/element-classifications.js +33 -0
- package/dist/analytics/passport-page-view.dto.d.ts +7 -0
- package/dist/analytics/passport-page-view.dto.d.ts.map +1 -0
- package/dist/analytics/passport-page-view.dto.js +9 -0
- package/dist/branding/branding.dto.d.ts +1 -0
- package/dist/branding/branding.dto.d.ts.map +1 -1
- package/dist/branding/branding.dto.js +2 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/instance-settings/instance-settings.dto.d.ts +11 -0
- package/dist/instance-settings/instance-settings.dto.d.ts.map +1 -1
- package/dist/instance-settings/instance-settings.dto.js +9 -1
- package/dist/permalinks/permalink.dto.d.ts +161 -0
- package/dist/permalinks/permalink.dto.d.ts.map +1 -0
- package/dist/permalinks/permalink.dto.js +62 -0
- package/dist/presentation-configurations/presentation-configuration.dto.d.ts +187 -0
- package/dist/presentation-configurations/presentation-configuration.dto.d.ts.map +1 -0
- package/dist/presentation-configurations/presentation-configuration.dto.js +71 -0
- package/dist/shared/digital-product-document.schemas.d.ts.map +1 -1
- package/dist/shared/digital-product-document.schemas.js +16 -1
- package/dist/shared/permalink-base-url.schema.d.ts +5 -0
- package/dist/shared/permalink-base-url.schema.d.ts.map +1 -0
- package/dist/shared/permalink-base-url.schema.js +48 -0
- package/package.json +1 -1
- package/src/aas/element-classifications.ts +32 -0
- package/src/analytics/passport-page-view.dto.ts +9 -0
- package/src/branding/branding.dto.ts +2 -0
- package/src/index.ts +5 -0
- package/src/instance-settings/instance-settings.dto.ts +11 -0
- package/src/permalinks/permalink.dto.spec.ts +206 -0
- package/src/permalinks/permalink.dto.ts +87 -0
- package/src/presentation-configurations/presentation-configuration.dto.spec.ts +165 -0
- package/src/presentation-configurations/presentation-configuration.dto.ts +110 -0
- package/src/shared/digital-product-document.schemas.ts +20 -4
- package/src/shared/permalink-base-url.schema.spec.ts +95 -0
- package/src/shared/permalink-base-url.schema.ts +53 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { describe, expect, it } from "@jest/globals";
|
|
3
|
+
import { KeyTypes } from "../aas/enums/key-types-enum";
|
|
4
|
+
import {
|
|
5
|
+
PresentationComponentName,
|
|
6
|
+
PresentationConfigurationCreateRequestSchema,
|
|
7
|
+
PresentationConfigurationDtoSchema,
|
|
8
|
+
PresentationConfigurationExportSchema,
|
|
9
|
+
PresentationConfigurationPatchSchema,
|
|
10
|
+
PresentationReferenceType,
|
|
11
|
+
} from "./presentation-configuration.dto";
|
|
12
|
+
|
|
13
|
+
const validBase = () => ({
|
|
14
|
+
id: randomUUID(),
|
|
15
|
+
organizationId: "org-1",
|
|
16
|
+
referenceId: randomUUID(),
|
|
17
|
+
referenceType: PresentationReferenceType.Template,
|
|
18
|
+
label: null,
|
|
19
|
+
createdAt: new Date().toISOString(),
|
|
20
|
+
updatedAt: new Date().toISOString(),
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("PresentationConfigurationDtoSchema (tolerant load)", () => {
|
|
24
|
+
it("silently drops elementDesign entries with unknown component names", () => {
|
|
25
|
+
const parsed = PresentationConfigurationDtoSchema.parse({
|
|
26
|
+
...validBase(),
|
|
27
|
+
elementDesign: {
|
|
28
|
+
"sm.p": "Ghost",
|
|
29
|
+
"sm.q": PresentationComponentName.BigNumber,
|
|
30
|
+
},
|
|
31
|
+
defaultComponents: {},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(parsed.elementDesign).toEqual({ "sm.q": PresentationComponentName.BigNumber });
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("silently drops defaultComponents entries for unknown KeyTypes or unknown values", () => {
|
|
38
|
+
const parsed = PresentationConfigurationDtoSchema.parse({
|
|
39
|
+
...validBase(),
|
|
40
|
+
elementDesign: {},
|
|
41
|
+
defaultComponents: {
|
|
42
|
+
NotAKeyType: PresentationComponentName.BigNumber,
|
|
43
|
+
[KeyTypes.Property]: "AlsoGone",
|
|
44
|
+
[KeyTypes.File]: PresentationComponentName.BigNumber,
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
expect(parsed.defaultComponents).toEqual({
|
|
49
|
+
[KeyTypes.File]: PresentationComponentName.BigNumber,
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("accepts empty maps via schema defaults", () => {
|
|
54
|
+
const parsed = PresentationConfigurationDtoSchema.parse({
|
|
55
|
+
...validBase(),
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(parsed.elementDesign).toEqual({});
|
|
59
|
+
expect(parsed.defaultComponents).toEqual({});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("PresentationConfigurationExportSchema (tolerant import)", () => {
|
|
64
|
+
it("silently drops unknown entries", () => {
|
|
65
|
+
const parsed = PresentationConfigurationExportSchema.parse({
|
|
66
|
+
elementDesign: {
|
|
67
|
+
keep: PresentationComponentName.BigNumber,
|
|
68
|
+
drop: "LegacyTextField",
|
|
69
|
+
},
|
|
70
|
+
defaultComponents: {
|
|
71
|
+
UnknownKeyType: PresentationComponentName.BigNumber,
|
|
72
|
+
[KeyTypes.Property]: PresentationComponentName.BigNumber,
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(parsed.elementDesign).toEqual({ keep: PresentationComponentName.BigNumber });
|
|
77
|
+
expect(parsed.defaultComponents).toEqual({
|
|
78
|
+
[KeyTypes.Property]: PresentationComponentName.BigNumber,
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
describe("PresentationConfigurationPatchSchema (strict write)", () => {
|
|
84
|
+
it("accepts valid component names and null-for-delete", () => {
|
|
85
|
+
const parsed = PresentationConfigurationPatchSchema.parse({
|
|
86
|
+
elementDesign: {
|
|
87
|
+
"sm.set": PresentationComponentName.BigNumber,
|
|
88
|
+
"sm.clear": null,
|
|
89
|
+
},
|
|
90
|
+
defaultComponents: {
|
|
91
|
+
[KeyTypes.Property]: PresentationComponentName.BigNumber,
|
|
92
|
+
[KeyTypes.File]: null,
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
expect(parsed.elementDesign).toEqual({
|
|
97
|
+
"sm.set": PresentationComponentName.BigNumber,
|
|
98
|
+
"sm.clear": null,
|
|
99
|
+
});
|
|
100
|
+
expect(parsed.defaultComponents).toEqual({
|
|
101
|
+
[KeyTypes.Property]: PresentationComponentName.BigNumber,
|
|
102
|
+
[KeyTypes.File]: null,
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("rejects unknown component names in elementDesign", () => {
|
|
107
|
+
const result = PresentationConfigurationPatchSchema.safeParse({
|
|
108
|
+
elementDesign: { "sm.p": "NotARealComponent" },
|
|
109
|
+
});
|
|
110
|
+
expect(result.success).toBe(false);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("rejects unknown KeyType keys in defaultComponents", () => {
|
|
114
|
+
const result = PresentationConfigurationPatchSchema.safeParse({
|
|
115
|
+
defaultComponents: { NotAKeyType: PresentationComponentName.BigNumber },
|
|
116
|
+
});
|
|
117
|
+
expect(result.success).toBe(false);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("accepts an empty patch body", () => {
|
|
121
|
+
const result = PresentationConfigurationPatchSchema.safeParse({});
|
|
122
|
+
expect(result.success).toBe(true);
|
|
123
|
+
expect(result.data).toEqual({});
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
describe("PresentationConfigurationDtoSchema label", () => {
|
|
128
|
+
it("accepts null label", () => {
|
|
129
|
+
const parsed = PresentationConfigurationDtoSchema.parse({
|
|
130
|
+
...validBase(),
|
|
131
|
+
label: null,
|
|
132
|
+
});
|
|
133
|
+
expect(parsed.label).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("accepts non-empty string label", () => {
|
|
137
|
+
const parsed = PresentationConfigurationDtoSchema.parse({
|
|
138
|
+
...validBase(),
|
|
139
|
+
label: "Variant A",
|
|
140
|
+
});
|
|
141
|
+
expect(parsed.label).toBe("Variant A");
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("rejects an empty string label", () => {
|
|
145
|
+
const result = PresentationConfigurationDtoSchema.safeParse({
|
|
146
|
+
...validBase(),
|
|
147
|
+
label: "",
|
|
148
|
+
});
|
|
149
|
+
expect(result.success).toBe(false);
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
describe("PresentationConfigurationCreateRequestSchema", () => {
|
|
154
|
+
it("accepts a null label", () => {
|
|
155
|
+
expect(PresentationConfigurationCreateRequestSchema.parse({ label: null }).label).toBeNull();
|
|
156
|
+
});
|
|
157
|
+
it("accepts a string label", () => {
|
|
158
|
+
expect(PresentationConfigurationCreateRequestSchema.parse({ label: "v1" }).label).toBe("v1");
|
|
159
|
+
});
|
|
160
|
+
it("rejects an empty string label", () => {
|
|
161
|
+
expect(PresentationConfigurationCreateRequestSchema.safeParse({ label: "" }).success).toBe(
|
|
162
|
+
false,
|
|
163
|
+
);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { KeyTypes, KeyTypesEnum } from "../aas/enums/key-types-enum";
|
|
3
|
+
import { DateTimeSchema } from "../shared/digital-product-document.schemas";
|
|
4
|
+
|
|
5
|
+
export const PresentationReferenceType = {
|
|
6
|
+
Template: "template",
|
|
7
|
+
Passport: "passport",
|
|
8
|
+
} as const;
|
|
9
|
+
|
|
10
|
+
export const PresentationReferenceTypeEnum = z.enum(PresentationReferenceType);
|
|
11
|
+
export type PresentationReferenceTypeType = z.infer<typeof PresentationReferenceTypeEnum>;
|
|
12
|
+
|
|
13
|
+
export const PresentationComponentName = {
|
|
14
|
+
BigNumber: "BigNumber",
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
export const PresentationComponentNameEnum = z.enum(PresentationComponentName);
|
|
18
|
+
export type PresentationComponentNameType = z.infer<typeof PresentationComponentNameEnum>;
|
|
19
|
+
|
|
20
|
+
const KEY_TYPES_SET: ReadonlySet<string> = new Set(Object.values(KeyTypes));
|
|
21
|
+
|
|
22
|
+
function dropUnknownComponents(input: unknown, validKeys?: ReadonlySet<string>): unknown {
|
|
23
|
+
if (input == null || typeof input !== "object") return input;
|
|
24
|
+
const cleaned: Record<string, unknown> = {};
|
|
25
|
+
for (const [key, value] of Object.entries(input as Record<string, unknown>)) {
|
|
26
|
+
if (validKeys && !validKeys.has(key)) continue;
|
|
27
|
+
const parsed = PresentationComponentNameEnum.safeParse(value);
|
|
28
|
+
if (parsed.success) {
|
|
29
|
+
cleaned[key] = parsed.data;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return cleaned;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export const PresentationConfigurationDtoSchema = z
|
|
36
|
+
.object({
|
|
37
|
+
id: z.uuid(),
|
|
38
|
+
organizationId: z.string().min(1),
|
|
39
|
+
referenceId: z.uuid(),
|
|
40
|
+
referenceType: PresentationReferenceTypeEnum,
|
|
41
|
+
label: z.string().min(1).nullable().default(null),
|
|
42
|
+
elementDesign: z
|
|
43
|
+
.preprocess(
|
|
44
|
+
(input) => dropUnknownComponents(input),
|
|
45
|
+
z.record(z.string(), PresentationComponentNameEnum),
|
|
46
|
+
)
|
|
47
|
+
.default({}),
|
|
48
|
+
defaultComponents: z
|
|
49
|
+
.preprocess(
|
|
50
|
+
(input) => dropUnknownComponents(input, KEY_TYPES_SET),
|
|
51
|
+
z.partialRecord(KeyTypesEnum, PresentationComponentNameEnum),
|
|
52
|
+
)
|
|
53
|
+
.default({}),
|
|
54
|
+
createdAt: DateTimeSchema,
|
|
55
|
+
updatedAt: DateTimeSchema,
|
|
56
|
+
})
|
|
57
|
+
.meta({ id: "PresentationConfiguration" });
|
|
58
|
+
|
|
59
|
+
export type PresentationConfigurationDto = z.infer<typeof PresentationConfigurationDtoSchema>;
|
|
60
|
+
|
|
61
|
+
export const PresentationConfigurationInvariantsSchema = z.object({
|
|
62
|
+
organizationId: z.string().min(1),
|
|
63
|
+
referenceId: z.uuid(),
|
|
64
|
+
referenceType: PresentationReferenceTypeEnum,
|
|
65
|
+
label: z.string().min(1).nullable(),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export const PresentationConfigurationExportSchema = z.object({
|
|
69
|
+
elementDesign: z
|
|
70
|
+
.preprocess(
|
|
71
|
+
(input) => dropUnknownComponents(input),
|
|
72
|
+
z.record(z.string(), PresentationComponentNameEnum),
|
|
73
|
+
)
|
|
74
|
+
.default({}),
|
|
75
|
+
defaultComponents: z
|
|
76
|
+
.preprocess(
|
|
77
|
+
(input) => dropUnknownComponents(input, KEY_TYPES_SET),
|
|
78
|
+
z.partialRecord(KeyTypesEnum, PresentationComponentNameEnum),
|
|
79
|
+
)
|
|
80
|
+
.default({}),
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
export type PresentationConfigurationExportDto = z.infer<
|
|
84
|
+
typeof PresentationConfigurationExportSchema
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
export const PresentationConfigurationCreateRequestSchema = z.object({
|
|
88
|
+
label: z.string().min(1).nullable(),
|
|
89
|
+
});
|
|
90
|
+
export type PresentationConfigurationCreateRequestDto = z.infer<
|
|
91
|
+
typeof PresentationConfigurationCreateRequestSchema
|
|
92
|
+
>;
|
|
93
|
+
|
|
94
|
+
export const PresentationConfigurationListResponseSchema = z.array(
|
|
95
|
+
PresentationConfigurationDtoSchema,
|
|
96
|
+
);
|
|
97
|
+
export type PresentationConfigurationListResponseDto = z.infer<
|
|
98
|
+
typeof PresentationConfigurationListResponseSchema
|
|
99
|
+
>;
|
|
100
|
+
|
|
101
|
+
export const PresentationConfigurationPatchSchema = z.object({
|
|
102
|
+
elementDesign: z.record(z.string(), PresentationComponentNameEnum.nullable()).optional(),
|
|
103
|
+
defaultComponents: z
|
|
104
|
+
.partialRecord(KeyTypesEnum, PresentationComponentNameEnum.nullable())
|
|
105
|
+
.optional(),
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
export type PresentationConfigurationPatchDto = z.infer<
|
|
109
|
+
typeof PresentationConfigurationPatchSchema
|
|
110
|
+
>;
|
|
@@ -27,10 +27,26 @@ export const DigitalProductDocumentStatusModificationDtoEnum = z.enum(
|
|
|
27
27
|
DigitalProductDocumentStatusModificationMethodDto,
|
|
28
28
|
);
|
|
29
29
|
|
|
30
|
-
export const DigitalProductDocumentStatusChangeDtoSchema = z
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
export const DigitalProductDocumentStatusChangeDtoSchema = z
|
|
31
|
+
.object({
|
|
32
|
+
previousStatus: DigitalProductDocumentStatusDtoEnum.nullish(),
|
|
33
|
+
currentStatus: DigitalProductDocumentStatusDtoEnum,
|
|
34
|
+
})
|
|
35
|
+
.refine(
|
|
36
|
+
(change) => {
|
|
37
|
+
const { previousStatus, currentStatus } = change;
|
|
38
|
+
if (previousStatus == null) return true;
|
|
39
|
+
const { Draft, Published, Archived } = DigitalProductDocumentStatusDto;
|
|
40
|
+
if (previousStatus === Draft)
|
|
41
|
+
return currentStatus === Published || currentStatus === Archived;
|
|
42
|
+
if (previousStatus === Published) return currentStatus === Archived;
|
|
43
|
+
return currentStatus === Draft || currentStatus === Published;
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
error: "Illegal status transition",
|
|
47
|
+
path: ["currentStatus"],
|
|
48
|
+
},
|
|
49
|
+
);
|
|
34
50
|
|
|
35
51
|
export const DigitalProductDocumentStatusModificationDtoSchema = z.object({
|
|
36
52
|
method: DigitalProductDocumentStatusModificationDtoEnum,
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { describe, expect, it } from "@jest/globals";
|
|
2
|
+
import { PermalinkBaseUrlSchema } from "./permalink-base-url.schema";
|
|
3
|
+
|
|
4
|
+
describe("PermalinkBaseUrlSchema", () => {
|
|
5
|
+
describe("accepts", () => {
|
|
6
|
+
it.each([
|
|
7
|
+
"https://passports.example.com",
|
|
8
|
+
"http://localhost:3000",
|
|
9
|
+
"https://passport.brand.io:8443",
|
|
10
|
+
"https://xn--mller-kva.de",
|
|
11
|
+
"https://example.com/",
|
|
12
|
+
])("'%s'", (input) => {
|
|
13
|
+
const result = PermalinkBaseUrlSchema.safeParse(input);
|
|
14
|
+
expect(result.success).toBe(true);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it("canonicalises a trailing slash to no slash", () => {
|
|
18
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://Example.com/");
|
|
19
|
+
expect(result.success).toBe(true);
|
|
20
|
+
if (result.success) {
|
|
21
|
+
expect(result.data).toBe("https://example.com");
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("lowercases the host", () => {
|
|
26
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://PASS.ACME.COM");
|
|
27
|
+
expect(result.success).toBe(true);
|
|
28
|
+
if (result.success) {
|
|
29
|
+
expect(result.data).toBe("https://pass.acme.com");
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("preserves a non-default port", () => {
|
|
34
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://example.com:8443");
|
|
35
|
+
expect(result.success).toBe(true);
|
|
36
|
+
if (result.success) {
|
|
37
|
+
expect(result.data).toBe("https://example.com:8443");
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("accepts a path component and preserves it", () => {
|
|
42
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://example.com/p");
|
|
43
|
+
expect(result.success).toBe(true);
|
|
44
|
+
if (result.success) {
|
|
45
|
+
expect(result.data).toBe("https://example.com/p");
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("accepts a nested path and preserves it", () => {
|
|
50
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://example.com/dpp/v1");
|
|
51
|
+
expect(result.success).toBe(true);
|
|
52
|
+
if (result.success) {
|
|
53
|
+
expect(result.data).toBe("https://example.com/dpp/v1");
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("strips a trailing slash from a path", () => {
|
|
58
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://example.com/p/");
|
|
59
|
+
expect(result.success).toBe(true);
|
|
60
|
+
if (result.success) {
|
|
61
|
+
expect(result.data).toBe("https://example.com/p");
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("lowercases the host but preserves path case", () => {
|
|
66
|
+
const result = PermalinkBaseUrlSchema.safeParse("https://Example.COM/MyPath");
|
|
67
|
+
expect(result.success).toBe(true);
|
|
68
|
+
if (result.success) {
|
|
69
|
+
expect(result.data).toBe("https://example.com/MyPath");
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe("rejects", () => {
|
|
75
|
+
it.each([
|
|
76
|
+
["empty string", ""],
|
|
77
|
+
["bare hostname (no scheme)", "passports.example.com"],
|
|
78
|
+
["non-http scheme", "ftp://example.com"],
|
|
79
|
+
["data URL", "data:text/plain,hi"],
|
|
80
|
+
["query string", "https://example.com?q=1"],
|
|
81
|
+
["fragment", "https://example.com#h"],
|
|
82
|
+
["double slash in path", "https://example.com//p"],
|
|
83
|
+
["double slash mid-path", "https://example.com/dpp//v1"],
|
|
84
|
+
])("rejects %s ('%s')", (_label, input) => {
|
|
85
|
+
const result = PermalinkBaseUrlSchema.safeParse(input);
|
|
86
|
+
expect(result.success).toBe(false);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("rejects a value longer than 2048 chars", () => {
|
|
90
|
+
const long = "https://example.com/" + "a".repeat(2030);
|
|
91
|
+
const result = PermalinkBaseUrlSchema.safeParse(long);
|
|
92
|
+
expect(result.success).toBe(false);
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export function canonicaliseBaseUrl(s: string): string {
|
|
5
|
+
try {
|
|
6
|
+
const u = new URL(s);
|
|
7
|
+
u.hostname = u.hostname.toLowerCase();
|
|
8
|
+
const path = u.pathname.replace(/\/+$/, "");
|
|
9
|
+
return `${u.protocol}//${u.host}${path}`;
|
|
10
|
+
} catch {
|
|
11
|
+
return s;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const PermalinkBaseUrlSchema = z
|
|
16
|
+
.string()
|
|
17
|
+
.min(8)
|
|
18
|
+
.max(2048)
|
|
19
|
+
.url()
|
|
20
|
+
.refine(
|
|
21
|
+
(s) => {
|
|
22
|
+
try {
|
|
23
|
+
return ["http:", "https:"].includes(new URL(s).protocol);
|
|
24
|
+
} catch {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
{ message: "must use http or https" },
|
|
29
|
+
)
|
|
30
|
+
.refine(
|
|
31
|
+
(s) => {
|
|
32
|
+
try {
|
|
33
|
+
const u = new URL(s);
|
|
34
|
+
return !u.search && !u.hash;
|
|
35
|
+
} catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
{ message: "must not include query or fragment" },
|
|
40
|
+
)
|
|
41
|
+
.refine(
|
|
42
|
+
(s) => {
|
|
43
|
+
try {
|
|
44
|
+
return !new URL(s).pathname.includes("//");
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{ message: "path must not contain empty segments ('//')" },
|
|
50
|
+
)
|
|
51
|
+
.overwrite(canonicaliseBaseUrl);
|
|
52
|
+
|
|
53
|
+
export type PermalinkBaseUrl = z.infer<typeof PermalinkBaseUrlSchema>;
|