Skip to content

Commit

Permalink
feat(validators): add validator for comparision
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 20, 2023
1 parent 9304fac commit 548e6ee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
10 changes: 5 additions & 5 deletions validators/value.ts → validators/equal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

import { shouldBeBut } from "./utils.ts";
import { ScalarValidator } from "../utils.ts";
import type { Assert } from "../types.ts";
import type { Assert, AssertiveValidator } from "../types.ts";

export class ValueValidator<Out = unknown> extends ScalarValidator
implements Assert<unknown, Out> {
declare [Assert.symbol]: Out;
constructor(public value: Out) {
export class EqualValidator<const In_ = unknown> extends ScalarValidator
implements AssertiveValidator<unknown, In_> {
declare [Assert.symbol]: In_;
constructor(public value: In_) {
super();
super.expect(shouldBeBut);
}
Expand Down
20 changes: 20 additions & 0 deletions validators/greater.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 GreaterValidator<In> extends ScalarValidator<In> {
constructor(public base: In) {
super();
super.expect(shouldBeBut);
}

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

toString(): string {
return `greater then ${this.base}`;
}
}
20 changes: 20 additions & 0 deletions validators/less.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 LessValidator<In> extends ScalarValidator<In> {
constructor(public base: In) {
super();
super.expect(shouldBeBut);
}

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

toString(): string {
return `less then ${this.base}`;
}
}

0 comments on commit 548e6ee

Please sign in to comment.