Skip to content

Commit 5c19804

Browse files
committed
feat(supported-version): validate custom_versions
Previously, @danslo reported that he tried to use `custom_versions` without setting the kind. This isn't the correct behavior. I've added a validator to alert him of this.
1 parent 81a1eb2 commit 5c19804

10 files changed

+69
-42
lines changed

Diff for: supported-version/dist/index.js

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: supported-version/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import * as core from '@actions/core';
2-
import { validateOrError } from './kind/compute-kind';
2+
import { validateKind } from './kind/validate-kinds';
33
import { getMatrixForKind } from './matrix/get-matrix-for-kind';
44

55

66
export async function run(): Promise<void> {
77
try {
88
const kind = core.getInput("kind");
9-
validateOrError(kind);
10-
119
const customVersions = core.getInput("custom_versions");
1210

11+
validateKind(<any>kind, customVersions.split(','));
12+
1313
core.setOutput('matrix', getMatrixForKind(kind, customVersions));
1414
}
1515
catch (error) {

Diff for: supported-version/src/kind/compute-kind.spec.ts

-11
This file was deleted.

Diff for: supported-version/src/kind/compute-kind.ts

-23
This file was deleted.

Diff for: supported-version/src/kind/kinds.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Acceptable arguments for version `kind`
3+
*/
4+
export const KNOWN_KINDS = {
5+
'currently-supported': true,
6+
'latest': true,
7+
'custom': true,
8+
'nightly': true,
9+
'all': true,
10+
}
11+
12+
export type Kind = keyof typeof KNOWN_KINDS;

Diff for: supported-version/src/kind/validate-kinds.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { validateKind } from "./validate-kinds";
2+
3+
describe('validateKind', () => {
4+
it('returns `true` if its a valid kind', () => {
5+
expect(validateKind("latest")).toBe(true);
6+
});
7+
8+
it('throws a helpful exception if its an invalid kind', () => {
9+
expect(() => validateKind(<any>"taco")).toThrowError();
10+
})
11+
12+
it('throws a helpful exception if custom versions are provided with the wrong kind', () => {
13+
expect(() => validateKind(<any>"latest", [])).toThrowError();
14+
})
15+
})

Diff for: supported-version/src/kind/validate-kinds.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { customVersionsValidator } from "./validations/custom-versions-validator";
2+
import { isKnownKind } from "./validations/is-known-kind";
3+
import { KindValidator } from "./validator";
4+
5+
export const validateKind: KindValidator = (kind, custom_versions = null): boolean => {
6+
return validators.reduce((acc, el) => el(kind, custom_versions), true);
7+
}
8+
9+
export const validators: KindValidator[] = [
10+
isKnownKind,
11+
customVersionsValidator,
12+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { KindValidator } from "../validator";
2+
3+
export const customVersionsValidator: KindValidator = (kind, customVersions) => {
4+
if(customVersions && kind !== 'custom') {
5+
throw new Error('`custom_versions` can only be used with kind `custom`');
6+
}
7+
return true;
8+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { KNOWN_KINDS, Kind } from "../kinds";
2+
3+
export const isKnownKind = (kind: Kind): boolean => {
4+
if(!(kind in KNOWN_KINDS)) {
5+
throw new Error(
6+
`Invalid kind provided, supported kinds are: ${Object.keys(KNOWN_KINDS).join(', ')}`
7+
);
8+
}
9+
10+
return true;
11+
};

Diff for: supported-version/src/kind/validator.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Kind } from "./kinds";
2+
3+
export type KindValidator = (kind: Kind, custom_versions?: string[]) => boolean;

0 commit comments

Comments
 (0)