Skip to content

Commit a94ebac

Browse files
chore: add expectation utilities for parsing
This adds a few methods to assert that a type is the expected type before returning it. This is useful to make sure that we aren't blindly returning json that is invalid.
1 parent 608e606 commit a94ebac

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

packages/smithy-client/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export * from "./extended-encode-uri-component";
66
export * from "./get-array-if-single-item";
77
export * from "./get-value-from-text-node";
88
export * from "./lazy-json";
9+
export * from "./parse-utils";
910
export * from "./date-utils";
1011
export * from "./split-every";
1112
export * from "./constants";
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { expectBoolean, expectNumber, expectString } from "./parse-utils";
2+
3+
describe("expectBoolean", () => {
4+
it("accepts booleans", () => {
5+
expect(expectBoolean(true)).toEqual(true);
6+
expect(expectBoolean(false)).toEqual(false);
7+
});
8+
9+
it("rejects non-booleans", () => {
10+
expect(() => expectBoolean("true")).toThrowError();
11+
expect(() => expectBoolean("false")).toThrowError();
12+
expect(() => expectBoolean(0)).toThrowError();
13+
expect(() => expectBoolean(1)).toThrowError();
14+
expect(() => expectBoolean(1.1)).toThrowError();
15+
expect(() => expectBoolean(Infinity)).toThrowError();
16+
expect(() => expectBoolean(-Infinity)).toThrowError();
17+
expect(() => expectBoolean(NaN)).toThrowError();
18+
expect(() => expectBoolean({})).toThrowError();
19+
expect(() => expectBoolean([])).toThrowError();
20+
expect(() => expectBoolean(null)).toThrowError();
21+
expect(() => expectBoolean(undefined)).toThrowError();
22+
});
23+
});
24+
25+
describe("expectNumber", () => {
26+
it("accepts numbers", () => {
27+
expect(expectNumber(1)).toEqual(1);
28+
expect(expectNumber(1.1)).toEqual(1.1);
29+
expect(expectNumber(Infinity)).toEqual(Infinity);
30+
expect(expectNumber(-Infinity)).toEqual(-Infinity);
31+
expect(expectNumber(NaN)).toEqual(NaN);
32+
});
33+
34+
it("rejects non-numbers", () => {
35+
expect(() => expectNumber("1")).toThrowError();
36+
expect(() => expectNumber("1.1")).toThrowError();
37+
expect(() => expectNumber("Infinity")).toThrowError();
38+
expect(() => expectNumber("-Infinity")).toThrowError();
39+
expect(() => expectNumber("NaN")).toThrowError();
40+
expect(() => expectNumber(true)).toThrowError();
41+
expect(() => expectNumber(false)).toThrowError();
42+
expect(() => expectNumber([])).toThrowError();
43+
expect(() => expectNumber({})).toThrowError();
44+
expect(() => expectNumber(null)).toThrowError();
45+
expect(() => expectNumber(undefined)).toThrowError();
46+
});
47+
});
48+
49+
describe("expectString", () => {
50+
it("accepts strings", () => {
51+
expect(expectString("foo")).toEqual("foo");
52+
});
53+
54+
it("rejects non-strings", () => {
55+
expect(() => expectString(1)).toThrowError();
56+
expect(() => expectString(NaN)).toThrowError();
57+
expect(() => expectString(Infinity)).toThrowError();
58+
expect(() => expectString(-Infinity)).toThrowError();
59+
expect(() => expectString(true)).toThrowError();
60+
expect(() => expectString(false)).toThrowError();
61+
expect(() => expectString([])).toThrowError();
62+
expect(() => expectString({})).toThrowError();
63+
expect(() => expectString(null)).toThrowError();
64+
expect(() => expectString(undefined)).toThrowError();
65+
});
66+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Asserts a value is a boolean and returns it.
3+
*
4+
* @param value A value that is expected to be a boolean.
5+
* @returns The value if it is a boolean, otherwise an error is thrown.
6+
*/
7+
export function expectBoolean(value: any): boolean {
8+
if (typeof value === "boolean") {
9+
return value;
10+
}
11+
throw new TypeError(`Expected boolean, got ${typeof value}`);
12+
}
13+
14+
/**
15+
* Asserts a value is a number and returns it.
16+
*
17+
* @param value A value that is expected to be a number.
18+
* @returns The value if it is a number, otherwise an error is thrown.
19+
*/
20+
export function expectNumber(value: any): number {
21+
if (typeof value === "number") {
22+
return value;
23+
}
24+
throw new TypeError(`Expected number, got ${typeof value}`);
25+
}
26+
27+
/**
28+
* Asserts a value is a string and returns it.
29+
*
30+
* @param value A value that is expected to be a string.
31+
* @returns The value if it is a string, otherwise an error is thrown.
32+
*/
33+
export function expectString(value: any): string {
34+
if (typeof value === "string") {
35+
return value;
36+
}
37+
throw new TypeError(`Expected string, got ${typeof value}`);
38+
}

0 commit comments

Comments
 (0)