Skip to content

Commit

Permalink
feat(primitives): implement xhtml type
Browse files Browse the repository at this point in the history
Add xhtml as a primitive type, even though it's not listed as one
because it's referenced as one by the Narrative type.
  • Loading branch information
tangdrew committed Feb 21, 2019
1 parent 4a869f3 commit 67b5a29
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/primitives/src/R4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { Base64BinaryType, base64Binary } from "./base64Binary";
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 { DateType, date } from "./date";
import { DecimalType, decimal } from "./decimal";
import { IDType, id } from "./id";
import { InstantType, instant } from "./instant";
Expand All @@ -21,6 +21,7 @@ import { UnsignedIntegerType, unsignedInt } from "./unsignedInt";
import { URIType, uri } from "./uri";
import { URLType, url } from "./url";
import { UUIDType, uuid } from "./uuid";
import { XHTMLType, xhtml } from "./xhtml";

export {
base64Binary,
Expand Down Expand Up @@ -60,5 +61,7 @@ export {
url,
URLType,
uuid,
UUIDType
UUIDType,
xhtml,
XHTMLType
};
24 changes: 24 additions & 0 deletions packages/primitives/src/R4/xhtml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* XHTML FHIR Primitive Runtime Type
*/

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

const STRING_REGEX = /[ \r\n\t\S]+/;

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

/**
* A XHTML fragment.
*/
export const xhtml = new XHTMLType();
39 changes: 39 additions & 0 deletions packages/primitives/test/R4/xhtml.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Tests for XHTML Runtime Type
*/

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

describe("XHTMLType", () => {
it("should succeed validating a valid value", () => {
const T = xhtml;
assertSuccess(T.decode("<div>Hello World!</div>"));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = xhtml;
const value = "<div>Hello World!</div>";
assertStrictEqual(T.decode(value), value);
});

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

// TODO: Do actual xhtml validation
xit("should fail validating a string that is not valid xhtml", () => {
const T = xhtml;
assertFailure(T.decode("Invalid xhtml"), [
'Invalid value "Invalid xhtml" supplied to : xhtml'
]);
});

it("should type guard", () => {
const T = xhtml;
expect(T.is("<div>Hello World!</div>")).toEqual(true);
expect(T.is(true)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});

0 comments on commit 67b5a29

Please sign in to comment.