Skip to content

Commit 09aea9f

Browse files
committed
refactor: Improve validation logic and test clarity
This commit includes several improvements to the function configuration validation: - The error message for duplicate `source`/`prefix` pairs is now more descriptive and suggests a solution. - The test suite is more robust, with more specific error message checks and improved typing. - Linter warnings have been addressed.
1 parent 57f6250 commit 09aea9f

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

src/functions/projectConfig.spec.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,37 +43,39 @@ describe("projectConfig", () => {
4343
});
4444

4545
it("passes validation for multi-instance config with same source", () => {
46-
const config = [
46+
const config: projectConfig.NormalizedConfig = [
4747
{ source: "foo", codebase: "bar" },
4848
{ source: "foo", codebase: "baz", prefix: "prefix-two" },
4949
];
50-
expect(projectConfig.validate(config as projectConfig.NormalizedConfig)).to.deep.equal(
51-
config,
52-
);
50+
expect(projectConfig.validate(config)).to.deep.equal(config);
5351
});
5452

5553
it("passes validation for multi-instance config with one missing codebase", () => {
56-
const config = [{ source: "foo", codebase: "bar", prefix: "bar-prefix" }, { source: "foo" }];
54+
const config: projectConfig.NormalizedConfig = [
55+
{ source: "foo", codebase: "bar", prefix: "bar-prefix" },
56+
{ source: "foo" },
57+
];
5758
const expected = [
5859
{ source: "foo", codebase: "bar", prefix: "bar-prefix" },
5960
{ source: "foo", codebase: "default" },
6061
];
61-
expect(projectConfig.validate(config as projectConfig.NormalizedConfig)).to.deep.equal(
62-
expected,
63-
);
62+
expect(projectConfig.validate(config)).to.deep.equal(expected);
6463
});
6564

6665
it("fails validation for multi-instance config with missing codebase and a default codebase", () => {
67-
const config = [{ source: "foo", codebase: "default" }, { source: "foo" }];
68-
expect(() => projectConfig.validate(config as projectConfig.NormalizedConfig)).to.throw(
66+
const config: projectConfig.NormalizedConfig = [
67+
{ source: "foo", codebase: "default" },
68+
{ source: "foo" },
69+
];
70+
expect(() => projectConfig.validate(config)).to.throw(
6971
FirebaseError,
7072
/functions.codebase must be unique but 'default' was used more than once./,
7173
);
7274
});
7375

7476
it("fails validation for multi-instance config with multiple missing codebases", () => {
75-
const config = [{ source: "foo" }, { source: "foo" }];
76-
expect(() => projectConfig.validate(config as projectConfig.NormalizedConfig)).to.throw(
77+
const config: projectConfig.NormalizedConfig = [{ source: "foo" }, { source: "foo" }];
78+
expect(() => projectConfig.validate(config)).to.throw(
7779
FirebaseError,
7880
/functions.codebase must be unique but 'default' was used more than once./,
7981
);
@@ -119,22 +121,31 @@ describe("projectConfig", () => {
119121
});
120122

121123
it("fails validation given a duplicate source/prefix pair", () => {
122-
const config = [
124+
const config: projectConfig.NormalizedConfig = [
123125
{ source: "foo", codebase: "bar", prefix: "a" },
124126
{ source: "foo", codebase: "baz", prefix: "a" },
125127
];
126-
expect(() => projectConfig.validate(config as projectConfig.NormalizedConfig)).to.throw(
128+
expect(() => projectConfig.validate(config)).to.throw(
127129
FirebaseError,
128-
/More than one functions config specifies the same source directory/,
130+
/More than one functions config specifies the same source directory \('foo'\) and prefix \('a'\)/,
131+
);
132+
});
133+
134+
it("fails validation for multi-instance config with same source and no prefixes", () => {
135+
const config: projectConfig.NormalizedConfig = [
136+
{ source: "foo", codebase: "bar" },
137+
{ source: "foo", codebase: "baz" },
138+
];
139+
expect(() => projectConfig.validate(config)).to.throw(
140+
FirebaseError,
141+
/More than one functions config specifies the same source directory \('foo'\) and prefix \(''\)/,
129142
);
130143
});
131144

132145
it("should allow a single function in an array to have a default codebase", () => {
133-
const config = [{ source: "foo" }];
146+
const config: projectConfig.NormalizedConfig = [{ source: "foo" }];
134147
const expected = [{ source: "foo", codebase: "default" }];
135-
expect(projectConfig.validate(config as projectConfig.NormalizedConfig)).to.deep.equal(
136-
expected,
137-
);
148+
expect(projectConfig.validate(config)).to.deep.equal(expected);
138149
});
139150
});
140151

src/functions/projectConfig.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FunctionsConfig, FunctionConfig } from "../firebaseConfig";
22
import { FirebaseError } from "../error";
33

44
export type NormalizedConfig = [FunctionConfig, ...FunctionConfig[]];
5-
export type ValidatedSingle = FunctionConfig & { source:string; codebase: string };
5+
export type ValidatedSingle = FunctionConfig & { source: string; codebase: string };
66
export type ValidatedConfig = [ValidatedSingle, ...ValidatedSingle[]];
77

88
export const DEFAULT_CODEBASE = "default";
@@ -92,10 +92,14 @@ export function assertUnique(
9292
function assertUniqueSourcePrefixPair(config: ValidatedConfig): void {
9393
const sourcePrefixPairs = new Set<string>();
9494
for (const c of config) {
95-
const key = `${c.source || ""}-${c.prefix || ""}`;
95+
const key = `${c.source}-${c.prefix || ""}`;
9696
if (sourcePrefixPairs.has(key)) {
9797
throw new FirebaseError(
98-
`More than one functions config specifies the same source directory ('${c.source}') and function name prefix ('${c.prefix || ""}').`,
98+
`More than one functions config specifies the same source directory ('${
99+
c.source
100+
}') and prefix ('${
101+
c.prefix ?? ""
102+
}'). Please add a unique 'prefix' to each function configuration that shares this source to resolve the conflict.`,
99103
);
100104
}
101105
sourcePrefixPairs.add(key);
@@ -130,4 +134,4 @@ export function configForCodebase(config: ValidatedConfig, codebase: string): Va
130134
throw new FirebaseError(`No functions config found for codebase ${codebase}`);
131135
}
132136
return codebaseCfg;
133-
}
137+
}

0 commit comments

Comments
 (0)