Loosen catalog constraints on entity tag string validity.

Adding `+` and `#` as valid chars, to e.g. allow "c++" and "c#".

Signed-off-by: Gustaf Räntilä <g.rantila@gmail.com>
This commit is contained in:
Gustaf Räntilä
2021-09-22 17:40:56 +02:00
parent 7913983364
commit d42566c5c9
6 changed files with 50 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/catalog-model': patch
---
Loosen constraints on what's a valid catalog entity tag name (include + and #)
+1
View File
@@ -50,6 +50,7 @@ export class CommonValidatorFunctions {
isValidSuffix: (value: string) => boolean,
): boolean;
static isValidString(value: unknown): boolean;
static isValidTag(value: unknown): boolean;
static isValidUrl(value: unknown): boolean;
}
@@ -78,6 +78,10 @@ export class FieldFormatEntityPolicy implements EntityPolicy {
expectation =
'a string that is sequences of [a-z0-9] separated by [-], at most 63 characters in total';
break;
case 'isValidTag':
expectation =
'a string that is sequences of [a-z0-9+#] separated by [-], at most 63 characters in total';
break;
case 'isValidAnnotationValue':
expectation = 'a string';
break;
@@ -162,6 +162,31 @@ describe('CommonValidatorFunctions', () => {
expect(CommonValidatorFunctions.isValidDnsLabel(value)).toBe(result);
});
it.each([
// These are identical to isValidDnsLabel
[null, false],
[7, false],
['', false],
['a', true],
['a-b', true],
['-a-b', false],
['a-b-', false],
['a--b', false],
['a_b', false],
[`${'a'.repeat(63)}`, true],
[`${'a'.repeat(64)}`, false],
// Tags can have other characters though
['a+b', true],
['+a+b', true],
['a+b+', true],
['a++b', true],
['c++', true],
['c#', true],
['#c++', true],
])(`isValidTag %p ? %p`, (value, result) => {
expect(CommonValidatorFunctions.isValidTag(value)).toBe(result);
});
it.each([
[null, false],
[7, false],
@@ -95,6 +95,20 @@ export class CommonValidatorFunctions {
);
}
/**
* Checks that the value is a valid tag.
*
* @param value - The value to check
*/
static isValidTag(value: unknown): boolean {
return (
typeof value === 'string' &&
value.length >= 1 &&
value.length <= 63 &&
/^[a-z0-9+#]+(\-[a-z0-9+#]+)*$/.test(value)
);
}
/**
* Checks that the value is a valid URL.
*
@@ -27,7 +27,7 @@ const defaultValidators: Validators = {
isValidLabelValue: KubernetesValidatorFunctions.isValidLabelValue,
isValidAnnotationKey: KubernetesValidatorFunctions.isValidAnnotationKey,
isValidAnnotationValue: KubernetesValidatorFunctions.isValidAnnotationValue,
isValidTag: CommonValidatorFunctions.isValidDnsLabel,
isValidTag: CommonValidatorFunctions.isValidTag,
};
/** @public */