UNPKG

3.79 kBJavaScriptView Raw
1var validations = require('./index')
2
3exports.types = {
4 Select: {
5 type: 'String',
6 enum: ['SPECIFIC_ATTRIBUTES', 'COUNT', 'ALL_ATTRIBUTES', 'ALL_PROJECTED_ATTRIBUTES'],
7 },
8 IndexName: {
9 type: 'String',
10 regex: '[a-zA-Z0-9_.-]+',
11 lengthGreaterThanOrEqual: 3,
12 lengthLessThanOrEqual: 255,
13 },
14 ReturnConsumedCapacity: {
15 type: 'String',
16 enum: ['INDEXES', 'TOTAL', 'NONE'],
17 },
18 QueryFilter: {
19 type: 'Map<Condition>',
20 children: {
21 type: 'ValueStruct<Condition>',
22 children: {
23 AttributeValueList: {
24 type: 'List',
25 children: 'AttrStruct<ValueStruct>',
26 },
27 ComparisonOperator: {
28 type: 'String',
29 notNull: true,
30 enum: ['IN', 'NULL', 'BETWEEN', 'LT', 'NOT_CONTAINS', 'EQ', 'GT', 'NOT_NULL', 'NE', 'LE', 'BEGINS_WITH', 'GE', 'CONTAINS'],
31 },
32 },
33 },
34 },
35 TableName: {
36 type: 'String',
37 notNull: true,
38 regex: '[a-zA-Z0-9_.-]+',
39 lengthGreaterThanOrEqual: 3,
40 lengthLessThanOrEqual: 255,
41 },
42 ConditionalOperator: {
43 type: 'String',
44 enum: ['OR', 'AND'],
45 },
46 AttributesToGet: {
47 type: 'List',
48 lengthGreaterThanOrEqual: 1,
49 lengthLessThanOrEqual: 255,
50 children: 'String',
51 },
52 Limit: {
53 type: 'Integer',
54 greaterThanOrEqual: 1,
55 },
56 KeyConditions: {
57 type: 'Map<Condition>',
58 children: {
59 type: 'ValueStruct<Condition>',
60 children: {
61 ComparisonOperator: {
62 type: 'String',
63 notNull: true,
64 enum: ['IN', 'NULL', 'BETWEEN', 'LT', 'NOT_CONTAINS', 'EQ', 'GT', 'NOT_NULL', 'NE', 'LE', 'BEGINS_WITH', 'GE', 'CONTAINS'],
65 },
66 AttributeValueList: {
67 type: 'List',
68 children: 'AttrStruct<ValueStruct>',
69 },
70 },
71 },
72 },
73 ExclusiveStartKey: {
74 type: 'Map<AttributeValue>',
75 children: 'AttrStruct<ValueStruct>',
76 },
77 ConsistentRead: 'Boolean',
78 ScanIndexForward: 'Boolean',
79 KeyConditionExpression: {
80 type: 'String',
81 },
82 FilterExpression: {
83 type: 'String',
84 },
85 ProjectionExpression: {
86 type: 'String',
87 },
88 ExpressionAttributeValues: {
89 type: 'Map<AttributeValue>',
90 children: 'AttrStruct<ValueStruct>',
91 },
92 ExpressionAttributeNames: {
93 type: 'Map<java.lang.String>',
94 children: 'String',
95 },
96}
97
98exports.custom = function(data) {
99
100 var msg = validations.validateExpressionParams(data,
101 ['ProjectionExpression', 'FilterExpression', 'KeyConditionExpression'],
102 ['AttributesToGet', 'QueryFilter', 'ConditionalOperator', 'KeyConditions'])
103 if (msg) return msg
104
105 var key
106 msg = validations.validateConditions(data.QueryFilter)
107 if (msg) return msg
108
109 if (data.AttributesToGet) {
110 msg = validations.findDuplicate(data.AttributesToGet)
111 if (msg) return 'One or more parameter values were invalid: Duplicate value in attribute name: ' + msg
112 }
113
114 for (key in data.ExclusiveStartKey) {
115 msg = validations.validateAttributeValue(data.ExclusiveStartKey[key])
116 if (msg) return 'The provided starting key is invalid: ' + msg
117 }
118
119 if (data.KeyConditions == null && data.KeyConditionExpression == null) {
120 return 'Either the KeyConditions or KeyConditionExpression parameter must be specified in the request.'
121 }
122
123 msg = validations.validateExpressions(data)
124 if (msg) return msg
125
126 if (data._keyCondition != null) {
127 data.KeyConditions = validations.convertKeyCondition(data._keyCondition.expression)
128 if (typeof data.KeyConditions == 'string') {
129 return data.KeyConditions
130 }
131 }
132
133 msg = validations.validateConditions(data.KeyConditions)
134 if (msg) return msg
135
136 var numConditions = Object.keys(data.KeyConditions || {}).length
137 if (numConditions != 1 && numConditions != 2) {
138 return 'Conditions can be of length 1 or 2 only'
139 }
140}