-
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 base64binary type
- Loading branch information
Showing
3 changed files
with
62 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,21 @@ | ||
/** | ||
* Base64Binary FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
const BASE64_BINARY_REGEX = /(\s*([0-9a-zA-Z\+\=]){4}\s*)+/; | ||
|
||
export class Base64BinaryType extends Type<string> { | ||
readonly _tag: "Base64BinaryType" = "Base64BinaryType"; | ||
constructor() { | ||
super( | ||
"base64binary", | ||
(m): m is string => typeof m === "string" && BASE64_BINARY_REGEX.test(m), | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const base64binary = new Base64BinaryType(); |
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,38 @@ | ||
/** | ||
* Tests for Base64Binary Runtime Type | ||
*/ | ||
|
||
import { base64binary } from "../src"; | ||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
|
||
describe("Base64BinaryType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = base64binary; | ||
assertSuccess(T.decode("aGVsbG8gd29ybGQ=")); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = base64binary; | ||
const value = "aGVsbG8gd29ybGQ="; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = base64binary; | ||
assertFailure(T.decode(2), ["Invalid value 2 supplied to : base64binary"]); | ||
}); | ||
|
||
it("should fail validating a string with invalid characters", () => { | ||
const T = base64binary; | ||
assertFailure(T.decode("\0"), [ | ||
'Invalid value "\\u0000" supplied to : base64binary' | ||
]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = base64binary; | ||
expect(T.is("aGVsbG8gd29ybGQ=")).toEqual(true); | ||
expect(T.is(true)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |