Skip to content

Update the LIVR test suite and fix the bugs it finds - #31

Merged
antonfin merged 2 commits into
masterfrom
update-test-suite
Oct 19, 2026
Merged

antonfin merged 2 commits into
masterfrom
update-test-suite

Conversation

@koorchik

@koorchik koorchik commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Updates tests/test_suite to the current LIVR test suite. As before, negative/22-not_empty_list and negative/27-any_object stay adjusted, as PHP decodes {} and [] to the same array.
  • Fixes the bugs that the new cases find, and a few more found with the tests of the JavaScript implementation that apply to PHP.

Fixes

Found with the test suite:

  • or skipped empty values, so required and not_empty in its rule sets never worked.
  • or took a list in its first argument as the list of rule sets, so { or: [['email', 'to_lc'], 'positive_integer'] }, the example from the LIVR docs, threw a TypeError.
  • list_of_different_objects and variable_object threw a TypeError on PHP 8 (a warning on PHP 7) when the selector value is an array.
  • list_of_different_objects dropped items that are not objects from the output instead of returning an error, and turned values that are not lists into empty lists.
  • equal_to_field compared values with ==, so "10" was equal to "1e1" and "0123" to "123", for example in a password confirmation. It also warned when the other field is missing.

Found with the tests of the JavaScript implementation:

  • Auto trim did not trim the values of lists.
  • registerDefaultRules() did not replace registered rules, though the README shows it for max_number.
  • An empty list of rules ('field' => [] or list_of: []) threw Rule [] not registered.

Behavior changes

  • or takes each argument as a set of rules, like the JavaScript and Perl implementations. Before, when the first argument was a list, PHP took it as the list of rule sets: { or: [['email', 'to_lc'], 'positive_integer'] } threw a TypeError, and { or: [['positive_integer', { max_number: 10 }]] } meant "positive_integer or max_number" instead of both. { or: ['email', 'positive_integer'] } works as before.
  • equal_to_field compares values as strings, so numeric strings in different notations are not equal anymore.
  • registerDefaultRules() replaces rules with the same names.

Testing

  • PHPUnit on PHP 7.2 and 8.5: 76 tests pass. New tests are in tests/05-rules_edge_cases.php and tests/03-auto_trim.php.
  • phpcs passes.
  • The LIVR conformance harness with the upstream test suite: 68 of 70 cases pass, 64 before. The two failures are the {} and [] cases above.
  • No warnings or deprecations while running the test suite on PHP 7.2 and 8.5.

Emails are still checked with filter_var(), so quoted local parts like "a@b"@example.com are accepted, unlike in the JavaScript implementation. The version is not bumped.

The test suite is copied from the LIVR repository. As before, the copy keeps
negative/22-not_empty_list and negative/27-any_object adjusted, as PHP decodes
{} and [] to the same array.

- or: pass empty values to the rule sets, so "required" and "not_empty" in
  them work
- or: do not unwrap a list in the first argument, as a set of rules can be a
  list: { or: [['email', 'to_lc'], 'positive_integer'] }
- list_of_different_objects, variable_object: return FORMAT_ERROR for array
  selector values instead of throwing a TypeError
- list_of_different_objects: return the errors when an item is not an object
  (it was dropped from the output), and FORMAT_ERROR when the value is not a
  list (it became an empty list)
- equal_to_field: compare values as strings, as "==" finds "10" and "1e1"
  equal, and do not warn when the other field is missing
Found with the tests of the JavaScript implementation that apply to PHP.

- Auto trim trims the values of lists too, not only of objects
- registerDefaultRules() replaces registered rules with the same names, as
  the README shows for max_number. "+" kept the registered rules.
- An empty list of rules ('field' => [] or list_of: []) is a list without
  rules, not a rule without a name ("Rule [] not registered")
@antonfin
antonfin merged commit 93e31c9 into master Oct 19, 2026
8 checks passed
koorchik added a commit that referenced this pull request Oct 23, 2026
The repository has no Packagist hook, so Packagist only picks up changes
on its weekly crawl: four days after #31 was merged, dev-master on
Packagist was still abbe57e. The workflow calls Packagist's
update-package API after every push to master and every tag. It needs the
PACKAGIST_USERNAME and PACKAGIST_TOKEN secrets of a maintainer of
validator/livr on packagist.org, and only warns while they're not set.
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.

2 participants