Skip to content

Commit

Permalink
feat(primitives): implement positiveInt type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent dda43ad commit b4e3714
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { InstantType, instant } from "./instant";
import { IntegerType, integer } from "./integer";
import { MarkdownType, markdown } from "./markdown";
import { OIDType, oid } from "./oid";
import { PositiveIntegerType, positiveInt } from "./positiveInt";
import { StringType, string } from "./string";
import { TimeType, time } from "./time";
import { UnsignedIntegerType, unsignedInt } from "./unsignedInt";
Expand Down Expand Up @@ -42,6 +43,8 @@ export {
MarkdownType,
oid,
OIDType,
positiveInt,
PositiveIntegerType,
string,
StringType,
time,
Expand Down
19 changes: 19 additions & 0 deletions packages/primitives/src/positiveInt.ts
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();
45 changes: 45 additions & 0 deletions packages/primitives/test/positiveInt.test.ts
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);
});
});

0 comments on commit b4e3714

Please sign in to comment.