@openshift-console/dynamic-plugin-sdk-webpack
Version:
Provides webpack ConsoleRemotePlugin used to build all dynamic plugin assets.
65 lines (64 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ValidationResult = void 0;
const chalk_1 = require("chalk");
const semver = require("semver");
class ValidationAssertions {
constructor(result) {
this.result = result;
}
// https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names
validDNSSubdomainName(obj, objPath) {
if (typeof obj === 'string') {
this.result.assertThat(obj.length <= 253, `${objPath} must contain no more than 253 characters`);
this.result.assertThat(/^[a-z0-9-.]*$/.test(obj), `${objPath} must contain only lowercase alphanumeric characters, '-' or '.'`);
this.result.assertThat(/^[a-z0-9]+/.test(obj) && /[a-z0-9]+$/.test(obj), `${objPath} must start and end with an alphanumeric character`);
}
}
validSemverString(obj, objPath) {
if (typeof obj === 'string') {
this.result.assertThat(!!semver.valid(obj), `${objPath} must be semver compliant`);
}
}
validSemverRangeString(obj, objPath) {
if (typeof obj === 'string') {
this.result.assertThat(!!semver.validRange(obj), `${objPath} semver range is not valid`);
}
}
}
class ValidationResult {
constructor(description) {
this.description = description;
this.errors = [];
this.assertions = new ValidationAssertions(this);
}
assertThat(condition, message) {
if (!condition) {
this.addError(message);
}
}
addError(message) {
this.errors.push(message);
}
hasErrors() {
return this.errors.length > 0;
}
getErrors() {
return [...this.errors];
}
formatErrors() {
const prefix = `${chalk_1.default.bold(this.description)} (${this.errors.length} errors)\n`;
const errorLines = this.errors.map((e) => ` ${chalk_1.default.red(e)}`);
return prefix + errorLines.join('\n');
}
report(throwOnErrors = true) {
if (this.hasErrors()) {
// eslint-disable-next-line no-console
console.error(this.formatErrors());
if (throwOnErrors) {
throw new Error('Validation failed');
}
}
}
}
exports.ValidationResult = ValidationResult;