feathersjs / feathers

The API and real-time application framework
https://feathersjs.com
MIT License
15.04k stars 751 forks source link

JSON Schema querySyntax function code error for $in operator #3346

Closed ha6755ad closed 10 months ago

ha6755ad commented 10 months ago

If you have a schema property that is an array, and you run a query to search for a value $in the array, the querySyntax function does not allow for this. So, if I have a schema

... owners: { type: 'array', items: ObjectIdSchema() } ...

and then I write the query to search for records who's owners are among some list of ids like so:

owners: { $in: [some, list, of, ownerIds] }

This code $in: { type: 'array', items: definition },

from @feathersjs/schema/src/json-schema.js line 115 will throw an error if each item in my array is not an array - since the schema type of definition - being owners - is an array.

You can go around this by simply writing your query

owners: [some,list,of,ownerIds]

This works fine. It just departs from the allowed $in operations that mongo, for example, allows for. It creates some unnecessary complexity with programmatically writing queries using $in operator for different possible scenarios.

It's not an urgent bug, or necessarily even a bug, but I would think an anyOf allowing for the items of the $in querySchema validator to be either the definition or if the definition is an array, the type of the items from definition.

If not, it would help to have it be clearly documented. Where I discovered this was in a piece of code that has otherwise worked for a long time, and it was painful to find it because I didn't read it as an error even when I looked at my query language.