Skip to content

Commit

Permalink
feat(element): add validator for element
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 20, 2023
1 parent 294b5c6 commit bdde405
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions validators/iterable/element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { fromPath, partialR } from "../../utils.ts";
import { entries, map } from "../../iter_utils.ts";
import {
Assert,
AssertiveValidator,
Validation,
ValidationError,
} from "../../types.ts";

export class ElementValidator<In = unknown, In_ extends In = In>
implements AssertiveValidator<Iterable<In>, Iterable<In_>> {
declare [Assert.symbol]: Iterable<In_>;
constructor(public readonly validator: Validation<In, In_>) {}

*validate(input: Iterable<In>): Iterable<ValidationError> {
for (const [i, el] of entries(input)) {
const iterable = this.validator.validate(el);
const createError = partialR(fromPath, `${i}`);

yield* map(iterable, createError);
}
}
}

0 comments on commit bdde405

Please sign in to comment.