-
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 code type
- Loading branch information
Showing
3 changed files
with
64 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,22 @@ | ||
/** | ||
* Code FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
// TODO: This regex seems incorrect as well | ||
const CODE_REGEX = /[^\s]+(\s[^\s]+)*/; | ||
|
||
export class CodeType extends Type<string> { | ||
readonly _tag: "CodeType" = "CodeType"; | ||
constructor() { | ||
super( | ||
"code", | ||
(m): m is string => typeof m === "string" && CODE_REGEX.test(m), | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const code = new CodeType(); |
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,39 @@ | ||
/** | ||
* Tests for Code Runtime Type | ||
*/ | ||
|
||
import { code } from "../src"; | ||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
|
||
describe("CodeType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = code; | ||
assertSuccess(T.decode("55423-8")); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = code; | ||
const value = "55423-8"; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = code; | ||
assertFailure(T.decode(2), ["Invalid value 2 supplied to : code"]); | ||
}); | ||
|
||
// TODO: Improve regex so this fails | ||
xit("should fail validating a string with invalid characters", () => { | ||
const T = code; | ||
assertFailure(T.decode("invalid\tcode"), [ | ||
'Invalid value "invalid\tcode" supplied to : code' | ||
]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = code; | ||
expect(T.is("55423-8")).toEqual(true); | ||
expect(T.is(true)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |