Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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