Skip to content

Commit

Permalink
feat(or): add validator for logical or operator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 24, 2023
1 parent caee113 commit 63d8d2f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
4 changes: 3 additions & 1 deletion combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import { LessThenOrEqualValidator } from "./validators/operators/lte.ts";
import { GreaterThenValidator } from "./validators/operators/gt.ts";
import { GreaterThenOrEqualValidator } from "./validators/operators/gte.ts";
import { InequalityValidator } from "./validators/operators/inequality.ts";
import { TypeValidator } from "./validators/operators/typeof.ts";
import { InstanceValidator } from "./validators/operators/instanceof.ts";
import { OrValidator } from "./validators/operators/or.ts";
import { TypeValidator } from "./validators/operators/typeof.ts";
import { ValidDateValidator } from "./validators/date/valid_date.ts";

export const string = /* @__PURE__ */ new TypeValidator("string");
Expand All @@ -41,6 +42,7 @@ export const lte = /* @__PURE__ */ lazy(LessThenOrEqualValidator);
export const gt = /* @__PURE__ */ lazy(GreaterThenValidator);
export const gte = /* @__PURE__ */ lazy(GreaterThenOrEqualValidator);
export const ne = /* @__PURE__ */ lazy(InequalityValidator);
export const or = /* @__PURE__ */ lazy(OrValidator);
export const and = /* @__PURE__ */ lazy(AndValidator);

// known
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export {
number,
object,
optional,
or,
pattern,
positive,
property,
Expand Down
48 changes: 48 additions & 0 deletions validators/operators/or.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { isEmpty } from "../../deps.ts";
import { displayOr, shouldBe } from "../utils.ts";
import { Reporter, ValidationFailure, Validator } from "../../types.ts";
import { iter } from "../../iter_utils.ts";

export interface ReportContext<In = unknown> {
input: In;
}

export class OrValidator<in In = unknown, In_ extends In = In>
extends Reporter<ReportContext<In>>
implements Validator<In, In_> {
validators: [Validator<In, In_>, Validator<In, In_>, ...Validator<In, In_>[]];

constructor(
left: Validator<In, In_>,
right: Validator<In, In_>,
...validations: Validator<In, In_>[]
) {
super();
this.expect(shouldBe);
this.validators = [left, right, ...validations];
}

is(input: In): input is In_ {
return isEmpty(this.validate(input));
}

*validate(input: In): Iterable<ValidationFailure> {
for (const validator of this.validators) {
const iterable = validator.validate(input);

if (iter(iterable).done) return;
}

const context: ReportContext<In> = { input };
const message = this.report(context);

yield new ValidationFailure(message);
}

override toString(): string {
return displayOr(...this.validators);
}
}

0 comments on commit 63d8d2f

Please sign in to comment.