Skip to content

Commit

Permalink
fix: the numeric input type added
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed May 11, 2021
1 parent c42ce1d commit 758e07f
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/input/src/Type/Numeric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {isNumber, isString} from "@bunt/util";
import {ScalarType} from "./ScalarType";

export const Numeric = new ScalarType<number, string | number>({
name: "Numeric",
validate(payload) {
this.assert(isNumber(payload) || isString(payload), `Wrong payload: ${this.name} expected`, payload);
const value = isNumber(payload) ? payload : parseInt(payload, 10);

this.assert(
!isNaN(value) && Number.isSafeInteger(value),
`Wrong payload: ${this.name} expected`,
payload,
);

return value;
},
});

0 comments on commit 758e07f

Please sign in to comment.