-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloud-functions-validator.ts
More file actions
executable file
·81 lines (63 loc) · 2.75 KB
/
cloud-functions-validator.ts
File metadata and controls
executable file
·81 lines (63 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
ExistingDynamicRouteAtSameLevelError,
IndistinctDynamicRouteNamesInPathError,
InvalidFilepathNamingError,
TopLevelDynamicRouteError,
} from './errors/cloud-function.errors';
import { CloudFunctionResource, CloudFunctionValidationError } from './types';
export class CloudFunctionsValidator {
private cloudFunctionResources: CloudFunctionResource[];
private dynamicRoutes: Record<string, string>;
constructor(cloudFunctionResources: CloudFunctionResource[]) {
this.cloudFunctionResources = cloudFunctionResources;
this.dynamicRoutes = {};
}
validate(): CloudFunctionValidationError | undefined {
for (const cloudFunctionResource of this.cloudFunctionResources) {
const filepath = cloudFunctionResource.apiResourceURI;
if (this.hasTopLevelDynamicRoute(filepath)) {
return new TopLevelDynamicRouteError(filepath);
}
if (this.hasInvalidFilepathNaming(filepath)) {
return new InvalidFilepathNamingError(filepath);
}
if (this.hasIndistinctDynamicRouteNamesInPath(filepath)) {
return new IndistinctDynamicRouteNamesInPathError(filepath);
}
const sameLevelDynamicRoute = this.getDynamicRouteAtSameLevel(filepath);
if (sameLevelDynamicRoute) {
return new ExistingDynamicRouteAtSameLevelError(filepath, sameLevelDynamicRoute);
}
}
return undefined;
}
private hasTopLevelDynamicRoute(filepath: string): boolean {
const matchTopLevelDynamicRoute = /^\/\[(.*?)\].*$/;
const matchResult = filepath.match(matchTopLevelDynamicRoute);
return matchResult !== null;
}
private hasInvalidFilepathNaming(filepath: string): boolean {
const validFilePathRegex = /^(\[[\w-]+\]|[\w-]+|\/)+$/;
const matchResult = filepath.match(validFilePathRegex);
return matchResult === null;
}
private hasIndistinctDynamicRouteNamesInPath(filepath: string): boolean {
const matchDistinctDynamicRoutesInPath = /\[(.*?)\]/g;
const dynamicRouteNames = filepath.match(matchDistinctDynamicRoutesInPath) as string[];
if (dynamicRouteNames === null) {
return false;
}
const distinctDynamicRouteNames = Array.from(new Set(dynamicRouteNames));
return distinctDynamicRouteNames.length !== dynamicRouteNames.length;
}
private getDynamicRouteAtSameLevel(filepath: string): string | undefined {
const matchDyanmicRouteRegex = /\[(.*?)\]/g;
const dynamicRouteNameReplacer = '[id]';
const transformedFilePathWithDynamicRoute = filepath.replace(matchDyanmicRouteRegex, dynamicRouteNameReplacer);
if (this.dynamicRoutes[transformedFilePathWithDynamicRoute]) {
return this.dynamicRoutes[transformedFilePathWithDynamicRoute];
}
this.dynamicRoutes[transformedFilePathWithDynamicRoute] = filepath;
return undefined;
}
}