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

fix(schema): Allow $in and $nin queries to work for arrays #3352

Merged
merged 1 commit into from
Nov 27, 2023
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
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
Loading