Skip to content

Commit 9ded969

Browse files
committed
Tidy up isEmpty check
1 parent 6d527ee commit 9ded969

File tree

2 files changed

+20
-26
lines changed

2 files changed

+20
-26
lines changed

src/PgConnectionArgFilterAttributesPlugin.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
PgConditionCapableParent,
44
} from "@dataplan/pg";
55
import type { GraphQLInputObjectType } from "graphql";
6+
import { isEmpty } from "./utils";
67

78
const { version } = require("../package.json");
89

@@ -73,13 +74,7 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
7374
description: `Filter by the object’s \`${fieldName}\` field.`,
7475
type: OperatorsType,
7576
apply: EXPORTABLE(
76-
(
77-
PgCondition,
78-
colSpec,
79-
connectionFilterAllowEmptyObjectInput,
80-
connectionFilterAllowNullInput
81-
) =>
82-
function (
77+
(PgCondition, colSpec, connectionFilterAllowEmptyObjectInput, connectionFilterAllowNullInput, isEmpty) => function (
8378
queryBuilder: PgConditionCapableParent,
8479
value: unknown
8580
) {
@@ -98,8 +93,7 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
9893
}
9994
if (
10095
!connectionFilterAllowEmptyObjectInput &&
101-
value != null &&
102-
Object.keys(value).length === 0
96+
isEmpty(value)
10397
) {
10498
throw Object.assign(
10599
new Error(
@@ -114,12 +108,7 @@ export const PgConnectionArgFilterAttributesPlugin: GraphileConfig.Plugin = {
114108
condition.extensions.pgFilterAttribute = colSpec;
115109
return condition;
116110
},
117-
[
118-
PgCondition,
119-
colSpec,
120-
connectionFilterAllowEmptyObjectInput,
121-
connectionFilterAllowNullInput,
122-
]
111+
[PgCondition, colSpec, connectionFilterAllowEmptyObjectInput, connectionFilterAllowNullInput, isEmpty]
123112
),
124113
})
125114
),

src/utils.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ export function makeAssertAllowed(build: GraphileBuild.Build) {
6262
connectionFilterAllowEmptyObjectInput,
6363
} = options;
6464
const assertAllowed = EXPORTABLE(
65-
(connectionFilterAllowEmptyObjectInput, connectionFilterAllowNullInput) =>
65+
(
66+
connectionFilterAllowEmptyObjectInput,
67+
connectionFilterAllowNullInput,
68+
isEmpty
69+
) =>
6670
function (value: unknown, mode: "list" | "object" | "scalar") {
6771
if (
6872
mode === "object" &&
6973
!connectionFilterAllowEmptyObjectInput &&
70-
typeof value === "object" &&
71-
value !== null &&
72-
Object.keys(value).length === 0
74+
isEmpty(value)
7375
) {
7476
throw Object.assign(
7577
new Error("Empty objects are forbidden in filter argument input."),
@@ -83,12 +85,7 @@ export function makeAssertAllowed(build: GraphileBuild.Build) {
8385
if (arr) {
8486
const l = arr.length;
8587
for (let i = 0; i < l; i++) {
86-
const entry = arr[i];
87-
if (
88-
typeof entry === "object" &&
89-
entry !== null &&
90-
Object.keys(entry).length === 0
91-
) {
88+
if (isEmpty(arr[i])) {
9289
throw Object.assign(
9390
new Error(
9491
"Empty objects are forbidden in filter argument input."
@@ -111,7 +108,15 @@ export function makeAssertAllowed(build: GraphileBuild.Build) {
111108
);
112109
}
113110
},
114-
[connectionFilterAllowEmptyObjectInput, connectionFilterAllowNullInput]
111+
[
112+
connectionFilterAllowEmptyObjectInput,
113+
connectionFilterAllowNullInput,
114+
isEmpty,
115+
]
115116
);
116117
return assertAllowed;
117118
}
119+
120+
export function isEmpty(o: unknown): o is Record<string, never> {
121+
return typeof o === "object" && o !== null && Object.keys(o).length === 0;
122+
}

0 commit comments

Comments
 (0)