Skip to content

Commit a3a6a64

Browse files
committed
refactor: reuse group prefix validation function
1 parent 60fdc32 commit a3a6a64

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

src/apitypes/rest/rest.changes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import {
6161
extractRootSecurityDiffs,
6262
extractRootServersDiffs,
6363
getOperationBasePath,
64+
validateGroupPrefix,
6465
} from './rest.utils'
6566
import { createOperationChange, getOperationTags, OperationsMap } from '../../components'
6667

@@ -277,7 +278,7 @@ export function createCopyWithEmptyPathItems(template: RestOperationData): RestO
277278
}
278279
}
279280

280-
export function createCopyWithCurrentGroupOperationsOnly(template: RestOperationData, group: string): RestOperationData {
281+
validateGroupPrefix(groupPrefix, 'groupPrefix')
281282
// eslint-disable-next-line @typescript-eslint/no-unused-vars
282283
const { paths, servers: rootServers, ...rest } = template
283284
const groupWithoutEdgeSlashes = trimSlashes(group)
@@ -307,7 +308,7 @@ export function createCopyWithCurrentGroupOperationsOnly(template: RestOperation
307308
return [fullPath, pathItemWithoutServers] as const
308309
})
309310
.filter(([key]) => removeFirstSlash(key as string).startsWith(`${groupWithoutEdgeSlashes}/`)) // note that 'api/v10' is a substring of 'api/v1000'
310-
// remove prefix group for correct path mapping in apiDiff
311+
// remove group prefix for correct path mapping in apiDiff
311312
.map(([key, value]) => [removeFirstSlash(key as string).substring(groupWithoutEdgeSlashes.length), value]),
312313
),
313314
},

src/apitypes/rest/rest.utils.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,17 @@ export const extractRootSecurityDiffs = (doc: OpenAPIV3.Document): Diff[] => {
116116
...componentsSecuritySchemesDiffs,
117117
]
118118
}
119+
120+
export function validateGroupPrefix(group: unknown, paramName: string): void {
121+
if (group === undefined) {
122+
return
123+
}
124+
125+
if (typeof group !== 'string') {
126+
throw new Error(`${paramName} must be a string, received: ${typeof group}`)
127+
}
128+
129+
if (group.length < 3 || !group.startsWith('/') || !group.endsWith('/')) {
130+
throw new Error(`${paramName} must begin and end with a "/" character and contain at least one meaningful character, received: "${group}"`)
131+
}
132+
}

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
import { BuildConfig, BuilderStrategy, BuildResult, BuildTypeContexts } from '../types'
1818
import { compareVersions } from '../components/compare'
19+
import { validateGroupPrefix } from '../apitypes/rest/rest.utils'
1920

2021
export class PrefixGroupsChangelogStrategy implements BuilderStrategy {
2122
async execute(config: BuildConfig, buildResult: BuildResult, contexts: BuildTypeContexts): Promise<BuildResult> {
2223
const { packageId, version } = config
2324
const { compareContext } = contexts
2425

2526
// Validate groups
26-
this.validateGroup(config.currentGroup, 'currentGroup')
27-
this.validateGroup(config.previousGroup, 'previousGroup')
27+
validateGroupPrefix(config.currentGroup, 'currentGroup')
28+
validateGroupPrefix(config.previousGroup, 'previousGroup')
2829

2930
buildResult.comparisons = await compareVersions(
3031
[version, packageId],
@@ -34,18 +35,4 @@ export class PrefixGroupsChangelogStrategy implements BuilderStrategy {
3435

3536
return buildResult
3637
}
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-
}
5138
}

0 commit comments

Comments
 (0)