Skip to content

Commit

Permalink
fix: Recognize accessible configuration parts on validation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 22, 2021
1 parent bf62b7c commit b7a6349
Showing 1 changed file with 67 additions and 59 deletions.
126 changes: 67 additions & 59 deletions lib/classes/Service.js
Expand Up @@ -57,61 +57,6 @@ class Service {
this.serviceFilename = this.serverless.configurationFilename;

const configurationInput = this.serverless.configurationInput;
// basic service level validation
const version = this.serverless.utils.getVersion();
let ymlVersion = configurationInput.frameworkVersion;
if (ymlVersion && !semver.validRange(ymlVersion)) {
if (configurationInput.configValidationMode === 'error') {
throw new ServerlessError(
'Configured "frameworkVersion" does not represent a valid semver version range.',
'INVALID_FRAMEWORK_VERSION'
);
}
logWarning(
'Configured "frameworkVersion" does not represent a valid semver version range, version validation is skipped'
);
log.warning(
'Configured "frameworkVersion" does not represent a valid semver version range, version validation is skipped'
);
ymlVersion = null;
}
if (!this.isLocallyInstalled && !ymlVersion) {
log.info(
'To ensure safe major version upgrades ensure "frameworkVersion" setting in ' +
'service configuration ' +
`(recommended setup: "frameworkVersion: ^${currentVersion}")\n`
);
if (process.env.SLS_DEBUG) {
legacy.log(
'To ensure safe major version upgrades ensure "frameworkVersion" setting in ' +
'service configuration ' +
`(recommended setup: "frameworkVersion: ^${currentVersion}")\n`
);
}
}
if (
ymlVersion &&
version !== ymlVersion &&
!semver.satisfies(semver.coerce(version).raw, ymlVersion)
) {
const errorMessage = [
`The Serverless version (${version}) does not satisfy the`,
` "frameworkVersion" (${ymlVersion}) in ${this.serverless.configurationFilename}`,
].join('');
throw new ServerlessError(errorMessage, 'FRAMEWORK_VERSION_MISMATCH');
}
if (!configurationInput.service) {
throw new ServerlessError(
`"service" property is missing in ${this.serverless.configurationFilename}`,
'SERVICE_NAME_MISSING'
);
}
if (!configurationInput.provider) {
throw new ServerlessError(
`"provider" property is missing in ${this.serverless.configurationFilename}`,
'PROVIDER_NAME_MISSING'
);
}

this.initialServerlessConfig = configurationInput;

Expand All @@ -134,14 +79,15 @@ class Service {
// `provider.stage` are read by dashboard plugin)
if (!_.isObject(configurationInput.provider)) {
const providerName = configurationInput.provider;
configurationInput.provider = {
this.provider = {
name: providerName,
};
} else {
this.provider = configurationInput.provider;
}
if (configurationInput.provider.stage == null) {
configurationInput.provider.stage = 'dev';
if (this.provider.stage == null) {
this.provider.stage = 'dev';
}
this.provider = configurationInput.provider;

// `service` (read by dashboard plugin)
if (_.isObject(configurationInput.service)) {
Expand Down Expand Up @@ -181,6 +127,68 @@ class Service {
}
this.outputs = configurationInput.outputs;

// basic service level validation
const version = this.serverless.utils.getVersion();
let ymlVersion = configurationInput.frameworkVersion;
if (ymlVersion && !semver.validRange(ymlVersion)) {
if (configurationInput.configValidationMode === 'error') {
throw new ServerlessError(
'Configured "frameworkVersion" does not represent a valid semver version range.',
'INVALID_FRAMEWORK_VERSION'
);
}
logWarning(
'Configured "frameworkVersion" does not represent a valid semver version range, version validation is skipped'
);
log.warning(
'Configured "frameworkVersion" does not represent a valid semver version range, version validation is skipped'
);
ymlVersion = null;
}
if (!this.isLocallyInstalled && !ymlVersion) {
log.info(
'To ensure safe major version upgrades ensure "frameworkVersion" setting in ' +
'service configuration ' +
`(recommended setup: "frameworkVersion: ^${currentVersion}")\n`
);
if (process.env.SLS_DEBUG) {
legacy.log(
'To ensure safe major version upgrades ensure "frameworkVersion" setting in ' +
'service configuration ' +
`(recommended setup: "frameworkVersion: ^${currentVersion}")\n`
);
}
}
if (
ymlVersion &&
version !== ymlVersion &&
!semver.satisfies(semver.coerce(version).raw, ymlVersion)
) {
const errorMessage = [
`The Serverless version (${version}) does not satisfy the`,
` "frameworkVersion" (${ymlVersion}) in ${this.serverless.configurationFilename}`,
].join('');
throw new ServerlessError(errorMessage, 'FRAMEWORK_VERSION_MISMATCH');
}
if (!configurationInput.service) {
throw new ServerlessError(
`"service" property is missing in ${this.serverless.configurationFilename}`,
'SERVICE_NAME_MISSING'
);
}
if (!configurationInput.provider) {
throw new ServerlessError(
`"provider" property is missing in ${this.serverless.configurationFilename}`,
'PROVIDER_NAME_MISSING'
);
}
if (!_.isObject(configurationInput.provider)) {
// Schema uncoditionally expects `provider` to be an object.
// Ideally if it's fixed at some point, and either we do not support string notation for
// provider, or we support string by schema
configurationInput.provider = this.provider;
}

return this;
}

Expand Down

0 comments on commit b7a6349

Please sign in to comment.