Skip to content

Commit

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

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

// TODO: This regex seems incorrect as well
const DATE_TIME_REGEX = /([0-9]([0-9]([0-9][1-9]|[1-9]0)|[1-9]00)|[1-9]000)(-(0[1-9]|1[0-2])(-(0[1-9]|[1-2][0-9]|3[0-1])(T([01][0-9]|2[0-3]):[0-5][0-9]:([0-5][0-9]|60)(\.[0-9]+)?(Z|(\+|-)((0[0-9]|1[0-3]):[0-5][0-9]|14:00)))?)?)/;

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

export const dateTime = new DateTimeType();
3 changes: 3 additions & 0 deletions packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BooleanType, boolean } from "./boolean";
import { CanonicalType, canonical } from "./canonical";
import { CodeType, code } from "./code";
import { DateType, date } from "./date";
import { DateTimeType, dateTime } from "./dateTime";
import { DecimalType, decimal } from "./decimal";
import { IDType, id } from "./id";
import { InstantType, instant } from "./instant";
Expand All @@ -32,6 +33,8 @@ export {
CodeType,
date,
DateType,
dateTime,
DateTimeType,
decimal,
DecimalType,
id,
Expand Down
44 changes: 44 additions & 0 deletions packages/primitives/test/dateTime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Tests for DateTime Runtime Type
*/

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

describe("DateTimeType", () => {
it("should succeed validating a valid value", () => {
const T = dateTime;
assertSuccess(T.decode("2017-01-01T00:00:00.000Z"));
});

// TODO: Improve regex so this passes
xit("should succeed validating a partial dateTime", () => {
const T = dateTime;
assertSuccess(T.decode("2017"));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = dateTime;
const value = "2017-01-01T00:00:00.000Z";
assertStrictEqual(T.decode(value), value);
});

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

it("should fail validating non YYYY, YYYY-MM, YYYY-MM-DD or YYYY-MM-DDThh:mm:ss+zz:zz formatted date", () => {
const T = dateTime;
assertFailure(T.decode("08/23"), [
'Invalid value "08/23" supplied to : dateTime'
]);
});

it("should type guard", () => {
const T = dateTime;
expect(T.is("2017-01-01T00:00:00.000Z")).toEqual(true);
expect(T.is(true)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});

0 comments on commit 51e23d6

Please sign in to comment.