From 41dec26f13f727a7f38ad3e61c00a25a8a5f93e6 Mon Sep 17 00:00:00 2001 From: Michael Hart Date: Sun, 24 May 2020 15:51:12 -0400 Subject: [PATCH] Add support for empty string and binary values --- db/index.js | 8 ++++++++ validations/batchGetItem.js | 16 ++++++++-------- validations/createTable.js | 28 ++++++++++++++-------------- validations/index.js | 16 ++-------------- validations/query.js | 4 +++- validations/scan.js | 12 +++++++----- 6 files changed, 42 insertions(+), 42 deletions(-) diff --git a/db/index.js b/db/index.js index cf9c4c5..03f29d4 100644 --- a/db/index.js +++ b/db/index.js @@ -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) { @@ -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) } diff --git a/validations/batchGetItem.js b/validations/batchGetItem.js index 3bb5416..6fbea27 100644 --- a/validations/batchGetItem.js +++ b/validations/batchGetItem.js @@ -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] @@ -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 } } diff --git a/validations/createTable.js b/validations/createTable.js index 704384c..4bd9fc6 100644 --- a/validations/createTable.js +++ b/validations/createTable.js @@ -67,6 +67,13 @@ exports.types = { children: { type: 'ValueStruct', children: { + IndexName: { + type: 'String', + notNull: true, + regex: '[a-zA-Z0-9_.-]+', + lengthGreaterThanOrEqual: 3, + lengthLessThanOrEqual: 255, + }, KeySchema: { type: 'List', notNull: true, @@ -101,13 +108,6 @@ exports.types = { }, }, }, - IndexName: { - type: 'String', - notNull: true, - regex: '[a-zA-Z0-9_.-]+', - lengthGreaterThanOrEqual: 3, - lengthLessThanOrEqual: 255, - }, }, }, }, @@ -116,6 +116,13 @@ exports.types = { children: { type: 'ValueStruct', children: { + IndexName: { + type: 'String', + notNull: true, + regex: '[a-zA-Z0-9_.-]+', + lengthGreaterThanOrEqual: 3, + lengthLessThanOrEqual: 255, + }, KeySchema: { type: 'List', notNull: true, @@ -150,13 +157,6 @@ exports.types = { }, }, }, - IndexName: { - type: 'String', - notNull: true, - regex: '[a-zA-Z0-9_.-]+', - lengthGreaterThanOrEqual: 3, - lengthLessThanOrEqual: 255, - }, ProvisionedThroughput: { type: 'FieldStruct', children: { diff --git a/validations/index.js b/validations/index.js index 6a6a025..3b55ba5 100644 --- a/validations/index.js +++ b/validations/index.js @@ -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') } @@ -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') } @@ -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' @@ -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]) diff --git a/validations/query.js b/validations/query.js index 3433039..4a8fd77 100644 --- a/validations/query.js +++ b/validations/query.js @@ -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) { diff --git a/validations/scan.js b/validations/scan.js index 5186106..b7cd562 100644 --- a/validations/scan.js +++ b/validations/scan.js @@ -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', children: 'AttrStruct', @@ -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) {