-
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 string type
- Loading branch information
Showing
6 changed files
with
75 additions
and
11 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
lerna-debug.log | ||
node_modules | ||
coverage |
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
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 @@ | ||
/** | ||
* String FHIR Primitive Runtime Type | ||
*/ | ||
|
||
import { Type, success, failure, identity } from "io-ts"; | ||
|
||
const STRING_REGEX = /[ \r\n\t\S]+/; | ||
|
||
export class StringType extends Type<string> { | ||
readonly _tag: "StringType" = "StringType"; | ||
constructor() { | ||
super( | ||
"string", | ||
(m): m is string => typeof m === "string" && STRING_REGEX.test(m), | ||
(m, c) => (this.is(m) ? success(m) : failure(m, c)), | ||
identity | ||
); | ||
} | ||
} | ||
|
||
export const string = new StringType(); |
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,37 @@ | ||
/** | ||
* Tests for String Runtime Type | ||
*/ | ||
|
||
import { string } from "../src"; | ||
import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers"; | ||
|
||
describe("StringType", () => { | ||
it("should succeed validating a valid value", () => { | ||
const T = string; | ||
assertSuccess(T.decode("abc")); | ||
}); | ||
|
||
it("should return the same reference if validation succeeded and nothing changed", () => { | ||
const T = string; | ||
const value = "abc"; | ||
assertStrictEqual(T.decode(value), value); | ||
}); | ||
|
||
it("should fail validating an invalid value", () => { | ||
const T = string; | ||
assertFailure(T.decode(2), ["Invalid value 2 supplied to : string"]); | ||
}); | ||
|
||
// TODO: Improve unicode check in regex | ||
xit("should fail validating a string with invalid characters", () => { | ||
const T = string; | ||
assertFailure(T.decode("\0"), ["Invalid value 2 supplied to : string"]); | ||
}); | ||
|
||
it("should type guard", () => { | ||
const T = string; | ||
expect(T.is("abc")).toEqual(true); | ||
expect(T.is(true)).toEqual(false); | ||
expect(T.is(undefined)).toEqual(false); | ||
}); | ||
}); |
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