-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5df543
commit 9d0b8e3
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { iter } from "../iter_utils.ts"; | ||
import { | ||
Assert, | ||
AssertiveValidator, | ||
Validation, | ||
ValidationError, | ||
} from "../types.ts"; | ||
|
||
export class AndValidator<In, In_ extends Via, Via extends In = In & In_> | ||
implements AssertiveValidator<In, In & In_ & Via> { | ||
declare [Assert.symbol]: In & In_ & Via; | ||
validators: Validation[] = []; | ||
|
||
constructor(left: Validation<In, Via>, right: Validation<Via, In_>); | ||
constructor(...validators: Validation[]) { | ||
this.validators = validators; | ||
} | ||
|
||
*validate(input: In): Iterable<ValidationError> { | ||
for (const validator of this.validators) { | ||
const iterable = validator.validate(input); | ||
const result = iter(iterable); | ||
|
||
if (result.done) continue; | ||
|
||
yield result.value; | ||
yield* iterable; | ||
return; | ||
} | ||
} | ||
} |