Skip to content

Array field predicates in .where() are ignored (always match) #371

Description

@michael-wolfenden

Summary
When using a function predicate on a field whose schema type is z.array(...), the predicate appears to be ignored entirely — findMany returns all records regardless of whether the predicate function would return true or false. Predicates on non-array fields (e.g. strings) work correctly.

Reproduction

import { Collection } from '@msw/data';
import { z } from 'zod';

const users = new Collection({
  schema: z.object({
    id: z.number(),
    name: z.string(),
    petNames: z.array(z.string()),
  }),
});

await users.create({
  id: 1,
  name: 'user 1',
  petNames: ['wolfy'],
});

await users.create({
  id: 2,
  name: 'user 2',
  petNames: [],
});

// incorrect: returns both records, should return only user 1
users.findMany((q) => q.where({ petNames: (petNames) => petNames.length > 0 }));

// incorrect: returns both records, should return only user 1
users.findMany((q) =>
  q.where({ petNames: (petNames) => petNames.includes('wolfy') })
);

// correct: returns only user 2, as expected
console.log(
  users.findMany((q) => q.where({ name: (name) => name == 'user 2' }))
);

Expected behavior
The first two findMany calls should each return only the single matching record (user 1), since the predicate function evaluates to false for user 2 (empty array).

Actual behavior
Both queries return all records in the collection, as if the predicate on the petNames field is not being invoked/applied at all.

Environment

  • @msw/data: 1.1.7
  • Zod: 4.4.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions