Skip to content

Commit

Permalink
feat(positive_number): add validator for positive number
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed May 18, 2023
1 parent 62ea00a commit 88ab3f5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions validators/number/positive_number.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.

import { shouldBeBut } from "../utils.ts";
import { display, ScalarValidator } from "../../utils.ts";
import { isPositiveNumber } from "../../deps.ts";

@display("positive number")
export class PositiveNumberValidator extends ScalarValidator<number> {
constructor() {
super();
super.expect(shouldBeBut);
}

is = isPositiveNumber;
}
20 changes: 20 additions & 0 deletions validators/utils.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 { format } from "../utils.ts";
import error from "./error.json" assert { type: "json" };
import type { Display } from "../types.ts";

export function displayOr(input: readonly unknown[]): string {
const head = input.slice(0, -1);
const last = input.slice(-1)[0];

return head.join(", ") + " or " + last;
}

export function shouldBeBut(
this: Display,
{ input }: { input: unknown },
): string {
return format(error.should_be_but, this, input);
}

0 comments on commit 88ab3f5

Please sign in to comment.