Skip to content

Commit

Permalink
feat(primitives): implement id type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent 72bc500 commit a7cdab2
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/primitives/src/id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* ID FHIR Primitive Runtime Type
*/

import { Type, success, failure, identity } from "io-ts";

// TODO: This regex seems incorrect as well
const ID_REGEX = /[A-Za-z0-9\-\.]{1,64}/;

export class IDType extends Type<string> {
readonly _tag: "IDType" = "IDType";
constructor() {
super(
"id",
(m): m is string => typeof m === "string" && ID_REGEX.test(m),
(m, c) => (this.is(m) ? success(m) : failure(m, c)),
identity
);
}
}

export const id = new IDType();
3 changes: 3 additions & 0 deletions packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CanonicalType, canonical } from "./canonical";
import { CodeType, code } from "./code";
import { DateType, date } from "./date";
import { DecimalType, decimal } from "./decimal";
import { IDType, id } from "./id";
import { InstantType, instant } from "./instant";
import { IntegerType, integer } from "./integer";
import { OIDType, oid } from "./oid";
Expand All @@ -29,6 +30,8 @@ export {
DateType,
decimal,
DecimalType,
id,
IDType,
instant,
InstantType,
integer,
Expand Down
53 changes: 53 additions & 0 deletions packages/primitives/test/id.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Tests for ID Runtime Type
*/

import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers";
import { id } from "../src";

describe("IDType", () => {
it("should succeed validating a valid value", () => {
const T = id;
const input = "5e8c5a39-ac09-4a1e-9769-c61192b4355d";
assertSuccess(T.decode(input));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = id;
const value = "5e8c5a39-ac09-4a1e-9769-c61192b4355d";
assertStrictEqual(T.decode(value), value);
});

it("should fail validating an invalid value", () => {
const T = id;
assertFailure(T.decode(2), ["Invalid value 2 supplied to : id"]);
});

// TODO: Improve regex so this fails
xit("should fail validating a string that doesn't match regex", () => {
const T = id;
assertFailure(T.decode("invalid{}"), [
'Invalid value "invalid{}" supplied to : id'
]);
});

// TODO: Improve regex so this fails
xit("should fail validating a string that is greater than 64 characters", () => {
const T = id;
assertFailure(
T.decode(
"5e8c5a39-ac09-4a1e-9769-c61192b4355d5e8c5a39-ac09-4a1e-9769-c61192b4355d"
),
[
'Invalid value "5e8c5a39-ac09-4a1e-9769-c61192b4355d5e8c5a39-ac09-4a1e-9769-c61192b4355d" supplied to : id'
]
);
});

it("should type guard", () => {
const T = id;
expect(T.is("5e8c5a39-ac09-4a1e-9769-c61192b4355d")).toEqual(true);
expect(T.is(2)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});

0 comments on commit a7cdab2

Please sign in to comment.