Skip to content

Commit

Permalink
fix: change to new joi syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhiga committed Mar 28, 2023
1 parent cee570e commit 75de507
Show file tree
Hide file tree
Showing 7 changed files with 494 additions and 97 deletions.
23 changes: 11 additions & 12 deletions lib/deploy/stepFunctions/compileAlarms.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const _ = require('lodash');
const BbPromise = require('bluebird');
const Joi = require('joi');
const schema = require('./compileAlarms.schema');
const logger = require('../../utils/logger');

Expand Down Expand Up @@ -142,17 +141,17 @@ function validateConfig(serverless, stateMachineName, alarmsObj) {
return false;
}

Joi.validate(alarmsObj, schema, { allowUnknown: false }, (err) => {
if (err) {
logger.log(
`State machine [${stateMachineName}] : alarms config is malformed. `
+ 'Please see https://github.com/horike37/serverless-step-functions for examples. '
+ `${err}`,
);
return false;
}
return true;
});
const { error } = schema.validate(alarmsObj, { allowUnknown: false });

if (error) {
logger.log(
`State machine [${stateMachineName}] : alarms config is malformed. `
+ 'Please see https://github.com/horike37/serverless-step-functions for examples. '
+ `${error}`,
);
return false;
}

return true;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/deploy/stepFunctions/compileAlarms.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const arn = Joi.alternatives().try(
),
}),
Joi.object().keys({
'Fn::Join': Joi.array().items([
'Fn::Join': Joi.array().items(
Joi.string(),
Joi.array().items([
Joi.array().items(
Joi.string(),
Joi.object().keys({
Ref: Joi.string(),
}),
]),
]),
),
),
}),
);

Expand Down
23 changes: 11 additions & 12 deletions lib/deploy/stepFunctions/compileNotifications.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const Joi = require('joi');
const crypto = require('crypto');
const BbPromise = require('bluebird');
const schema = require('./compileNotifications.schema');
Expand Down Expand Up @@ -326,18 +325,18 @@ function validateConfig(serverless, stateMachineName, stateMachineObj, notificat
return false;
}

Joi.validate(notificationsObj, schema, { allowUnknown: false }, (err) => {
if (err) {
logger.log(
`State machine [${stateMachineName}] : notifications config is malformed. `
+ 'Please see https://github.com/horike37/serverless-step-functions for examples. '
+ `${err}`,
);
return false;
}
const { error } = schema.validate(
notificationsObj, { allowUnknown: false },
);

return true;
});
if (error) {
logger.log(
`State machine [${stateMachineName}] : notifications config is malformed. `
+ 'Please see https://github.com/horike37/serverless-step-functions for examples. '
+ `${error}`,
);
return false;
}

return true;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/deploy/stepFunctions/compileNotifications.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ const arn = Joi.alternatives().try(
'Fn::GetAtt': Joi.array().items(Joi.string()),
}),
Joi.object().keys({
'Fn::Join': Joi.array().items([
'Fn::Join': Joi.array().items(
Joi.string(),
Joi.array().items([
Joi.array().items(
Joi.string(),
Joi.object().keys({
Ref: Joi.string(),
}),
]),
]),
),
),
}),
);

Expand Down
29 changes: 12 additions & 17 deletions lib/deploy/stepFunctions/compileStateMachines.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';

const _ = require('lodash');
const Joi = require('joi');
const aslValidator = require('asl-validator');
const BbPromise = require('bluebird');
const crypto = require('crypto');
Expand Down Expand Up @@ -107,17 +106,13 @@ module.exports = {
} else {
Tags = toTags(this.serverless.service.provider.tags);
}
let validationValue;
Joi.validate(stateMachineObj, schema, { allowUnknown: false }, (err, value) => {
if (err) {
const errorMessage = `State machine [${stateMachineName}] is malformed. `
const { error, value } = schema.validate(stateMachineObj, { allowUnknown: false });
if (error) {
const errorMessage = `State machine [${stateMachineName}] is malformed. `
+ 'Please check the README for more info. '
+ `${err}`;
throw new this.serverless.classes.Error(errorMessage);
} else {
validationValue = value;
}
});
+ `${error}`;
throw new this.serverless.classes.Error(errorMessage);
}

if (stateMachineObj.definition) {
if (this.serverless.service.stepFunctions.validate) {
Expand Down Expand Up @@ -204,23 +199,23 @@ module.exports = {
_.forEach(stateMachineTags, tag => Tags.push(tag));
}

if (validationValue.loggingConfig) {
const Destinations = (validationValue.loggingConfig.destinations || [])
if (value.loggingConfig) {
const Destinations = (value.loggingConfig.destinations || [])
.map(arn => ({
CloudWatchLogsLogGroup: {
LogGroupArn: arn,
},
}));
LoggingConfiguration = {
Level: validationValue.loggingConfig.level,
IncludeExecutionData: validationValue.loggingConfig.includeExecutionData,
Level: value.loggingConfig.level,
IncludeExecutionData: value.loggingConfig.includeExecutionData,
Destinations,
};
}

if (validationValue.tracingConfig) {
if (value.tracingConfig) {
TracingConfiguration = {
Enabled: validationValue.tracingConfig.enabled,
Enabled: value.tracingConfig.enabled,
};
}

Expand Down
8 changes: 4 additions & 4 deletions lib/deploy/stepFunctions/compileStateMachines.schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ const arn = Joi.alternatives().try(
),
}),
Joi.object().keys({
'Fn::Join': Joi.array().items([
'Fn::Join': Joi.array().items(
Joi.string(),
Joi.array().items([
Joi.array().items(
Joi.string(),
Joi.object().keys({
Ref: Joi.string(),
}),
]),
]),
),
),
}),
);

Expand Down
Loading

0 comments on commit 75de507

Please sign in to comment.