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
+19 -10
View File
@@ -40,15 +40,24 @@ export async function loadCliConfig(configArgs: string[]) {
`Loaded config from ${appConfigs.map(c => c.context).join(', ')}`,
);
const frontendAppConfigs = schema.process(appConfigs, {
visiblity: ['frontend'],
});
const frontendConfig = ConfigReader.fromConfigs(frontendAppConfigs);
try {
const frontendAppConfigs = schema.process(appConfigs, {
visiblity: ['frontend'],
});
const frontendConfig = ConfigReader.fromConfigs(frontendAppConfigs);
return {
schema,
appConfigs,
frontendConfig,
frontendAppConfigs,
};
return {
schema,
appConfigs,
frontendConfig,
frontendAppConfigs,
};
} catch (error) {
const maybeSchemaError = error as Error & { messages?: string[] };
if (maybeSchemaError.messages) {
const messages = maybeSchemaError.messages.join('\n ');
throw new Error(`Configuration does not match schema\n\n ${messages}`);
}
throw error;
}
}
@@ -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;