Skip to content

Commit

Permalink
feat(validation): rename field to maxFailures
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 31, 2023
1 parent 2f85524 commit 228bdf4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ if (result.isOk()) {

By default, validate collects as many errors as possible.

### maxErrors
### maxFailures

The maximum number of `ValidationFailure`. It should be positive integer.

Expand All @@ -161,7 +161,7 @@ import type {
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const Tuple = fixedArray(string, number);
const result = validate(Tuple, [0, ""], { maxErrors: 1 });
const result = validate(Tuple, [0, ""], { maxFailures: 1 });
declare const failure: ValidationFailure;

if (result.isErr()) {
Expand All @@ -176,7 +176,7 @@ improves performance.

Throws an error in the following cases:

- `RangeError`: If [maxErrors](#maxerrors) is not positive integer.
- `RangeError`: If [maxFailures](#maxfailures) is not positive integer.

## assert

Expand Down Expand Up @@ -311,10 +311,10 @@ try {

The following fields can only be specified in greedy mode.

### maxErrors option
### maxFailures option

The number of validation errors can be changed with `maxErrors`. For details,
see [maxErrors](#maxerrors)
The number of validation failures can be changed with `maxFailures`. For
details, see [maxFailures](#maxfailures)

### aggregation

Expand Down
16 changes: 8 additions & 8 deletions validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export interface GreedyAssertOptions extends AssertOptions, ValidateOptions {
*
* @throws {AggregateError} If assertion is fail.
* @throws {ValidationError} If assertion is fail and {@link options.failFast} is true.
* @throws {RangeError} If options.maxErrors is not positive integer.
* @throws {RangeError} If options.maxFailures is not positive integer.
*/
export function assert<In = unknown, A extends In = In>(
validator: Readonly<Validator<In, A>>,
Expand All @@ -76,8 +76,8 @@ export function assert<In = unknown, A extends In = In>(
failFast,
validation = {},
} = options;
const maxErrors = failFast ? 1 : options.maxErrors;
const result = validate(validator, input, { maxErrors });
const maxFailures = failFast ? 1 : options.maxFailures;
const result = validate(validator, input, { maxFailures });

if (result.isOk()) return;

Expand Down Expand Up @@ -122,7 +122,7 @@ export interface ValidateOptions {
* It should be positive integer.
* @default Number.MAX_SAFE_INTEGER
*/
maxErrors?: number;
maxFailures?: number;
}

/** The `validate` executes the {@link Validator} and returns a {@link Result} type. If validation
Expand Down Expand Up @@ -165,7 +165,7 @@ export interface ValidateOptions {
*
* ```
*
* ## maxErrors
* ## maxFailures
* The maximum number of {@link ValidationFailure}. It should be positive integer.
* The default is `Number.MAX_SAFE_INTEGER`.
*
Expand All @@ -187,7 +187,7 @@ export interface ValidateOptions {
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
*
* const Tuple = fixedArray(string, number);
* const result = validate(Tuple, [0, ""], { maxErrors: 1 });
* const result = validate(Tuple, [0, ""], { maxFailures: 1 });
* declare const failure: ValidationFailure;
*
* if (result.isErr()) {
Expand All @@ -198,14 +198,14 @@ export interface ValidateOptions {
* Because the validator performs lazy evaluation, limiting the number of errors
* improves performance.
*
* @throws {RangeError} If the {@link ValidateOptions.maxErrors} is not positive integer.
* @throws {RangeError} If the {@link ValidateOptions.maxFailures} is not positive integer.
*/
export function validate<In = unknown, A extends In = In>(
validator: Readonly<Validator<In, A>>,
input: In,
options: Readonly<ValidateOptions> = {},
): Result<A, [ValidationFailure, ...ValidationFailure[]]> {
const failures = [...take(validator.validate(input), options.maxErrors)];
const failures = [...take(validator.validate(input), options.maxFailures)];

if (isNotEmpty(failures)) return new Err(failures);

Expand Down
10 changes: 5 additions & 5 deletions validation_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("assert", () => {
it("should return void if there are not validation errors", () => {
assertFalse(assert(v, ""));
assertFalse(assert(v, "", { failFast: true }));
assertFalse(assert(v, "", { maxErrors: 1 }));
assertFalse(assert(v, "", { maxFailures: 1 }));
});

it("should throw AggregateError by default", () => {
Expand Down Expand Up @@ -105,7 +105,7 @@ describe("assert", () => {
let err;

try {
assertFalse(assert(v1, "", { maxErrors: 1 }));
assertFalse(assert(v1, "", { maxFailures: 1 }));
} catch (e) {
err = e;
} finally {
Expand Down Expand Up @@ -219,14 +219,14 @@ describe("validate", () => {
],
},
"",
{ maxErrors: 1 },
{ maxFailures: 1 },
),
new Err<[ValidationFailure]>([{ message: "error", instancePath: [] }]),
);
});

it("should throw error is maxError is not positive integer", () => {
assertThrows(() => validate(v, "", { maxErrors: 0 }));
it("should throw error is maxFailures is not positive integer", () => {
assertThrows(() => validate(v, "", { maxFailures: 0 }));
});
});

Expand Down

0 comments on commit 228bdf4

Please sign in to comment.