feat(core): validate that outputs is an array of strings (#22371)

This commit is contained in:
Michal Jez 2024-04-30 13:53:19 -04:00 committed by GitHub
parent 063a5d464f
commit 8631b40d54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -489,4 +489,24 @@ describe('utils', () => {
});
});
});
describe('validateOutputs', () => {
it('returns undefined if there are no errors', () => {
expect(validateOutputs(['{projectRoot}/dist'])).toBeUndefined();
});
it('throws an error if the output is not an array', () => {
expect(() => validateOutputs('output' as unknown as string[])).toThrow(
"The 'outputs' field must be an array"
);
});
it("throws an error if the output has entries that aren't strings", () => {
expect(() =>
validateOutputs(['foo', 1, null, true, {}, []] as unknown as string[])
).toThrow(
"The 'outputs' field must contain only strings, but received types: [string, number, object, boolean, object, object]"
);
});
});
});

View File

@ -107,7 +107,32 @@ class InvalidOutputsError extends Error {
}
}
function assertOutputsAreValidType(outputs: unknown) {
if (!Array.isArray(outputs)) {
throw new Error("The 'outputs' field must be an array");
}
const typesArray = [];
let hasInvalidType = false;
for (const output of outputs) {
if (typeof output !== 'string') {
hasInvalidType = true;
}
typesArray.push(typeof output);
}
if (hasInvalidType) {
throw new Error(
`The 'outputs' field must contain only strings, but received types: [${typesArray.join(
', '
)}]`
);
}
}
export function validateOutputs(outputs: string[]) {
assertOutputsAreValidType(outputs);
const invalidOutputs = new Set<string>();
for (const output of outputs) {