Skip to content

Commit a1298f2

Browse files
committed
Throws when trying to set a records field that has a dot in the key
1 parent f8088a3 commit a1298f2

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

client/src/set/fieldParsers/record.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export default async (
1919

2020
const fn = fieldParsers[fields.values.type]
2121

22+
if (Object.keys(payload).filter((key) => /\./g.test(key)).length) {
23+
throw new Error('Cannot use "." in a record key')
24+
}
25+
2226
if (payload.$delete) {
2327
result.push('7', field, '')
2428
return 0

client/src/set/validate.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ export default async function parseSetObject(
6565

6666
// && (<any>payload.parents).$noRoot
6767
const isNonEmpty = (v: any): boolean => !!(v && v.length)
68-
if (payload.parents && (payload.parents['$noRoot'] || Array.isArray(payload.parents) || payload.parents.$value || isNonEmpty(payload.parents['$add']))) {
68+
if (
69+
payload.parents &&
70+
(payload.parents['$noRoot'] ||
71+
Array.isArray(payload.parents) ||
72+
payload.parents.$value ||
73+
isNonEmpty(payload.parents['$add']))
74+
) {
6975
result[0] += 'N'
7076
}
7177

client/test/record.ts

+41-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ test.before(async (t) => {
2121
},
2222
},
2323
hello: {
24+
prefix: 'he',
2425
fields: {
2526
name: { type: 'string' },
2627
members: {
@@ -48,7 +49,7 @@ test.after(async (t) => {
4849
await t.connectionsAreEmpty()
4950
})
5051

51-
test.serial('remove object from record', async (t) => {
52+
test.serial.failing('remove object from record', async (t) => {
5253
const client = connect({ port: port })
5354

5455
const thingId = await client.set({
@@ -110,3 +111,42 @@ test.serial('remove object from record', async (t) => {
110111

111112
await client.destroy()
112113
})
114+
115+
test.serial('fail when setting key with a dot', async (t) => {
116+
const client = connect({ port: port })
117+
118+
await client.set({
119+
$id: 'he111111',
120+
members: {
121+
'Very long first': {
122+
x: 'first',
123+
},
124+
},
125+
})
126+
127+
t.throwsAsync(
128+
client.set({
129+
$id: 'he111111',
130+
name: 'derp',
131+
members: {
132+
hallo: {
133+
x: 'hallo',
134+
},
135+
'1.yeye': {
136+
x: 'doei',
137+
},
138+
},
139+
})
140+
)
141+
await wait(200)
142+
143+
t.deepEqual(await client.get({ $id: 'he111111', members: true }), {
144+
members: {
145+
'Very long first': {
146+
x: 'first',
147+
},
148+
},
149+
})
150+
151+
await client.destroy()
152+
})

0 commit comments

Comments
 (0)