Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add validations for aliases #6426

Merged
merged 5 commits into from Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/config/__snapshots__/validation.spec.ts.snap
Expand Up @@ -88,6 +88,24 @@ Array [
]
`;

exports[`config/validation validateConfig(config) errors if aliases depth is more than 1 1`] = `
Array [
Object {
"depName": "Configuration Error",
"message": "Invalid alias object configuration",
},
]
`;

exports[`config/validation validateConfig(config) errors if aliases have invalid url 1`] = `
Array [
Object {
"depName": "Configuration Error",
"message": "Invalid alias object configuration",
},
]
`;

exports[`config/validation validateConfig(config) errors if regexManager fields are missing 1`] = `
Array [
Object {
Expand Down Expand Up @@ -150,3 +168,5 @@ Array [
},
]
`;

exports[`config/validation validateConfig(config) validates valid alias objects 1`] = `Array []`;
46 changes: 46 additions & 0 deletions lib/config/validation.spec.ts
Expand Up @@ -336,5 +336,51 @@ describe('config/validation', () => {
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(0);
});

it('validates valid alias objects', async () => {
const config = {
aliases: {
example1: 'http://www.example.com',
example2: 'https://www.example2.com/example',
},
};
const { warnings, errors } = await configValidation.validateConfig(
config
);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(0);
expect(errors).toMatchSnapshot();
});

it('errors if aliases depth is more than 1', async () => {
const config = {
aliases: {
sample: {
example1: 'http://www.example.com',
},
},
};
const { warnings, errors } = await configValidation.validateConfig(
config
);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(1);
expect(errors).toMatchSnapshot();
});

it('errors if aliases have invalid url', async () => {
const config = {
aliases: {
example1: 'noturl',
example2: 'http://www.example.com',
},
};
const { warnings, errors } = await configValidation.validateConfig(
config
);
expect(warnings).toHaveLength(0);
expect(errors).toHaveLength(1);
expect(errors).toMatchSnapshot();
});
});
});
43 changes: 32 additions & 11 deletions lib/config/validation.ts
@@ -1,4 +1,5 @@
import is from '@sindresorhus/is';
import isUrl from 'is-url';
adusumillipraveen marked this conversation as resolved.
Show resolved Hide resolved
import { regEx } from '../util/regex';
import * as template from '../util/template';
import { hasValidSchedule, hasValidTimezone } from '../workers/branch/schedule';
Expand Down Expand Up @@ -56,6 +57,17 @@ export async function validateConfig(
return ignoredNodes.includes(key);
}

function validateAliasObject(key: string, val: object): boolean {
if (key === 'aliases') {
for (const value of Object.values(val)) {
if (!isUrl(value)) {
return false;
}
}
}
return true;
}

for (const [key, val] of Object.entries(config)) {
const currentPath = parentPath ? `${parentPath}.${key}` : key;
if (
Expand Down Expand Up @@ -346,17 +358,26 @@ export async function validateConfig(
}
} else if (type === 'object' && currentPath !== 'compatibility') {
if (is.object(val)) {
const ignoredObjects = options
.filter((option) => option.freeChoice)
.map((option) => option.name);
if (!ignoredObjects.includes(key)) {
const subValidation = await module.exports.validateConfig(
val,
isPreset,
currentPath
);
warnings = warnings.concat(subValidation.warnings);
errors = errors.concat(subValidation.errors);
if (key === 'aliases') {
if (!validateAliasObject(key, val)) {
errors.push({
depName: 'Configuration Error',
message: `Invalid alias object configuration`,
});
}
} else {
const ignoredObjects = options
.filter((option) => option.freeChoice)
.map((option) => option.name);
if (!ignoredObjects.includes(key)) {
const subValidation = await module.exports.validateConfig(
val,
isPreset,
currentPath
);
warnings = warnings.concat(subValidation.warnings);
errors = errors.concat(subValidation.errors);
}
}
} else {
errors.push({
Expand Down
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -135,6 +135,7 @@
"hasha": "5.2.0",
"ignore": "5.1.8",
"ini": "1.3.5",
"is-url": "^1.2.4",
"js-yaml": "3.14.0",
"json-dup-key-validator": "1.0.2",
"json-stringify-pretty-compact": "2.0.0",
Expand Down Expand Up @@ -199,6 +200,7 @@
"@types/got": "9.6.11",
"@types/graceful-fs": "4.1.3",
"@types/ini": "1.3.30",
"@types/is-url": "^1.2.28",
"@types/jest": "25.2.3",
"@types/js-yaml": "3.12.4",
"@types/json5": "0.0.30",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Expand Up @@ -1542,6 +1542,11 @@
resolved "https://registry.yarnpkg.com/@types/ini/-/ini-1.3.30.tgz#d1485459c9fad84e937414b832a2adb649eab379"
integrity sha512-2+iF8zPSbpU83UKE+PNd4r/MhwNAdyGpk3H+VMgEH3EhjFZq1kouLgRoZrmIcmoGX97xFvqdS44DkICR5Nz3tQ==

"@types/is-url@^1.2.28":
version "1.2.28"
resolved "https://registry.yarnpkg.com/@types/is-url/-/is-url-1.2.28.tgz#914dabd50546d9b0142806e42c72bc7c2b7e0787"
integrity sha1-kU2r1QVG2bAUKAbkLHK8fCt+B4c=

"@types/istanbul-lib-coverage@*", "@types/istanbul-lib-coverage@^2.0.0", "@types/istanbul-lib-coverage@^2.0.1":
version "2.0.2"
resolved "https://registry.yarnpkg.com/@types/istanbul-lib-coverage/-/istanbul-lib-coverage-2.0.2.tgz#79d7a78bad4219f4c03d6557a1c72d9ca6ba62d5"
Expand Down Expand Up @@ -5210,6 +5215,11 @@ is-typedarray@^1.0.0, is-typedarray@~1.0.0:
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
integrity sha1-5HnICFjfDBsR3dppQPlgEfzaSpo=

is-url@^1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/is-url/-/is-url-1.2.4.tgz#04a4df46d28c4cff3d73d01ff06abeb318a1aa52"
integrity sha512-ITvGim8FhRiYe4IQ5uHSkj7pVaPDrCTkNd3yq3cV7iZAcJdHTUMPMEHcqSOy9xZ9qFenQCvi+2wjH9a1nXqHww==

is-whitespace-character@^1.0.0:
version "1.0.4"
resolved "https://registry.yarnpkg.com/is-whitespace-character/-/is-whitespace-character-1.0.4.tgz#0858edd94a95594c7c9dd0b5c174ec6e45ee4aa7"
Expand Down