-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(primitives): implement xhtml type
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
Showing
3 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |