Skip to content

Add the keysExist assertion - #364

Open
Amoifr wants to merge 1 commit into
webmozarts:masterfrom
Amoifr:feat-258-keys-exist
Open

Add the keysExist assertion#364
Amoifr wants to merge 1 commit into
webmozarts:masterfrom
Amoifr:feat-258-keys-exist

Conversation

@Amoifr

@Amoifr Amoifr commented Aug 31, 2026

Copy link
Copy Markdown

Closes #258, or at least the half of it that seems uncontroversial.

keyExists() only takes one key, so checking a set of them means a loop at the call site, which is the kind of loop this library exists to replace. keysExist() takes an iterable of keys and asserts that all of them are present.

Assert::keysExist($payload, ['name', 'email', 'password']);

It reports every missing key rather than stopping at the first one:

Expected the keys "email", "password" to exist.

That is deliberate. The use case in the issue is validating a request payload, where knowing the full list in one go is the point. A caller who wants to stop at the first miss still has keyExists().

The name is the plural of the existing keyExists(), and the argument order matches it.

The other half

@gordinskiy also asked for an "at least one of these keys" assertion, named oneOfKeysExists in the issue. I have left it out rather than pick a name on my own: the library already spells that idea two different ways, isInstanceOfAny() and isAnyOf(), and settling which one applies here is your call. Happy to add it in this pull request or a separate one once you say which shape you prefer.

Checks

The suite pointed me at three conventions I would otherwise have missed, and all are covered: src/Mixin.php regenerated with composer generate-mixin, tests/static-analysis/assert-keysExist.php written and its non-return counterpart generated, and the README table updated for testIsInReadme.

Suite green at 4949 tests, php-cs-fixer clean, psalm reports no errors. I could not run roave-bc-check locally, its lock file does not resolve on PHP 8.5, so that one is on CI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Check that multiple specific keys exist in an array

1 participant