config-loader,cli: better presentation of config schema validation errors

This commit is contained in:
Patrik Oldsberg
2020-11-15 13:07:13 +01:00
parent eb9e293fce
commit d02d79e392
3 changed files with 29 additions and 14 deletions
@@ -104,10 +104,14 @@ export function compileConfigSchemas(
const valid = validate(config);
if (!valid) {
// TODO(Rugvip): better messages here, with more context such as which file the error occurred in
const errors = ajv.errorsText(validate.errors);
const errors = validate.errors ?? [];
return {
errors: [errors],
errors: errors.map(({ dataPath, message, params }) => {
const paramStr = Object.entries(params)
.map(([name, value]) => `${name}=${value}`)
.join(' ');
return `Config ${message || ''} { ${paramStr} } at ${dataPath}`;
}),
visibilityByPath: new Map(),
};
}
@@ -61,9 +61,11 @@ export async function loadConfigSchema(
): AppConfig[] {
const result = validate(configs);
if (result.errors) {
throw new Error(
const error = new Error(
`Config validation failed, ${result.errors.join('; ')}`,
);
(error as any).messages = result.errors;
throw error;
}
let processedConfigs = configs;