Allow plain query flags for boolean & geo filter operators (#18888)

* Allow plain query flags for boolean & geo filter operators

* Create blue-wolves-tell.md

---------

Co-authored-by: Brainslug <br41nslug@users.noreply.github.com>
This commit is contained in:
Pascal Jufer
2023-06-20 17:20:56 +02:00
committed by GitHub
parent 0fea5ab016
commit baceedf201
4 changed files with 42 additions and 7 deletions

View File

@@ -0,0 +1,5 @@
---
"@directus/api": patch
---
Added support for plain query flags for boolean & geo filter operators

View File

@@ -156,7 +156,7 @@ describe('applyFilter', () => {
}, operators);
for (const { filterOperator, sqlWhereClause } of withReverseOperators) {
for (const filterValue of [true, false]) {
for (const filterValue of [true, '', false]) {
test(`${filterOperator} with value ${filterValue}`, async () => {
class Client_SQLite3 extends MockClient {}
@@ -180,7 +180,7 @@ describe('applyFilter', () => {
const sql = tracker.history.select[0]?.sql.match(/select \* where \((.*)\)/)?.[1];
const expectedSql = sqlWhereClause[filterValue ? 'true' : 'false'].replaceAll(
const expectedSql = sqlWhereClause[filterValue === false ? 'false' : 'true'].replaceAll(
'$column',
`"${collection}"."${field}"`
);

View File

@@ -55,3 +55,34 @@ describe('export', async () => {
expect(() => validateQuery({ export: 'invalid-format' } as any)).toThrowError('"export" must be one of');
});
});
describe('validateBoolean', async () => {
const { validateBoolean } = await import('./validate-query.js');
test.each([true, '', null, false])('should allow value %s', (value: unknown) => {
expect(() => validateBoolean(value, 'test')).not.toThrowError();
});
test.each([undefined, 'wrong'])('should fail on value %s', (value: unknown) => {
expect(() => validateBoolean(value, 'test')).toThrowError('"test" has to be a boolean');
});
});
describe('validateGeometry', async () => {
const { validateGeometry } = await import('./validate-query.js');
test.each([
'',
null,
{
type: 'Point',
coordinates: [30.0, 10.0],
},
])('should allow value %s', (value: unknown) => {
expect(() => validateGeometry(value, 'test')).not.toThrowError();
});
test.each([undefined, 'wrong', {}])('should fail on value %s', (value: unknown) => {
expect(() => validateGeometry(value, 'test')).toThrowError('"test" has to be a valid GeoJSON object');
});
});

View File

@@ -67,7 +67,6 @@ function validateFilter(filter: Query['filter']) {
case '_nempty':
validateBoolean(value, key);
break;
case '_intersects':
case '_nintersects':
case '_intersects_bbox':
@@ -135,8 +134,8 @@ function validateList(value: any, key: string) {
return true;
}
function validateBoolean(value: any, key: string) {
if (value === null) return true;
export function validateBoolean(value: any, key: string) {
if (value === null || value === '') return true;
if (typeof value !== 'boolean') {
throw new InvalidQueryException(`"${key}" has to be a boolean`);
@@ -145,8 +144,8 @@ function validateBoolean(value: any, key: string) {
return true;
}
function validateGeometry(value: any, key: string) {
if (value === null) return true;
export function validateGeometry(value: any, key: string) {
if (value === null || value === '') return true;
try {
stringify(value);