-
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 canonical type
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Canonical URL FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { uri } from "./uri"; | ||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
export class CanonicalType extends Type<string> { | ||
readonly _tag: "CanonicalType" = "CanonicalType"; | ||
constructor() { | ||
super( | ||
"canonical", | ||
uri.is, | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const canonical = new CanonicalType(); |
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,32 @@ | ||
/** | ||
* Tests for Canonical URL Runtime Type | ||
*/ | ||
|
||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
import { canonical } from "../src"; | ||
|
||
describe("CanonicalType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = canonical; | ||
const input = "http://snomed.info/sct"; | ||
assertSuccess(T.decode(input)); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = canonical; | ||
const value = "http://snomed.info/sct"; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = canonical; | ||
assertFailure(T.decode(2), ["Invalid value 2 supplied to : canonical"]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = canonical; | ||
expect(T.is("http://snomed.info/sct")).toEqual(true); | ||
expect(T.is(2)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |