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 isObjectOf for non pure object predication #61

Merged
merged 3 commits into from
Feb 16, 2024
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
19 changes: 3 additions & 16 deletions is/factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FlatType } from "../_typeutil.ts";
import type { Predicate, PredicateType } from "./type.ts";
import { isOptional, isOptionalOf, isReadonlyOf } from "./annotation.ts";
import { isOptionalOf, isReadonlyOf } from "./annotation.ts";
import {
isAny,
isArray,
Expand Down Expand Up @@ -506,18 +506,12 @@ export function isObjectOf<
// deno-lint-ignore no-explicit-any
return isStrictOf(isObjectOf(predObj)) as any;
}
const requiredKeys = Object.entries(predObj)
.filter(([_, v]) => !isWithOptional(v))
.map(([k]) => k);
return setPredicateFactoryMetadata(
(x: unknown): x is ObjectOf<T> => {
if (!isRecord(x)) return false;
// Check required keys
const s = new Set(Object.keys(x));
if (requiredKeys.some((k) => !s.has(k))) return false;
if (x == null || typeof x !== "object") return false;
// Check each values
for (const k in predObj) {
if (!predObj[k](x[k])) return false;
if (!predObj[k]((x as T)[k])) return false;
Copy link

@coderabbitai coderabbitai bot Feb 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loop for (const k in predObj) iterates over the keys in predObj and uses the predicates to check the corresponding properties in x. However, there's a potential issue with properties that are optional in predObj. If predObj specifies a property as optional using is.OptionalOf(), and that property is not present in x, this check will incorrectly return false.

To address this, you should verify whether each property is optional and, if so, only perform the check if the property exists in x.

-        if (!predObj[k]((x as T)[k])) return false;
+        const predicate = predObj[k];
+        if (isWithOptional(predicate) && !(k in x)) continue;
+        if (!predicate((x as T)[k])) return false;

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (!predObj[k]((x as T)[k])) return false;
const predicate = predObj[k];
if (isWithOptional(predicate) && !(k in x)) continue;
if (!predicate((x as T)[k])) return false;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. In that case, predObj[k] return a predicate function that return true on undefined.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lambdalisue: Understood, thank you for clarifying how the predicate functions are designed to handle undefined values for optional properties. This approach elegantly ensures that the implementation correctly supports optional properties without needing additional checks.

}
return true;
},
Expand All @@ -529,13 +523,6 @@ type WithOptional =
| WithMetadata<GetMetadata<ReturnType<typeof isOptionalOf>>>
| { optional: true }; // For backward compatibility

function isWithOptional<T extends Predicate<unknown>>(
pred: T,
): pred is T & WithOptional {
// deno-lint-ignore no-explicit-any
return isOptional(pred) || (pred as any).optional === true;
}

type ObjectOf<T extends Record<PropertyKey, Predicate<unknown>>> = FlatType<
// Non optional
& {
Expand Down
16 changes: 15 additions & 1 deletion is/factory_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { assertType } from "https://deno.land/[email protected]/testing/types.ts";
import { type Equal, stringify } from "./_testutil.ts";
import { type Predicate } from "./type.ts";
import { isOptionalOf } from "./annotation.ts";
import { isArray, isBoolean, isNumber, isString, isUndefined } from "./core.ts";
import {
isArray,
isBoolean,
isFunction,
isNumber,
isString,
isUndefined,
} from "./core.ts";
import is, {
isArrayOf,
isInstanceOf,
Expand Down Expand Up @@ -612,6 +619,13 @@ Deno.test("isObjectOf<T>", async (t) => {
"Specify `{ strict: true }` and object have an unknown property",
);
});
await t.step("returns true on T instance", () => {
const date = new Date();
const predObj = {
getFullYear: isFunction,
};
assertEquals(isObjectOf(predObj)(date), true, "Value is not an object");
});
await testWithExamples(
t,
isObjectOf({ a: (_: unknown): _ is unknown => false }),
Expand Down
Loading