Skip to content

Commit

Permalink
Add support for empty string and binary values
Browse files Browse the repository at this point in the history
  • Loading branch information
mhart committed May 24, 2020
1 parent 8a4a8d1 commit 41dec26
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 42 deletions.
8 changes: 8 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ function validateItem(dataItem, table) {
'Type mismatch for key ' + attr + ' expected: ' + type +
' actual: ' + Object.keys(dataItem[attr])[0])
}
if (dataItem[attr][type] === '' && (type === 'S' || type === 'B')) {
return validationError('One or more parameter values are not valid. ' +
'The AttributeValue for a key attribute cannot contain an empty ' + (type === 'S' ? 'string' : 'binary') + ' value. Key: ' + attr)
}
return checkKeySize(dataItem[attr][type], type, isHash)
}) || traverseIndexes(table, function(attr, type, index) {
if (dataItem[attr] != null && dataItem[attr][type] == null) {
Expand Down Expand Up @@ -229,6 +233,10 @@ function validateKeyPiece(key, attr, type, isHash) {
if (key[attr] == null || key[attr][type] == null) {
return validationError('The provided key element does not match the schema')
}
if (key[attr][type] === '' && (type === 'S' || type === 'B')) {
return validationError('One or more parameter values were invalid: ' +
'The AttributeValue for a key attribute cannot contain an empty ' + (type === 'S' ? 'string' : 'binary') + ' value. Key: ' + attr)
}
return checkKeySize(key[attr][type], type, isHash)
}

Expand Down
16 changes: 8 additions & 8 deletions validations/batchGetItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ exports.custom = function(data) {
var msg = validations.validateExpressionParams(tableData, ['ProjectionExpression'], ['AttributesToGet'])
if (msg) return msg

if (tableData.AttributesToGet) {
msg = validations.findDuplicate(tableData.AttributesToGet)
if (msg) return 'One or more parameter values were invalid: Duplicate value in attribute name: ' + msg
}

msg = validations.validateExpressions(tableData)
if (msg) return msg

var seenKeys = Object.create(null)
for (var i = 0; i < tableData.Keys.length; i++) {
var key = tableData.Keys[i]
Expand All @@ -75,13 +83,5 @@ exports.custom = function(data) {
if (numReqs > 100)
return 'Too many items requested for the BatchGetItem call'
}

if (tableData.AttributesToGet) {
msg = validations.findDuplicate(tableData.AttributesToGet)
if (msg) return 'One or more parameter values were invalid: Duplicate value in attribute name: ' + msg
}

msg = validations.validateExpressions(tableData)
if (msg) return msg
}
}
28 changes: 14 additions & 14 deletions validations/createTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ exports.types = {
children: {
type: 'ValueStruct<LocalSecondaryIndex>',
children: {
IndexName: {
type: 'String',
notNull: true,
regex: '[a-zA-Z0-9_.-]+',
lengthGreaterThanOrEqual: 3,
lengthLessThanOrEqual: 255,
},
KeySchema: {
type: 'List',
notNull: true,
Expand Down Expand Up @@ -101,13 +108,6 @@ exports.types = {
},
},
},
IndexName: {
type: 'String',
notNull: true,
regex: '[a-zA-Z0-9_.-]+',
lengthGreaterThanOrEqual: 3,
lengthLessThanOrEqual: 255,
},
},
},
},
Expand All @@ -116,6 +116,13 @@ exports.types = {
children: {
type: 'ValueStruct<GlobalSecondaryIndex>',
children: {
IndexName: {
type: 'String',
notNull: true,
regex: '[a-zA-Z0-9_.-]+',
lengthGreaterThanOrEqual: 3,
lengthLessThanOrEqual: 255,
},
KeySchema: {
type: 'List',
notNull: true,
Expand Down Expand Up @@ -150,13 +157,6 @@ exports.types = {
},
},
},
IndexName: {
type: 'String',
notNull: true,
regex: '[a-zA-Z0-9_.-]+',
lengthGreaterThanOrEqual: 3,
lengthLessThanOrEqual: 255,
},
ProvisionedThroughput: {
type: 'FieldStruct<ProvisionedThroughput>',
children: {
Expand Down
16 changes: 2 additions & 14 deletions validations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ function checkTypes(data, types) {
case 'boolean':
case 'number':
case 'string':
throw typeError('sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class')
throw typeError("class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to class java.lang.Class (sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl and java.lang.Class are in module java.base of loader 'bootstrap')")
case 'object':
if (!Array.isArray(val)) throw typeError('Start of structure or map found where not expected')
}
Expand All @@ -172,7 +172,7 @@ function checkTypes(data, types) {
case 'boolean':
case 'number':
case 'string':
throw typeError('sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to java.lang.Class')
throw typeError("class sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl cannot be cast to class java.lang.Class (sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl and java.lang.Class are in module java.base of loader 'bootstrap')")
case 'object':
if (Array.isArray(val)) throw typeError('Unrecognized collection type java.util.Map<java.lang.String, com.amazonaws.dynamodb.v20120810.AttributeValue>')
}
Expand Down Expand Up @@ -370,12 +370,6 @@ function validateAttributeValue(value) {
if (msg) return msg
}

if (type == 'B' && !value[type])
return 'One or more parameter values were invalid: An AttributeValue may not contain a null or empty binary type.'

if (type == 'S' && !value[type])
return 'One or more parameter values were invalid: An AttributeValue may not contain an empty string'

if (type == 'NULL' && !value[type])
return 'One or more parameter values were invalid: Null attribute value types must have the value of true'

Expand All @@ -388,12 +382,6 @@ function validateAttributeValue(value) {
if (type == 'BS' && !value[type].length)
return 'One or more parameter values were invalid: Binary sets should not be empty'

if (type == 'SS' && value[type].some(function(x) { return !x })) // eslint-disable-line no-loop-func
return 'One or more parameter values were invalid: An string set may not have a empty string as a member'

if (type == 'BS' && value[type].some(function(x) { return !x })) // eslint-disable-line no-loop-func
return 'One or more parameter values were invalid: Binary sets may not contain null or empty values'

if (type == 'NS') {
for (i = 0; i < value[type].length; i++) {
msg = checkNum(i, value[type])
Expand Down
4 changes: 3 additions & 1 deletion validations/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ exports.custom = function(data) {

for (key in data.ExclusiveStartKey) {
msg = validations.validateAttributeValue(data.ExclusiveStartKey[key])
if (msg) return 'The provided starting key is invalid: ' + msg
// For some reason this message is only added to some messages...?
var prepend = /contains duplicates|number set|numeric value|significant digits|number with magnitude/.test(msg) ? '' : 'The provided starting key is invalid: '
if (msg) return prepend + msg
}

if (data.KeyConditions == null && data.KeyConditionExpression == null) {
Expand Down
12 changes: 7 additions & 5 deletions validations/scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ exports.types = {
type: 'Integer',
greaterThanOrEqual: 0,
},
Limit: {
type: 'Integer',
greaterThanOrEqual: 1,
},
AttributesToGet: {
type: 'List',
lengthGreaterThanOrEqual: 1,
lengthLessThanOrEqual: 255,
children: 'String',
},
Limit: {
type: 'Integer',
greaterThanOrEqual: 1,
},
ExclusiveStartKey: {
type: 'Map<AttributeValue>',
children: 'AttrStruct<ValueStruct>',
Expand Down Expand Up @@ -98,7 +98,9 @@ exports.custom = function(data) {

for (var key in data.ExclusiveStartKey) {
msg = validations.validateAttributeValue(data.ExclusiveStartKey[key])
if (msg) return 'The provided starting key is invalid: ' + msg
// For some reason this message is only added to some messages...?
var prepend = /contains duplicates|number set|numeric value|significant digits|number with magnitude/.test(msg) ? '' : 'The provided starting key is invalid: '
if (msg) return prepend + msg
}

if (data.Segment && data.TotalSegments == null) {
Expand Down

0 comments on commit 41dec26

Please sign in to comment.