Skip to content

Commit

Permalink
feat(core): validate that outputs is an array of strings (#22371)
Browse files Browse the repository at this point in the history
  • Loading branch information
MJez29 authored Apr 30, 2024
1 parent 063a5d4 commit 8631b40
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/nx/src/tasks-runner/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
);
});
});
});
25 changes: 25 additions & 0 deletions packages/nx/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8631b40

Please sign in to comment.