Skip to content

Commit cb103ef

Browse files
committed
chore: fix joi scheme for dynamodb
1 parent c768fee commit cb103ef

File tree

2 files changed

+210
-169
lines changed

2 files changed

+210
-169
lines changed

lib/apiGateway/schema.js

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -160,70 +160,21 @@ const partitionKey = Joi.alternatives().try([
160160
)
161161
])
162162

163-
const allowedDynamodbActions = ['PutItem', 'GetItem', 'DeleteItem', 'UpdateItem']
164-
const dynamodbDefaultKeyScheme = Joi.alternatives().try([
165-
Joi.string(),
166-
Joi.object()
167-
.keys({
168-
pathParam: Joi.string(),
169-
queryStringParam: Joi.string(),
170-
attributeType: Joi.string().required()
171-
})
172-
.xor('pathParam', 'queryStringParam')
173-
.error(
174-
customErrorBuilder(
175-
'object.xor',
176-
'key must contain "pathParam" or "queryStringParam" and only one'
177-
)
178-
)
179-
])
180-
const hashKey = Joi.when('action', {
181-
is: Joi.string()
182-
.valid(allowedDynamodbActions)
183-
.required(),
184-
then: dynamodbDefaultKeyScheme,
185-
otherwise: Joi.when('method', {
186-
is: Joi.string()
187-
.valid('post')
188-
.insensitive()
189-
.required(),
190-
then: Joi.string()
191-
.required()
192-
.error(
193-
customMultipleErrorBuilder({
194-
'string.base': [
195-
'"hashKey" must be a string when you define post "method" and not define',
196-
' "action" expolicitly since the hashKey value is auto-generated on AWS end'
197-
].join(''),
198-
'any.required': [
199-
'"hashKey" is required when you define post "method" and not define',
200-
' "action" expolicitly since the hashKey value is auto-generated on AWS end'
201-
].join('')
202-
})
203-
),
204-
otherwise: dynamodbDefaultKeyScheme
163+
const allowedDynamodbActions = ['PutItem', 'GetItem', 'DeleteItem']
164+
const dynamodbDefaultKeyScheme = Joi.object()
165+
.keys({
166+
pathParam: Joi.string(),
167+
queryStringParam: Joi.string(),
168+
attributeType: Joi.string().required()
205169
})
206-
})
170+
.xor('pathParam', 'queryStringParam')
171+
.error(
172+
customErrorBuilder(
173+
'object.xor',
174+
'key must contain "pathParam" or "queryStringParam" and only one'
175+
)
176+
)
207177

208-
const rangeKey = Joi.when('action', {
209-
is: Joi.string()
210-
.valid(allowedDynamodbActions)
211-
.required(),
212-
then: dynamodbDefaultKeyScheme,
213-
otherwise: Joi.when('method', {
214-
is: Joi.string()
215-
.valid('post')
216-
.insensitive()
217-
.required(),
218-
then: Joi.forbidden().error(
219-
customErrorBuilder(
220-
'any.unknown',
221-
'you can not define "rangeKey" when post "method" is defined without "action"'
222-
)
223-
),
224-
otherwise: dynamodbDefaultKeyScheme
225-
})
226-
})
227178

228179
const request = Joi.object({
229180
template: Joi.object().required()
@@ -263,30 +214,11 @@ const proxiesSchemas = {
263214
}),
264215
dynamodb: Joi.object({
265216
dynamodb: proxy.append({
266-
action: Joi.string()
267-
.valid(allowedDynamodbActions)
268-
.when('method', {
269-
is: Joi.string()
270-
.valid(['options', 'head', 'any'])
271-
.insensitive(),
272-
then: Joi.required().error(
273-
customErrorBuilder(
274-
'any.required',
275-
'"action" is required when you define options, head, any to "method" property'
276-
)
277-
)
278-
}),
279-
tableName: Joi.alternatives()
280-
.try([
281-
Joi.string(),
282-
Joi.object().keys({
283-
Ref: Joi.string().required()
284-
})
285-
])
286-
.required(),
217+
action: Joi.string().valid(allowedDynamodbActions).required(),
218+
tableName: stringOrRef.required(),
287219
condition: Joi.string(),
288-
hashKey,
289-
rangeKey
220+
hashKey: dynamodbDefaultKeyScheme.required(),
221+
rangeKey: dynamodbDefaultKeyScheme
290222
})
291223
})
292224
}

0 commit comments

Comments
 (0)