Skip to content

Commit

Permalink
feat(primitives): implement url type
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdrew committed Jan 21, 2019
1 parent 5bc26ce commit 461f587
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/primitives/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DecimalType, decimal } from "./decimal";
import { IntegerType, integer } from "./integer";
import { StringType, string } from "./string";
import { URIType, uri } from "./uri";
import { URLType, url } from "./url";

export {
boolean,
Expand All @@ -18,5 +19,7 @@ export {
string,
StringType,
uri,
URIType
URIType,
url,
URLType
};
20 changes: 20 additions & 0 deletions packages/primitives/src/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* URL FHIR Primitive Runtime Type
*/

import { uri } from "./uri";
import { Type, success, failure, identity } from "io-ts";

export class URLType extends Type<string> {
readonly _tag: "URLType" = "URLType";
constructor() {
super(
"url",
uri.is,
(m, c) => (this.is(m) ? success(m) : failure(m, c)),
identity
);
}
}

export const url = new URLType();
32 changes: 32 additions & 0 deletions packages/primitives/test/url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Tests for URL Runtime Type
*/

import { assertSuccess, assertFailure, assertStrictEqual } from "./helpers";
import { url } from "../src";

describe("URLType", () => {
it("should succeed validating a valid value", () => {
const T = url;
const input = "http://snomed.info/sct";
assertSuccess(T.decode(input));
});

it("should return the same reference if validation succeeded and nothing changed", () => {
const T = url;
const value = "http://snomed.info/sct";
assertStrictEqual(T.decode(value), value);
});

it("should fail validating an invalid value", () => {
const T = url;
assertFailure(T.decode(2), ["Invalid value 2 supplied to : url"]);
});

it("should type guard", () => {
const T = url;
expect(T.is("http://snomed.info/sct")).toEqual(true);
expect(T.is(2)).toEqual(false);
expect(T.is(undefined)).toEqual(false);
});
});

0 comments on commit 461f587

Please sign in to comment.