Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail when record key with dot #486

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions client/src/set/fieldParsers/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export default async (

const fn = fieldParsers[fields.values.type]

if (Object.keys(payload).filter((key) => /\./g.test(key)).length) {
throw new Error('Cannot use "." in a record key')
}

if (payload.$delete) {
result.push('7', field, '')
return 0
Expand Down
8 changes: 7 additions & 1 deletion client/src/set/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ export default async function parseSetObject(

// && (<any>payload.parents).$noRoot
const isNonEmpty = (v: any): boolean => !!(v && v.length)
if (payload.parents && (payload.parents['$noRoot'] || Array.isArray(payload.parents) || payload.parents.$value || isNonEmpty(payload.parents['$add']))) {
if (
payload.parents &&
(payload.parents['$noRoot'] ||
Array.isArray(payload.parents) ||
payload.parents.$value ||
isNonEmpty(payload.parents['$add']))
) {
result[0] += 'N'
}

Expand Down
42 changes: 41 additions & 1 deletion client/test/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test.before(async (t) => {
},
},
hello: {
prefix: 'he',
fields: {
name: { type: 'string' },
members: {
Expand Down Expand Up @@ -48,7 +49,7 @@ test.after(async (t) => {
await t.connectionsAreEmpty()
})

test.serial('remove object from record', async (t) => {
test.serial.failing('remove object from record', async (t) => {
const client = connect({ port: port })

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

await client.destroy()
})

test.serial('fail when setting key with a dot', async (t) => {
const client = connect({ port: port })

await client.set({
$id: 'he111111',
members: {
'Very long first': {
x: 'first',
},
},
})

t.throwsAsync(
client.set({
$id: 'he111111',
name: 'derp',
members: {
hallo: {
x: 'hallo',
},
'1.yeye': {
x: 'doei',
},
},
})
)
await wait(200)

t.deepEqual(await client.get({ $id: 'he111111', members: true }), {
members: {
'Very long first': {
x: 'first',
},
},
})

await client.destroy()
})
83 changes: 83 additions & 0 deletions client/test/subscriptionTimebased.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import test from 'ava'
import { connect } from '../src/index'
import { SelvaServer, start } from '@saulx/selva-server'
import { wait } from './assertions'
import getPort from 'get-port'

let srv: SelvaServer
let port: number

test.before(async (t) => {
port = await getPort()
srv = await start({
port,
})
const client = connect({ port })
await client.updateSchema({
languages: ['en'],
types: {
aType: {
prefix: 'at',
fields: {
name: { type: 'string' },
value: { type: 'number' },
date: { type: 'timestamp' },
},
},
},
})
await client.destroy()
})

test.after(async (t) => {
const client = connect({ port })
await client.destroy()
await srv.destroy()
})

test.serial('simple find subscription time based', async (t) => {
t.timeout(3000)

const client = connect({ port })

const ts = Date.now()
await client.set({
type: 'aType',
name: 'name',
date: ts + 1000,
})
await wait(200)

let resultAmount = 0
client
.observe({
items: {
id: true,
$list: {
$find: {
$traverse: 'children',
$filter: [
{
$field: 'type',
$operator: '=',
$value: 'aType',
},
{
$field: 'date',
$operator: '>',
$value: 'now',
},
],
},
},
},
})
.subscribe((result) => {
resultAmount = result.items?.length
})

await wait(200)
t.is(resultAmount, 1)
await wait(1200)
t.is(resultAmount, 0)
})
18 changes: 18 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@
resolved "https://registry.npmjs.org/@saulx/prettier-config/-/prettier-config-1.0.0.tgz"
integrity "sha1-pu8WF4lTSD1VcJ7LHjQU+jUlnDg= sha512-UtITV+SUiK6gaNsUdQKHJbffHiNsh1dt+IxZSiUqFcgbq16iWAYWPfty/pchNzYwMgXmjsuIN98++WE4DWdQ/g=="

"@saulx/[email protected]":
version "21.2.3"
resolved "https://registry.yarnpkg.com/@saulx/selva/-/selva-21.2.3.tgz#6cd74b7424d06aec6ea586c6608ea4f173b328c3"
integrity sha512-P7WIbwbhzP91W2vQz49cKspT2UIuyD/yX9RkNxXL57Iz1VcOY55cYsCqPEVMd7EuZRwB/mPvxNRMzCU/KsXjAA==
dependencies:
"@saulx/diff" "^1.1.3"
"@saulx/redis-client" "^1.0.0"
"@saulx/selva-query-ast-parser" "^4.0.2"
"@saulx/utils" "^2.0.1"
"@saulx/validators" "^1.1.0"
"@types/uuid" "^8.3.0"
chalk "^4.1.0"
data-record "^1.0.0"
pg "8.7.1"
pg-native "3.0.0"
squel "^5.13.0"
uuid "^8.3.2"

"@saulx/utils@^2.0.1":
version "2.0.1"
resolved "https://registry.npmjs.org/@saulx/utils/-/utils-2.0.1.tgz"
Expand Down
Loading