Skip to content

Commit 8ee1a0e

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 4e4c1a0 commit 8ee1a0e

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

packages/smithy-client/src/parse-utils.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parseBoolean } from "./parse-utils";
2+
import { expectBoolean, expectNumber, expectString } from "./parse-utils";
23

34
describe("parseBoolean", () => {
45
it('Returns true for "true"', () => {
@@ -45,3 +46,68 @@ describe("parseBoolean", () => {
4546
expect(() => parseBoolean({} as any)).toThrowError();
4647
});
4748
});
49+
50+
describe("expectBoolean", () => {
51+
it("accepts booleans", () => {
52+
expect(expectBoolean(true)).toEqual(true);
53+
expect(expectBoolean(false)).toEqual(false);
54+
});
55+
56+
it("rejects non-booleans", () => {
57+
expect(() => expectBoolean("true")).toThrowError();
58+
expect(() => expectBoolean("false")).toThrowError();
59+
expect(() => expectBoolean(0)).toThrowError();
60+
expect(() => expectBoolean(1)).toThrowError();
61+
expect(() => expectBoolean(1.1)).toThrowError();
62+
expect(() => expectBoolean(Infinity)).toThrowError();
63+
expect(() => expectBoolean(-Infinity)).toThrowError();
64+
expect(() => expectBoolean(NaN)).toThrowError();
65+
expect(() => expectBoolean({})).toThrowError();
66+
expect(() => expectBoolean([])).toThrowError();
67+
expect(() => expectBoolean(null)).toThrowError();
68+
expect(() => expectBoolean(undefined)).toThrowError();
69+
});
70+
});
71+
72+
describe("expectNumber", () => {
73+
it("accepts numbers", () => {
74+
expect(expectNumber(1)).toEqual(1);
75+
expect(expectNumber(1.1)).toEqual(1.1);
76+
expect(expectNumber(Infinity)).toEqual(Infinity);
77+
expect(expectNumber(-Infinity)).toEqual(-Infinity);
78+
expect(expectNumber(NaN)).toEqual(NaN);
79+
});
80+
81+
it("rejects non-numbers", () => {
82+
expect(() => expectNumber("1")).toThrowError();
83+
expect(() => expectNumber("1.1")).toThrowError();
84+
expect(() => expectNumber("Infinity")).toThrowError();
85+
expect(() => expectNumber("-Infinity")).toThrowError();
86+
expect(() => expectNumber("NaN")).toThrowError();
87+
expect(() => expectNumber(true)).toThrowError();
88+
expect(() => expectNumber(false)).toThrowError();
89+
expect(() => expectNumber([])).toThrowError();
90+
expect(() => expectNumber({})).toThrowError();
91+
expect(() => expectNumber(null)).toThrowError();
92+
expect(() => expectNumber(undefined)).toThrowError();
93+
});
94+
});
95+
96+
describe("expectString", () => {
97+
it("accepts strings", () => {
98+
expect(expectString("foo")).toEqual("foo");
99+
});
100+
101+
it("rejects non-strings", () => {
102+
expect(() => expectString(1)).toThrowError();
103+
expect(() => expectString(NaN)).toThrowError();
104+
expect(() => expectString(Infinity)).toThrowError();
105+
expect(() => expectString(-Infinity)).toThrowError();
106+
expect(() => expectString(true)).toThrowError();
107+
expect(() => expectString(false)).toThrowError();
108+
expect(() => expectString([])).toThrowError();
109+
expect(() => expectString({})).toThrowError();
110+
expect(() => expectString(null)).toThrowError();
111+
expect(() => expectString(undefined)).toThrowError();
112+
});
113+
});

packages/smithy-client/src/parse-utils.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,43 @@ export const parseBoolean = (value: string): boolean => {
1313
default:
1414
throw new Error(`Unable to parse boolean value "${value}"`);
1515
}
16+
};
17+
18+
/*
19+
* Asserts a value is a boolean and returns it.
20+
*
21+
* @param value A value that is expected to be a boolean.
22+
* @returns The value if it is a boolean, otherwise an error is thrown.
23+
*/
24+
export function expectBoolean(value: any): boolean {
25+
if (typeof value === "boolean") {
26+
return value;
27+
}
28+
throw new TypeError(`Expected boolean, got ${typeof value}`);
29+
}
30+
31+
/**
32+
* Asserts a value is a number and returns it.
33+
*
34+
* @param value A value that is expected to be a number.
35+
* @returns The value if it is a number, otherwise an error is thrown.
36+
*/
37+
export function expectNumber(value: any): number {
38+
if (typeof value === "number") {
39+
return value;
40+
}
41+
throw new TypeError(`Expected number, got ${typeof value}`);
42+
}
43+
44+
/**
45+
* Asserts a value is a string and returns it.
46+
*
47+
* @param value A value that is expected to be a string.
48+
* @returns The value if it is a string, otherwise an error is thrown.
49+
*/
50+
export function expectString(value: any): string {
51+
if (typeof value === "string") {
52+
return value;
53+
}
54+
throw new TypeError(`Expected string, got ${typeof value}`);
1655
}

0 commit comments

Comments
 (0)