-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move parseJsonWithSchema to utils package (@fehmer)
- Loading branch information
Showing
7 changed files
with
96 additions
and
26 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
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,20 @@ | ||
import * as Util from "../src/util"; | ||
|
||
describe("util", () => { | ||
describe("stringToFunboxNames", () => { | ||
it("should get single funbox", () => { | ||
expect(Util.stringToFunboxNames("58008")).toEqual(["58008"]); | ||
}); | ||
it("should fail for unknown funbox name", () => { | ||
expect(() => Util.stringToFunboxNames("unknown")).toThrowError( | ||
new Error("Invalid funbox name: unknown") | ||
); | ||
}); | ||
it("should split multiple funboxes by hash", () => { | ||
expect(Util.stringToFunboxNames("58008#choo_choo")).toEqual([ | ||
"58008", | ||
"choo_choo", | ||
]); | ||
}); | ||
}); | ||
}); |
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,42 @@ | ||
import { parseWithSchema } from "../src/json"; | ||
import { z } from "zod"; | ||
|
||
describe("json", () => { | ||
describe("parseWithSchema", () => { | ||
const schema = z.object({ | ||
test: z.boolean().optional(), | ||
name: z.string(), | ||
nested: z.object({ foo: z.string() }).strict().optional(), | ||
}); | ||
it("should parse", () => { | ||
const json = `{ | ||
"test":true, | ||
"name":"bob", | ||
"unknown":"unknown", | ||
"nested":{ | ||
"foo":"bar" | ||
} | ||
}`; | ||
|
||
expect(parseWithSchema(json, schema)).toStrictEqual({ | ||
test: true, | ||
name: "bob", | ||
nested: { foo: "bar" }, | ||
}); | ||
}); | ||
it("should fail with invalid schema", () => { | ||
const json = `{ | ||
"test":"yes", | ||
"nested":{ | ||
"foo":1 | ||
} | ||
}`; | ||
|
||
expect(() => parseWithSchema(json, schema)).toThrowError( | ||
new Error( | ||
`"test" Expected boolean, received string\n"name" Required\n"nested.foo" Expected string, received number` | ||
) | ||
); | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
import { ZodError, ZodIssue, ZodSchema } from "zod"; | ||
|
||
/** | ||
* Parse a JSON string into an object and validate it against a schema | ||
* @param json JSON string | ||
* @param schema Zod schema to validate the JSON against | ||
* @returns The parsed JSON object | ||
*/ | ||
export function parseWithSchema<T>(json: string, schema: ZodSchema<T>): T { | ||
try { | ||
const jsonParsed = JSON.parse(json) as unknown; | ||
const zodParsed = schema.parse(jsonParsed); | ||
return zodParsed; | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
throw new Error(error.issues.map(prettyErrorMessage).join("\n")); | ||
} else { | ||
throw error; | ||
} | ||
} | ||
} | ||
|
||
function prettyErrorMessage(issue: ZodIssue | undefined): string { | ||
if (issue === undefined) return ""; | ||
const path = issue.path.length > 0 ? `"${issue.path.join(".")}" ` : ""; | ||
return `${path}${issue.message}`; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.