Skip to content

Commit

Permalink
feat(primitives): implement markdown type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent a7cdab2 commit 4dbb278
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DecimalType, decimal } from "./decimal";
import { IDType, id } from "./id";
import { InstantType, instant } from "./instant";
import { IntegerType, integer } from "./integer";
import { MarkdownType, markdown } from "./markdown";
import { OIDType, oid } from "./oid";
import { StringType, string } from "./string";
import { TimeType, time } from "./time";
Expand All @@ -36,6 +37,8 @@ export {
InstantType,
integer,
IntegerType,
markdown,
MarkdownType,
oid,
OIDType,
string,
Expand Down
21 changes: 21 additions & 0 deletions packages/primitives/src/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Markdown FHIR Primitive Runtime Type
*/

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

const MARKDOWN_REGEX = /[^\s]+(\s[^\s]+)*/;

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

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

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

describe("MarkdownType", () => {
it("should succeed validating a valid value", () => {
const T = markdown;
assertSuccess(T.decode("# H1\n - bullet point"));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = markdown;
const value = "# H1\n - bullet point";
assertStrictEqual(T.decode(value), value);
});

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

// TODO: Improve unicode check in regex
xit("should fail validating a string with invalid characters", () => {
const T = markdown;
assertFailure(T.decode("\0"), [
'Invalid value "\\u0000" supplied to : markdown'
]);
});

it("should type guard", () => {
const T = markdown;
expect(T.is("# H1\n - bullet point")).toEqual(true);
expect(T.is(true)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});
4 changes: 3 additions & 1 deletion packages/primitives/test/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ describe("StringType", () => {
// TODO: Improve unicode check in regex
xit("should fail validating a string with invalid characters", () => {
const T = string;
assertFailure(T.decode("\0"), ["Invalid value 2 supplied to : string"]);
assertFailure(T.decode("\0"), [
'Invalid value "\\u0000" supplied to : string'
]);
});

it("should type guard", () => {
Expand Down

0 comments on commit 4dbb278

Please sign in to comment.