Skip to content

Commit

Permalink
fix(schema): Allow $in and $nin queries to work for arrays (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Nov 27, 2023
1 parent ff3e104 commit 677c214
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
22 changes: 14 additions & 8 deletions packages/schema/src/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,20 @@ export const queryProperty = <T extends JSONSchema, X extends { [key: string]: J
$lt: definition,
$lte: definition,
$ne: definition,
$in: {
type: 'array',
items: definition
},
$nin: {
type: 'array',
items: definition
},
$in:
definition.type === 'array'
? definition
: {
type: 'array',
items: definition
},
$nin:
definition.type === 'array'
? definition
: {
type: 'array',
items: definition
},
...extensions
}
}
Expand Down
29 changes: 28 additions & 1 deletion packages/schema/test/json-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,34 @@ describe('@feathersjs/schema/json-schema', () => {
}
}

const validator = new Ajv({ strict: false }).compile(schema)
const validator = new Ajv({ strict: false }).compile(querySchema)

assert.ok(validator(q))
})

it('$in and $nin works with array definitions', async () => {
const schema = {
things: {
type: 'array',
items: { type: 'number' }
}
} as const

const querySchema = {
type: 'object',
properties: querySyntax(schema)
} as const

type Query = FromSchema<typeof querySchema>

const q: Query = {
things: {
$in: [10, 20],
$nin: [30]
}
}

const validator = new Ajv({ strict: false }).compile(querySchema)

assert.ok(validator(q))
})
Expand Down
4 changes: 2 additions & 2 deletions packages/typebox/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ export const queryProperty = <T extends TSchema, X extends { [key: string]: TSch
$lt: def,
$lte: def,
$ne: def,
$in: Type.Array(def),
$nin: Type.Array(def)
$in: def.type === 'array' ? def : Type.Array(def),
$nin: def.type === 'array' ? def : Type.Array(def)
}),
Type.Object(extension)
],
Expand Down
21 changes: 21 additions & 0 deletions packages/typebox/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ describe('@feathersjs/schema/typebox', () => {
})
})

it('$in and $nin works with array type', async () => {
const schema = Type.Object({
things: Type.Array(Type.Number())
})
const querySchema = querySyntax(schema)
const validator = new Ajv().compile(querySchema)

type Query = Static<typeof querySchema>

const query: Query = {
things: {
$in: [10, 20],
$nin: [30]
}
}

const validated = (await validator(query)) as any as Query

assert.ok(validated)
})

it('defaultAppConfiguration', async () => {
const configSchema = Type.Intersect([
defaultAppConfiguration,
Expand Down

0 comments on commit 677c214

Please sign in to comment.