Skip to content

Commit 1d8c124

Browse files
committed
fix: allow empty generic tag filter values
1 parent b718036 commit 1d8c124

5 files changed

Lines changed: 48 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"nostream": patch
3+
---
4+
5+
Allow generic tag filters to match empty string tag values.

src/schemas/filter-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const filterSchema = z
1414
until: createdAtSchema.optional(),
1515
limit: z.number().int().min(0).optional(),
1616
})
17-
.catchall(z.array(z.string().min(1).max(1024)))
17+
.catchall(z.array(z.string().max(1024)))
1818
.superRefine((data, ctx) => {
1919
for (const key of Object.keys(data)) {
2020
if (!knownFilterKeys.has(key) && !isGenericTagQuery(key)) {

test/unit/repositories/event-repository.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,18 @@ describe('EventRepository', () => {
383383
)
384384
})
385385
})
386+
387+
describe('#d', () => {
388+
it('selects events by empty #d tag value', () => {
389+
const filters = [{ '#d': [''] }]
390+
391+
const query = repository.findByFilters(filters).toString()
392+
393+
expect(query).to.equal(
394+
'select "events".* from "events" left join "event_tags" on "events"."event_id" = "event_tags"."event_id" where (event_tags.tag_name = \'d\' AND event_tags.tag_value = \'\') order by "event_created_at" asc, "event_id" asc limit 500',
395+
)
396+
})
397+
})
386398
})
387399

388400
describe('2 filters', () => {

test/unit/schemas/filter-schema.spec.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ describe('NIP-01', () => {
5555
expect(result.value).to.deep.equal(filter)
5656
})
5757

58+
it('accepts empty strings in generic tag filters', () => {
59+
const filterWithEmptyTagValue = {
60+
'#d': [''],
61+
}
62+
63+
const result = validateSchema(filterSchema)(filterWithEmptyTagValue)
64+
65+
expect(result.error).to.be.undefined
66+
expect(result.value).to.deep.equal(filterWithEmptyTagValue)
67+
})
68+
5869
const cases = {
5970
ids: [{ message: 'must be an array', transform: assocPath(['ids'], null) }],
6071
prefixOrId: [
@@ -103,23 +114,20 @@ describe('NIP-01', () => {
103114
message: 'length must be less than or equal to 1024 characters long',
104115
transform: assocPath(['#e', 0], 'f'.repeat(1024 + 1)),
105116
},
106-
{ message: 'is not allowed to be empty', transform: assocPath(['#e', 0], '') },
107117
],
108118
'#p': [{ message: 'must be an array', transform: assocPath(['#p'], null) }],
109119
'#p[0]': [
110120
{
111121
message: 'length must be less than or equal to 1024 characters long',
112122
transform: assocPath(['#p', 0], 'f'.repeat(1024 + 1)),
113123
},
114-
{ message: 'is not allowed to be empty', transform: assocPath(['#p', 0], '') },
115124
],
116125
'#r': [{ message: 'must be an array', transform: assocPath(['#r'], null) }],
117126
'#r[0]': [
118127
{
119128
message: 'length must be less than or equal to 1024 characters long',
120129
transform: assocPath(['#r', 0], 'f'.repeat(1024 + 1)),
121130
},
122-
{ message: 'is not allowed to be empty', transform: assocPath(['#r', 0], '') },
123131
],
124132
}
125133

test/unit/utils/event.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ describe('NIP-01', () => {
258258

259259
describe('NIP-12', () => {
260260
let event: Event
261+
262+
describe('#d filter', () => {
263+
beforeEach(() => {
264+
event = {
265+
id: 'cf8de9db67a1d7203512d1d81e6190f5e53abfdc0ac90275f67172b65a5b09a0',
266+
pubkey: 'e8b487c079b0f67c695ae6c4c2552a47f38adfa2533cc5926bd2c102942fdcb7',
267+
created_at: 1645030752,
268+
kind: EventKinds.PARAMETERIZED_REPLACEABLE_FIRST,
269+
tags: [['d', '']],
270+
content: 'empty d tag',
271+
sig: '53d12018d036092794366283eca36df4e0cabd014b6e91bbf684c8bb9bbbe9dedafa77b6b928587e11e05e036227598dded8713e8da17d55076e12242b361542',
272+
}
273+
})
274+
275+
it('returns true if #d filter contains an empty tag value in the event', () => {
276+
expect(isEventMatchingFilter({ '#d': [''] })(event)).to.be.true
277+
})
278+
})
279+
261280
describe('#r filter', () => {
262281
beforeEach(() => {
263282
event = {

0 commit comments

Comments
 (0)