-
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 oid 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
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 @@ | ||
/** | ||
* OID FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { uri } from "./uri"; | ||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
const OID_REGEX = /urn:oid:[0-2](\.(0|[1-9][0-9]*))+/; | ||
|
||
export class OIDType extends Type<string> { | ||
readonly _tag: "OIDType" = "OIDType"; | ||
constructor() { | ||
super( | ||
"oid", | ||
(m): m is string => uri.is(m) && OID_REGEX.test(m), | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const oid = new OIDType(); |
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 OID Runtime Type | ||
*/ | ||
|
||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
import { oid } from "../src"; | ||
|
||
describe("OIDType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = oid; | ||
const input = "urn:oid:1.2.3.4.5"; | ||
assertSuccess(T.decode(input)); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = oid; | ||
const value = "urn:oid:1.2.3.4.5"; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = oid; | ||
assertFailure(T.decode(2), ["Invalid value 2 supplied to : oid"]); | ||
}); | ||
|
||
it("should fail validating a string that doesn't match regex", () => { | ||
const T = oid; | ||
assertFailure(T.decode("https://www.hl7.org/fhir/datatypes.html"), [ | ||
'Invalid value "https://www.hl7.org/fhir/datatypes.html" supplied to : oid' | ||
]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = oid; | ||
expect(T.is("urn:oid:1.2.3.4.5")).toEqual(true); | ||
expect(T.is(2)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |