Made validation messages more descriptive

This commit is contained in:
Eric Nilsson
2020-10-22 12:18:25 +02:00
parent 5cdb613277
commit dab96d0c68
3 changed files with 34 additions and 7 deletions
@@ -50,7 +50,37 @@ export class FieldFormatEntityPolicy implements EntityPolicy {
}
if (!isValid) {
throw new Error(`${field} "${value}" is not valid`);
let expectation;
switch (validator.name) {
case 'isValidLabelValue':
expectation =
'a string that is sequences of [a-zA-Z0-9] separated by any of [-_.], at most 63 characters in total';
break;
case 'isValidLabelKey':
case 'isValidApiVersion':
case 'isValidAnnotationKey':
expectation = 'a valid prefix and/or suffix';
break;
case 'isValidAnnotationValue':
expectation = 'a string';
break;
case 'isValidNamespace':
expectation =
'a string that is sequences of [a-zA-Z0-9] seperated by [-], at most 63 characters in total';
break;
default:
expectation = undefined;
break;
}
// ensure that if there are other/future validators, the error message defaults to a general "is not valid, visit link"
const message = expectation
? ` expected ${expectation} but found "${value}".`
: '';
throw new Error(
`"${field}" is not valid;${message} To learn more about catalog file format, visit: https://github.com/spotify/backstage/blob/master/docs/architecture-decisions/adr002-default-catalog-file-format.md`,
);
}
}