Skip to content

Commit

Permalink
size().isEqualTo() and isNotEqualTo() now include the array value…
Browse files Browse the repository at this point in the history
… in the exception context.
  • Loading branch information
cowwoc committed Nov 4, 2024
1 parent c013204 commit 543b47f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Minor updates involving cosmetic changes have been omitted from this list. See
https://github.com/cowwoc/requirements.java/commits/master for a full list.

## Version 4.0.6 - 2024/11/04

* Improvements
* `size().isEqualTo()` and `isNotEqualTo()` now include the array value in the exception context.

## Version 4.0.5 - 2024/10/31

* Breaking changes:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cowwoc/requirements",
"version": "4.0.5",
"version": "4.0.6",
"keywords": [
"preconditions",
"postconditions",
Expand Down
43 changes: 42 additions & 1 deletion src/internal/validator/ObjectSizeValidatorImpl.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
collectionContainsSize,
objectIsNotEmpty,
objectIsEmpty,
collectionSizeIsBetween
collectionSizeIsBetween,
Type
} from "../internal.mjs";
import {requireThatValueIsDefined} from "./Objects.mjs";

Expand Down Expand Up @@ -57,6 +58,46 @@ class ObjectSizeValidatorImpl extends AbstractValidator<number>
this.pluralizer = pluralizer;
}

public isEqualTo(expected: unknown): this;
public isEqualTo(expected: unknown, name?: string): this
{
const typeOfExpected = Type.of(expected);
if (typeOfExpected === Type.NUMBER)
{
if (name !== undefined)
this.requireThatNameIsUnique(name);

if (this.value.validationFailed(v => v === expected))
{
this.addRangeError(
collectionContainsSize(this.objectValidator, this.name, this.value.or(null), "must contain",
this.name, expected as number, this.pluralizer).toString());
}
return this;
}
return super.isEqualTo(expected);
}

public isNotEqualTo(unwanted: unknown): this;
public isNotEqualTo(unwanted: unknown, name?: string)
{
const typeOfExpected = Type.of(unwanted);
if (typeOfExpected === Type.NUMBER)
{
if (name !== undefined)
this.requireThatNameIsUnique(name);

if (this.value.validationFailed(v => v !== unwanted))
{
this.addRangeError(
collectionContainsSize(this.objectValidator, this.name, this.value.or(null), "may not contain",
this.name, unwanted as number, this.pluralizer).toString());
}
return this;
}
return super.isEqualTo(unwanted);
}

public isZero(): this
{
if (this.value.validationFailed(value => value == 0))
Expand Down
2 changes: 0 additions & 2 deletions test/ArrayTest.mts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import {TestApplicationScope} from "./TestApplicationScope.mjs";
const validators = new JavascriptValidatorsImpl(new TestApplicationScope(TerminalEncoding.NONE),
Configuration.DEFAULT);

/* eslint-disable max-statements */
suite("ArrayTest", () =>
{
/* eslint-enable max-statements */
test("isEmpty", () =>
{
const actual: unknown[] = [];
Expand Down

0 comments on commit 543b47f

Please sign in to comment.