Skip to content

Commit 39fde46

Browse files
committed
fix: enforce group prefix validation
1 parent 68d9c6f commit 39fde46

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/strategies/prefix-groups-changelog.strategy.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export class PrefixGroupsChangelogStrategy implements BuilderStrategy {
2222
const { packageId, version } = config
2323
const { compareContext } = contexts
2424

25+
// Validate groups
26+
this.validateGroup(config.currentGroup, 'currentGroup')
27+
this.validateGroup(config.previousGroup, 'previousGroup')
28+
2529
buildResult.comparisons = await compareVersions(
2630
[version, packageId],
2731
[version, packageId],
@@ -30,4 +34,18 @@ export class PrefixGroupsChangelogStrategy implements BuilderStrategy {
3034

3135
return buildResult
3236
}
37+
38+
private validateGroup(group: unknown, paramName: string): void {
39+
if (group === undefined) {
40+
return
41+
}
42+
43+
if (typeof group !== 'string') {
44+
throw new Error(`${paramName} must be a string, received: ${typeof group}`)
45+
}
46+
47+
if (group.length < 3 || !group.startsWith('/') || !group.endsWith('/')) {
48+
throw new Error(`${paramName} must begin and end with a "/" character and contain at least one meaningful character, received: "${group}"`)
49+
}
50+
}
3351
}

test/helpers/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ export async function buildPrefixGroupChangelogPackage(options: {
163163
packageId,
164164
config: {
165165
files = [{ fileId: 'spec.yaml' }],
166-
currentGroup = '/api/v2',
167-
previousGroup = 'api/v1',
166+
currentGroup = '/api/v2/',
167+
previousGroup = '/api/v1/',
168168
} = {},
169169
} = options ?? {}
170170

test/prefix-groups.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ describe('Prefix Groups test', () => {
263263
const result = await buildPrefixGroupChangelogPackage({
264264
packageId: 'prefix-groups/different-prefix-length',
265265
config: {
266-
previousGroup: '/api/v10',
267-
currentGroup: 'api/v1000/',
266+
previousGroup: '/api/v10/',
267+
currentGroup: '/api/v1000/',
268268
},
269269
})
270270

@@ -280,5 +280,37 @@ describe('Prefix Groups test', () => {
280280
]))
281281
})
282282

283+
describe('Validation of incorrect group prefixes', () => {
284+
test('should throw error for invalid currentGroup - missing ending slash', async () => {
285+
await expect(buildPrefixGroupChangelogPackage({
286+
packageId: 'prefix-groups/different-prefix-length',
287+
config: {
288+
previousGroup: '/api/v10/',
289+
currentGroup: '/api/v1000',
290+
},
291+
})).rejects.toThrow('currentGroup must begin and end with a "/" character and contain at least one meaningful character, received: "/api/v1000"')
292+
})
293+
294+
test('should throw error for invalid previousGroup - missing starting slash', async () => {
295+
await expect(buildPrefixGroupChangelogPackage({
296+
packageId: 'prefix-groups/different-prefix-length',
297+
config: {
298+
previousGroup: 'api/v10/',
299+
currentGroup: '/api/v1000/',
300+
},
301+
})).rejects.toThrow('previousGroup must begin and end with a "/" character and contain at least one meaningful character, received: "api/v10/"')
302+
})
303+
304+
test('should throw error for group that is too short', async () => {
305+
await expect(buildPrefixGroupChangelogPackage({
306+
packageId: 'prefix-groups/different-prefix-length',
307+
config: {
308+
previousGroup: '//',
309+
currentGroup: '/api/v1000/',
310+
},
311+
})).rejects.toThrow('previousGroup must begin and end with a "/" character and contain at least one meaningful character, received: "//"')
312+
})
313+
})
314+
283315
// todo add case when api/v1 in servers and api/v2 in some paths?
284316
})

0 commit comments

Comments
 (0)