-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(primitives): implement positiveInt type
- Loading branch information
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Positive Integer FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
export class PositiveIntegerType extends Type<number> { | ||
readonly _tag: "PositiveIntegerType" = "PositiveIntegerType"; | ||
constructor() { | ||
super( | ||
"positiveInt", | ||
(m): m is number => typeof m === "number" && Number.isInteger(m) && m > 0, | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const positiveInt = new PositiveIntegerType(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Tests for Postive Integer Runtime Type | ||
*/ | ||
|
||
import { positiveInt } from "../src"; | ||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
|
||
describe("PositiveIntegerType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = positiveInt; | ||
assertSuccess(T.decode(2)); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = positiveInt; | ||
const value = 2; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating a float", () => { | ||
const T = positiveInt; | ||
assertFailure(T.decode(1.1), [ | ||
"Invalid value 1.1 supplied to : positiveInt" | ||
]); | ||
}); | ||
|
||
it("should fail validating a negative number", () => { | ||
const T = positiveInt; | ||
assertFailure(T.decode(-2), ["Invalid value -2 supplied to : positiveInt"]); | ||
}); | ||
|
||
it("should fail validating 0", () => { | ||
const T = positiveInt; | ||
assertFailure(T.decode(0), ["Invalid value 0 supplied to : positiveInt"]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = positiveInt; | ||
expect(T.is(2)).toEqual(true); | ||
expect(T.is(0)).toEqual(false); | ||
expect(T.is(1.1)).toEqual(false); | ||
expect(T.is(-2)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |