Skip to content

Commit

Permalink
🐛 Do NOT check required keys in isObjectOf
Browse files Browse the repository at this point in the history
Some instance attributes are non enumerable, so we should not check.
  • Loading branch information
lambdalisue committed Feb 16, 2024
1 parent b01d673 commit 14a83f7
Showing 1 changed file with 2 additions and 15 deletions.
17 changes: 2 additions & 15 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 (x == null || typeof x !== "object") return false;
// Check required keys
const s = new Set(Object.keys(x));
if (requiredKeys.some((k) => !s.has(k))) 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;
}
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

0 comments on commit 14a83f7

Please sign in to comment.