feat(core): validate that outputs is an array of strings (#22371)
This commit is contained in:
parent
063a5d464f
commit
8631b40d54
@ -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]"
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user