Skip to content

Commit

Permalink
feat(Telemetry): Report configuration validation result
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Jul 1, 2021
1 parent 8e2d48f commit 01f1586
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions lib/classes/ConfigSchemaHandler/index.js
Expand Up @@ -2,6 +2,7 @@

const Ajv = require('ajv');
const _ = require('lodash');
const ensurePlainObject = require('type/plain-object/ensure');
const schema = require('../../configSchema');
const ServerlessError = require('../../serverless-error');
const normalizeAjvErrors = require('./normalizeAjvErrors');
Expand Down Expand Up @@ -56,6 +57,8 @@ const denormalizeUserConfig = (userConfig, { removedValuesMap }) => {
}
};

const configurationValidationResults = new WeakMap();

class ConfigSchemaHandler {
constructor(serverless) {
this.serverless = serverless;
Expand All @@ -67,8 +70,14 @@ class ConfigSchemaHandler {
deepFreeze(this.schema.properties.package);
}

static getConfigurationValidationResult(configuration) {
if (!configurationValidationResults.has(ensurePlainObject(configuration))) return null;
return configurationValidationResults.get(ensurePlainObject(configuration));
}

validateConfig(userConfig) {
if (!this.schema.properties.provider.properties.name) {
configurationValidationResults.set(this.serverless.configurationInput, false);
if (this.serverless.service.configValidationMode !== 'off') {
this.serverless.cli.log(
`${WARNING_PREFIX}: Unrecognized provider '${this.serverless.service.provider.name}'`,
Expand Down Expand Up @@ -108,6 +117,9 @@ class ConfigSchemaHandler {
const denormalizeOptions = normalizeUserConfig(userConfig);
validate(userConfig);
denormalizeUserConfig(userConfig, denormalizeOptions);
if (!configurationValidationResults.has(this.serverless.configurationInput)) {
configurationValidationResults.set(this.serverless.configurationInput, !validate.errors);
}
if (validate.errors && this.serverless.service.configValidationMode !== 'off') {
const messages = normalizeAjvErrors(validate.errors).map((err) => err.message);
this.handleErrorMessages(messages);
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/telemetry/generatePayload.js
Expand Up @@ -9,6 +9,7 @@ const isPlainObject = require('type/plain-object/is');
const isObject = require('type/object/is');
const userConfig = require('@serverless/utils/config');
const isStandalone = require('../isStandaloneExecutable');
const { getConfigurationValidationResult } = require('../../classes/ConfigSchemaHandler');
const { triggeredDeprecations } = require('../logDeprecation');
const isNpmGlobal = require('../npmPackage/isGlobal');
const resolveLocalServerlessPath = require('../../cli/resolve-local-serverless-path');
Expand Down Expand Up @@ -279,6 +280,7 @@ module.exports = async ({
payload.hasLocalCredentials = isAwsProvider && Boolean(new AWS.Config().credentials);
payload.npmDependencies = npmDependencies;
payload.config = getServiceConfig({ configuration, options });
payload.isConfigValid = getConfigurationValidationResult(configuration);
payload.dashboard.orgUid = serverless && serverless.service.orgUid;
}

Expand Down
8 changes: 6 additions & 2 deletions test/unit/lib/classes/ConfigSchemaHandler/index.test.js
Expand Up @@ -2,6 +2,9 @@

const chai = require('chai');
const runServerless = require('../../../../utils/run-serverless');
const {
getConfigurationValidationResult,
} = require('../../../../../lib/classes/ConfigSchemaHandler');

chai.use(require('chai-as-promised'));

Expand Down Expand Up @@ -69,11 +72,12 @@ describe('test/unit/lib/classes/ConfigSchemaHandler/index.test.js', () => {
});

describe('#validateConfig', () => {
it('should run without errors for valid config', () => {
return runServerless({
it('should run without errors for valid config', async () => {
const { serverless } = await runServerless({
fixture: 'configSchemaExtensions',
command: 'info',
});
expect(getConfigurationValidationResult(serverless.configurationInput)).to.be.true;
});
});

Expand Down
4 changes: 4 additions & 0 deletions test/unit/lib/utils/telemetry/generatePayload.test.js
Expand Up @@ -119,6 +119,7 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => {
cliName: 'serverless',
command: 'print',
commandOptionNames: [],
isConfigValid: true,
config: {
configValidationMode: 'error',
provider: {
Expand Down Expand Up @@ -181,6 +182,7 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => {
cliName: 'serverless',
command: 'print',
commandOptionNames: [],
isConfigValid: false, // No schema for custom provider
config: {
configValidationMode: 'warn',
provider: {
Expand Down Expand Up @@ -244,6 +246,7 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => {
cliName: 'serverless',
command: 'print',
commandOptionNames: [],
isConfigValid: null,
config: {
configValidationMode: 'error',
provider: {
Expand Down Expand Up @@ -332,6 +335,7 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => {
command: '',
commandOptionNames: [],
cliName: 'serverless',
isConfigValid: null,
config: {
configValidationMode: 'warn',
provider: {
Expand Down

0 comments on commit 01f1586

Please sign in to comment.