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.
This commit is contained in:
Damien Retzinger
2023-04-16 15:13:48 -04:00
parent 81a1eb2273
commit 5c198049f7
10 changed files with 69 additions and 42 deletions
+5 -5
View File
File diff suppressed because one or more lines are too long
+3 -3
View File
@@ -1,15 +1,15 @@
import * as core from '@actions/core';
import { validateOrError } from './kind/compute-kind';
import { validateKind } from './kind/validate-kinds';
import { getMatrixForKind } from './matrix/get-matrix-for-kind';
export async function run(): Promise<void> {
try {
const kind = core.getInput("kind");
validateOrError(kind);
const customVersions = core.getInput("custom_versions");
validateKind(<any>kind, customVersions.split(','));
core.setOutput('matrix', getMatrixForKind(kind, customVersions));
}
catch (error) {
@@ -1,11 +0,0 @@
import { isValidKind } from "./compute-kind";
describe('isValidKind', () => {
it('returns `true` if its a valid kind', () => {
expect(isValidKind("latest")).toBe(true);
});
it('returns `false` if it is not a valid kind', () => {
expect(isValidKind("taco")).toBe(false);
})
})
@@ -1,23 +0,0 @@
/**
* Acceptable arguments for version `kind`
*/
export const KNOWN_KINDS = {
'currently-supported': true,
'latest': true,
'custom': true,
'nightly': true,
'all': true,
}
export const isValidKind = (kind: string): boolean => {
return kind in KNOWN_KINDS;
};
export const validateOrError = (kind: string): true => {
if(isValidKind(kind)){
return true;
}
else {
throw new Error(`Invalid kind provided, supported kinds are: ${Object.keys(KNOWN_KINDS).join(', ')}`);
}
}
+12
View File
@@ -0,0 +1,12 @@
/**
* Acceptable arguments for version `kind`
*/
export const KNOWN_KINDS = {
'currently-supported': true,
'latest': true,
'custom': true,
'nightly': true,
'all': true,
}
export type Kind = keyof typeof KNOWN_KINDS;
@@ -0,0 +1,15 @@
import { validateKind } from "./validate-kinds";
describe('validateKind', () => {
it('returns `true` if its a valid kind', () => {
expect(validateKind("latest")).toBe(true);
});
it('throws a helpful exception if its an invalid kind', () => {
expect(() => validateKind(<any>"taco")).toThrowError();
})
it('throws a helpful exception if custom versions are provided with the wrong kind', () => {
expect(() => validateKind(<any>"latest", [])).toThrowError();
})
})
@@ -0,0 +1,12 @@
import { customVersionsValidator } from "./validations/custom-versions-validator";
import { isKnownKind } from "./validations/is-known-kind";
import { KindValidator } from "./validator";
export const validateKind: KindValidator = (kind, custom_versions = null): boolean => {
return validators.reduce((acc, el) => el(kind, custom_versions), true);
}
export const validators: KindValidator[] = [
isKnownKind,
customVersionsValidator,
];
@@ -0,0 +1,8 @@
import { KindValidator } from "../validator";
export const customVersionsValidator: KindValidator = (kind, customVersions) => {
if(customVersions && kind !== 'custom') {
throw new Error('`custom_versions` can only be used with kind `custom`');
}
return true;
}
@@ -0,0 +1,11 @@
import { KNOWN_KINDS, Kind } from "../kinds";
export const isKnownKind = (kind: Kind): boolean => {
if(!(kind in KNOWN_KINDS)) {
throw new Error(
`Invalid kind provided, supported kinds are: ${Object.keys(KNOWN_KINDS).join(', ')}`
);
}
return true;
};
+3
View File
@@ -0,0 +1,3 @@
import { Kind } from "./kinds";
export type KindValidator = (kind: Kind, custom_versions?: string[]) => boolean;