Skip to content

Commit

Permalink
feat(lte): add validator for less than or equal operator
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 24, 2023
1 parent 629919b commit cb247f8
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions combinator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { NonEmptyValidator } from "./validators/iterable/non_empty.ts";
import { AndValidator } from "./validators/operators/and.ts";
import { EqualityValidator } from "./validators/operators/eq.ts";
import { LessThenValidator } from "./validators/operators/lt.ts";
import { LessThenOrEqualValidator } from "./validators/operators/lte.ts";
import { GreaterThenValidator } from "./validators/operators/gt.ts";
import { GreaterThenOrEqualValidator } from "./validators/operators/gte.ts";
import { TypeValidator } from "./validators/operators/typeof.ts";
Expand All @@ -35,6 +36,7 @@ export const optional = /* @__PURE__ */ lazy(OptionalValidator);
export const nullish = /* @__PURE__ */ new NullishValidator();
export const eq = /* @__PURE__ */ lazy(EqualityValidator);
export const lt = /* @__PURE__ */ lazy(LessThenValidator);
export const lte = /* @__PURE__ */ lazy(LessThenOrEqualValidator);
export const gt = /* @__PURE__ */ lazy(GreaterThenValidator);
export const gte = /* @__PURE__ */ lazy(GreaterThenOrEqualValidator);
export const and = /* @__PURE__ */ lazy(AndValidator);
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export {
int,
item,
lt,
lte,
maxCount,
minCount,
nonEmpty,
Expand Down
20 changes: 20 additions & 0 deletions validators/operators/lte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { ScalarValidator } from "../../utils.ts";
import { shouldBeBut } from "../utils.ts";

export class LessThenOrEqualValidator<In> extends ScalarValidator<In> {
constructor(public base: In) {
super();
super.expect(shouldBeBut);
}

override is(input: In): input is In {
return input <= this.base;
}

override toString(): string {
return `<= ${this.base}`;
}
}

0 comments on commit cb247f8

Please sign in to comment.