Skip to content

Commit

Permalink
fix: string as number
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Dec 27, 2022
1 parent b7b9ac9 commit 1f7a021
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
18 changes: 0 additions & 18 deletions packages/input/src/Type/Numeric.ts

This file was deleted.

10 changes: 10 additions & 0 deletions packages/input/src/Type/StirngAsNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {isNumber, isString, MayNullable, Promisify} from "@bunt/util";
import {SuperType} from "../SuperType";

export class StringAsNumber<TValue> extends SuperType<MayNullable<TValue>, Exclude<TValue, undefined | null>> {
public validate(payload: unknown): Promisify<TValue | undefined> {
this.assert(isString(payload) || isNumber(payload), "Wrong payload type", payload);

return this.type.validate(+payload);
}
}
1 change: 1 addition & 0 deletions packages/input/src/Type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ export * from "./Enum";
export * from "./EmailAddress";
export * from "./ScalarType";
export * from "./Mutate";
export * from "./StirngAsNumber";
3 changes: 3 additions & 0 deletions packages/input/test/src/Main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Varchar,
RecordType,
FieldSelectType,
StringAsNumber,
} from "../../src";
import {ITestDescription, ITestHobby, ITestType} from "./interfaces";
import {TestEnum, TestEnumType} from "./Type/TestEnum";
Expand All @@ -39,6 +40,8 @@ describe("Test Input", () => {
const samples: [any, any, TypeAbstract<any>][] = [
[1, 1, Int],
["1", 1, ToNumber],
["2", 2, new StringAsNumber(Int)],
["1.123", 1.123, new StringAsNumber(Float)],
[false, false, Bool],
[true, true, Bool],
[rand, rand, Float],
Expand Down

0 comments on commit 1f7a021

Please sign in to comment.