Skip to content

Commit

Permalink
feat(config): validate config parent (#9350)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Apr 5, 2021
1 parent 1473047 commit 88ae12a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lib/config/__snapshots__/validation.spec.ts.snap
Expand Up @@ -210,3 +210,12 @@ Array [
`;

exports[`config/validation validateConfig(config) validates valid alias objects 1`] = `Array []`;

exports[`config/validation validateConfig(config) warns if hostType has the wrong parent 1`] = `
Array [
Object {
"message": "hostType should only be configured within a \\"hostRules\\" object. Was found in .",
"topic": "hostType",
},
]
`;
31 changes: 29 additions & 2 deletions lib/config/validation.spec.ts
Expand Up @@ -2,6 +2,21 @@ import * as configValidation from './validation';
import { RenovateConfig } from '.';

describe('config/validation', () => {
describe('getParentName()', () => {
it('ignores encrypted in root', () => {
expect(configValidation.getParentName('encrypted')).toEqual('');
});
it('handles array types', () => {
expect(configValidation.getParentName('hostRules[1]')).toEqual(
'hostRules'
);
});
it('handles encrypted within array types', () => {
expect(configValidation.getParentName('hostRules[0].encrypted')).toEqual(
'hostRules'
);
});
});
describe('validateConfig(config)', () => {
it('returns deprecation warnings', async () => {
const config = {
Expand Down Expand Up @@ -152,7 +167,7 @@ describe('config/validation', () => {
const { warnings, errors } = await configValidation.validateConfig(
config
);
expect(warnings).toHaveLength(0);
expect(warnings).toHaveLength(1);
expect(errors).toMatchSnapshot();
expect(errors).toHaveLength(12);
});
Expand All @@ -175,7 +190,7 @@ describe('config/validation', () => {
const { warnings, errors } = await configValidation.validateConfig(
config
);
expect(warnings).toHaveLength(0);
expect(warnings).toHaveLength(2);
expect(errors).toMatchSnapshot();
expect(errors).toHaveLength(2);
});
Expand Down Expand Up @@ -461,6 +476,18 @@ describe('config/validation', () => {
expect(warnings).toMatchSnapshot();
});

it('warns if hostType has the wrong parent', async () => {
const config = {
hostType: 'npm',
};
const { warnings, errors } = await configValidation.validateConfig(
config
);
expect(errors).toHaveLength(0);
expect(warnings).toHaveLength(1);
expect(warnings).toMatchSnapshot();
});

it('validates preset values', async () => {
const config = {
extends: ['foo', 'bar', 42] as never,
Expand Down
31 changes: 31 additions & 0 deletions lib/config/validation.ts
Expand Up @@ -15,6 +15,7 @@ import * as managerValidator from './validation-helpers/managers';
const options = getOptions();

let optionTypes: Record<string, RenovateOptions['type']>;
let optionParents: Record<string, RenovateOptions['parent']>;

export interface ValidationResult {
errors: ValidationMessage[];
Expand All @@ -30,6 +31,16 @@ function isManagerPath(parentPath: string): boolean {
);
}

export function getParentName(parentPath: string): string {
return parentPath
? parentPath
.replace(/\.?encrypted$/, '')
.replace(/\[\d+\]$/, '')
.split('.')
.pop()
: '.';
}

export async function validateConfig(
config: RenovateConfig,
isPreset?: boolean,
Expand All @@ -41,6 +52,14 @@ export async function validateConfig(
optionTypes[option.name] = option.type;
});
}
if (!optionParents) {
optionParents = {};
options.forEach((option) => {
if (option.parent) {
optionParents[option.name] = option.parent;
}
});
}
let errors: ValidationMessage[] = [];
let warnings: ValidationMessage[] = [];

Expand Down Expand Up @@ -138,6 +157,18 @@ export async function validateConfig(
});
}
}
const parentName = getParentName(parentPath);
if (
!isPreset &&
optionParents[key] &&
optionParents[key] !== parentName
) {
const message = `${key} should only be configured within a "${optionParents[key]}" object. Was found in ${parentName}`;
warnings.push({
topic: `${parentPath ? `${parentPath}.` : ''}${key}`,
message,
});
}
if (!optionTypes[key]) {
errors.push({
topic: 'Configuration Error',
Expand Down

0 comments on commit 88ae12a

Please sign in to comment.