Skip to content

Commit 877eeb1

Browse files
authored
Merge pull request dynamodb-toolbox#88 from tonyjoblin/getkey-handling-falsy-keys
Getkey handling falsy keys
2 parents 73dca2b + 14ffab1 commit 877eeb1

File tree

3 files changed

+188
-10
lines changed

3 files changed

+188
-10
lines changed

.eslintrc.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
{
22
"env": {
33
"es6": true,
4-
"node": true
4+
"node": true,
5+
"jest": true
56
},
67
"extends": "eslint:recommended",
78
"parserOptions": {
89
"ecmaVersion": 2018,
910
"sourceType": "module"
1011
},
1112
"rules": {
12-
"indent": [
13-
"error",
14-
2
15-
],
1613
"linebreak-style": [
1714
"error",
1815
"unix"

__tests__/lib/getKey.unit.test.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
const getKey = require('../../lib/getKey')
2+
3+
describe('getKey', () => {
4+
describe('pk', () => {
5+
describe('is a string', () => {
6+
it('returns the pk data in an object', () => {
7+
const data = { pk: 'id1' }
8+
const schema = {
9+
pk: { partitionKey: true, type: 'string', coerce: true },
10+
}
11+
const dummyDocumentClient = {}
12+
13+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
14+
15+
expect(result).toEqual({ pk: 'id1' })
16+
})
17+
18+
it('can not be empty, its an AWS Dynamodb thing', () => {
19+
const data = { pk: '' }
20+
const schema = {
21+
pk: { partitionKey: true, type: 'string', coerce: true },
22+
}
23+
const dummyDocumentClient = {}
24+
25+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk')).toThrow(new Error('\'pk\' is required'))
26+
})
27+
})
28+
29+
describe('can be a number', () => {
30+
it('returns the pk data', () => {
31+
const data = { pk: 123 }
32+
const schema = {
33+
pk: { partitionKey: true, type: 'number', coerce: true },
34+
}
35+
const dummyDocumentClient = {}
36+
37+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
38+
39+
expect(result).toEqual({ pk: 123 })
40+
})
41+
42+
it('can be 0', () => {
43+
const data = { pk: 0 }
44+
const schema = {
45+
pk: { partitionKey: true, type: 'number', coerce: true },
46+
}
47+
const dummyDocumentClient = {}
48+
49+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
50+
51+
expect(result).toEqual({ pk: 0 })
52+
})
53+
54+
it('can coerce from string', () => {
55+
const data = { pk: '123' }
56+
const schema = {
57+
pk: { partitionKey: true, type: 'number', coerce: true },
58+
}
59+
const dummyDocumentClient = {}
60+
61+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
62+
63+
expect(result).toEqual({ pk: 123 })
64+
})
65+
})
66+
67+
it('if no partitionKey in data will error', () => {
68+
const data = {}
69+
const schema = {
70+
pk: { partitionKey: true, type: 'string', coerce: true },
71+
}
72+
const dummyDocumentClient = {}
73+
74+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')).toThrow(new Error('\'pk\' is required'))
75+
})
76+
})
77+
78+
describe('sk', () => {
79+
it('returned object does not contain sort key if not required', () => {
80+
const data = { pk: 'id1' }
81+
const schema = {
82+
pk: { partitionKey: true, type: 'string', coerce: true },
83+
}
84+
const dummyDocumentClient = {}
85+
86+
const result = getKey(dummyDocumentClient)(data, schema, 'pk')
87+
88+
expect(result).toEqual({
89+
pk: 'id1'
90+
})
91+
})
92+
93+
describe('can be a string', () => {
94+
it('returns the sk data', () => {
95+
const data = { pk: 'id1', sk: 'sk1' }
96+
const schema = {
97+
pk: { partitionKey: true, type: 'string', coerce: true },
98+
sk: { sortKey: true, type: 'string', coerce: true },
99+
}
100+
const dummyDocumentClient = {}
101+
102+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
103+
104+
expect(result).toEqual({ pk: 'id1', sk: 'sk1' })
105+
})
106+
107+
it('can not be empty, its an AWS Dynamodb thing', () => {
108+
const data = { pk: 'id1', sk: '' }
109+
const schema = {
110+
pk: { partitionKey: true, type: 'string', coerce: true },
111+
sk: { sortKey: true, type: 'string', coerce: true },
112+
}
113+
const dummyDocumentClient = {}
114+
115+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')).toThrow(new Error('\'sk\' is required'))
116+
})
117+
})
118+
119+
describe('can be a number', () => {
120+
it('returns the sk data', () => {
121+
const data = { pk: 'id1', sk: 5 }
122+
const schema = {
123+
pk: { partitionKey: true, type: 'string', coerce: true },
124+
sk: { sortKey: true, type: 'number', coerce: true },
125+
}
126+
const dummyDocumentClient = {}
127+
128+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
129+
130+
expect(result).toEqual({ pk: 'id1', sk: 5 })
131+
})
132+
133+
it('can coerce sortKey string to number', () => {
134+
const data = { pk: 'id1', sk: '5' }
135+
const schema = {
136+
pk: { partitionKey: true, type: 'string', coerce: true },
137+
sk: { sortKey: true, type: 'number', coerce: true },
138+
}
139+
const dummyDocumentClient = {}
140+
141+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
142+
143+
expect(result).toEqual({
144+
pk: 'id1',
145+
sk: 5
146+
})
147+
})
148+
149+
it('can be 0', () => {
150+
const data = { pk: 'id1', sk: 0 }
151+
const schema = {
152+
pk: { partitionKey: true, type: 'string', coerce: true },
153+
sk: { sortKey: true, type: 'number', coerce: true },
154+
}
155+
const dummyDocumentClient = {}
156+
157+
const result = getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')
158+
159+
expect(result).toEqual({
160+
pk: 'id1',
161+
sk: 0
162+
})
163+
})
164+
})
165+
166+
it('if no sortKey in data will error', () => {
167+
const data = { pk: 'id1' }
168+
const schema = {
169+
pk: { partitionKey: true, type: 'string', coerce: true },
170+
sk: { sortKey: true, type: 'number', coerce: true },
171+
}
172+
const dummyDocumentClient = {}
173+
174+
expect(() => getKey(dummyDocumentClient)(data, schema, 'pk', 'sk')).toThrow(new Error('\'sk\' is required'))
175+
})
176+
})
177+
})

lib/getKey.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ module.exports = (DocumentClient) => (data,schema,partitionKey,sortKey) => {
1919
// Intialize validate type
2020
let validateType = validateTypes(DocumentClient)
2121

22-
let pk = data[partitionKey] ||
23-
error(`'${partitionKey}'${schema[partitionKey].alias ? ` or '${schema[partitionKey].alias}'` : ''} is required`)
24-
25-
let sk = sortKey === null || data[sortKey] ||
26-
error(`'${sortKey}'${schema[sortKey].alias ? ` or '${schema[sortKey].alias}'` : ''} is required`)
22+
let pk = data[partitionKey]
23+
if (pk === undefined || pk === null || pk === '') {
24+
error(`'${partitionKey}'${schema[partitionKey].alias ? ` or '${schema[partitionKey].alias}'` : ''} is required`)
25+
}
26+
27+
const sk = data[sortKey]
28+
if (sortKey && (sk === undefined || sk === null || sk === '')) {
29+
error(`'${sortKey}'${schema[sortKey].alias ? ` or '${schema[sortKey].alias}'` : ''} is required`)
30+
}
2731

2832
return Object.assign(
2933
{ [partitionKey]: transformAttr(schema[partitionKey],validateType(schema[partitionKey],partitionKey,pk,data),data) },

0 commit comments

Comments
 (0)