Skip to content

Commit

Permalink
Merge c3c514e into 2bc837f
Browse files Browse the repository at this point in the history
  • Loading branch information
horike37 committed Sep 2, 2019
2 parents 2bc837f + c3c514e commit 89847ac
Show file tree
Hide file tree
Showing 9 changed files with 2,246 additions and 2 deletions.
108 changes: 107 additions & 1 deletion lib/apiGateway/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ const customErrorBuilder = (type, message) => (errors) => {
return errors
}

// e.g. errorObject is { type1: message1, type2: message2 }
const customMultipleErrorBuilder = (errorObject) => (errors) => {
for (const error of errors) {
if (_.hasIn(errorObject, error.type)) {
error.message = errorObject[error.type]
}
}
return errors
}

const Joi = require('@hapi/joi')

const path = Joi.string().required()
Expand Down Expand Up @@ -119,6 +129,74 @@ const partitionKey = Joi.alternatives().try([
)
])

const allowedDynamodbActions = ['PutItem', 'GetItem', 'DeleteItem', 'UpdateItem']
const dynamodbDefaultKeyScheme = Joi.alternatives().try([
Joi.string(),
Joi.object()
.keys({
pathParam: Joi.string(),
queryStringParam: Joi.string(),
attributeType: Joi.string().required()
})
.xor('pathParam', 'queryStringParam')
.error(
customErrorBuilder(
'object.xor',
'key must contain "pathParam" or "queryStringParam" and only one'
)
)
])
const hashKey = Joi.when('action', {
is: Joi.string()
.valid(allowedDynamodbActions)
.required(),
then: dynamodbDefaultKeyScheme,
otherwise: Joi.when('method', {
is: Joi.string()
.valid('post')
.insensitive()
.required(),
then: Joi.string()
.required()
.error(
customMultipleErrorBuilder({
'string.base': [
'"hashKey" must be a string when you define post "method" and not define',
' "action" expolicitly since the hashKey value is auto-generated on AWS end'
].join(''),
'any.required': [
'"hashKey" is required when you define post "method" and not define',
' "action" expolicitly since the hashKey value is auto-generated on AWS end'
].join('')
})
),
otherwise: dynamodbDefaultKeyScheme
})
})

const rangeKey = Joi.when('action', {
is: Joi.string()
.valid(allowedDynamodbActions)
.required(),
then: dynamodbDefaultKeyScheme,
otherwise: Joi.when('method', {
is: Joi.string()
.valid('post')
.insensitive()
.required(),
then: Joi.string().error(
customErrorBuilder(
'string.base',
[
'"rengeKey" must be a string when you define post to "method" and not define',
' "action" expolicitly since the hashKey value is auto-generated on AWS end'
].join('')
)
),
otherwise: dynamodbDefaultKeyScheme
})
})

const stringOrGetAtt = (propertyName, attributeName) => {
return Joi.alternatives().try([
Joi.string(),
Expand All @@ -145,7 +223,7 @@ const request = Joi.object({
template: Joi.object().required()
})

const allowedProxies = ['kinesis', 'sqs', 's3', 'sns']
const allowedProxies = ['kinesis', 'sqs', 's3', 'sns', 'dynamodb']

const proxiesSchemas = {
kinesis: Joi.object({
Expand All @@ -168,6 +246,34 @@ const proxiesSchemas = {
queueName: stringOrGetAtt('queueName', 'QueueName').required(),
requestParameters
})
}),
dynamodb: Joi.object({
dynamodb: proxy.append({
action: Joi.string()
.valid(allowedDynamodbActions)
.when('method', {
is: Joi.string()
.valid(['options', 'head', 'any'])
.insensitive(),
then: Joi.required().error(
customErrorBuilder(
'any.required',
'"action" is required when you define options, head, any to "method" property'
)
)
}),
tableName: Joi.alternatives()
.try([
Joi.string(),
Joi.object().keys({
Ref: Joi.string().required()
})
])
.required(),
condition: Joi.string(),
hashKey,
rangeKey
})
})
}

Expand Down

0 comments on commit 89847ac

Please sign in to comment.