-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
216 additions
and
1 deletion.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,36 @@ | ||
import { createError } from "../../error"; | ||
|
||
// TODO: Consider using similar method of typeschema for external library compatibility | ||
// https://github.com/decs/typeschema/blob/v0.1.3/src/assert.ts | ||
|
||
export type ValidateResult<T> = T | true | false | void; | ||
|
||
export type ValidateFunction<T> = ( | ||
data: unknown | ||
) => ValidateResult<T> | Promise<ValidateResult<T>>; | ||
|
||
export async function validateData<T>( | ||
data: unknown, | ||
fn: ValidateFunction<T> | ||
): Promise<T> { | ||
try { | ||
const res = await fn(data); | ||
if (res === false) { | ||
throw createValidationError(); | ||
} | ||
if (res === true) { | ||
return data as T; | ||
} | ||
return res ?? (data as T); | ||
} catch (error) { | ||
throw createValidationError(error); | ||
} | ||
} | ||
|
||
function createValidationError(validateError?: any) { | ||
throw createError({ | ||
status: 400, | ||
message: validateError.message || "Validation Failed", | ||
...validateError, | ||
}); | ||
} |
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,146 @@ | ||
import supertest, { SuperTest, Test } from "supertest"; | ||
import { describe, it, expect, beforeEach } from "vitest"; | ||
import { z } from "zod"; | ||
import { | ||
createApp, | ||
toNodeListener, | ||
App, | ||
eventHandler, | ||
readValidatedBody, | ||
getValidatedQuery, | ||
ValidateFunction, | ||
} from "../src"; | ||
|
||
// Custom validator | ||
const customValidate: ValidateFunction<{ | ||
invalidKey: never; | ||
default: string; | ||
field?: string; | ||
}> = (data: any) => { | ||
if (data.invalid) { | ||
throw new Error("Invalid key"); | ||
} | ||
data.default = "default"; | ||
return data; | ||
}; | ||
|
||
// Zod validator (example) | ||
const zodValidate = z.object({ | ||
default: z.string().default("default"), | ||
field: z.string().optional(), | ||
invalid: z.never().optional() /* WTF! */, | ||
}).parse; | ||
|
||
describe("Validate", () => { | ||
let app: App; | ||
let request: SuperTest<Test>; | ||
|
||
beforeEach(() => { | ||
app = createApp({ debug: true }); | ||
request = supertest(toNodeListener(app)); | ||
}); | ||
|
||
describe("readValidatedBody", () => { | ||
beforeEach(() => { | ||
app.use( | ||
"/custom", | ||
eventHandler(async (event) => { | ||
console.log(event.headers); | ||
const data = await readValidatedBody(event, customValidate); | ||
return data; | ||
}) | ||
); | ||
|
||
app.use( | ||
"/zod", | ||
eventHandler(async (event) => { | ||
const data = await readValidatedBody(event, zodValidate); | ||
return data; | ||
}) | ||
); | ||
}); | ||
|
||
describe("custom validator", () => { | ||
it("Valid JSON", async () => { | ||
const res = await request.post("/custom").send({ field: "value" }); | ||
expect(res.body).toEqual({ field: "value", default: "default" }); | ||
expect(res.status).toEqual(200); | ||
}); | ||
|
||
it("Valid x-www-form-urlencoded", async () => { | ||
const res = await request | ||
.post("/custom") | ||
.set("Content-Type", "application/x-www-form-urlencoded") | ||
.send("field=value"); | ||
expect(res.body).toEqual({ field: "value", default: "default" }); | ||
expect(res.status).toEqual(200); | ||
}); | ||
|
||
it("Invalid JSON", async () => { | ||
const res = await request.post("/custom").send({ invalid: true }); | ||
expect(res.text).include("Invalid key"); | ||
expect(res.status).toEqual(400); | ||
}); | ||
}); | ||
|
||
describe("zod validator", () => { | ||
it("Valid", async () => { | ||
const res = await request.post("/zod").send({ field: "value" }); | ||
expect(res.body).toEqual({ field: "value", default: "default" }); | ||
expect(res.status).toEqual(200); | ||
}); | ||
|
||
it("Invalid", async () => { | ||
const res = await request.post("/zod").send({ invalid: true }); | ||
expect(res.status).toEqual(400); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("getQuery", () => { | ||
beforeEach(() => { | ||
app.use( | ||
"/custom", | ||
eventHandler(async (event) => { | ||
const data = await getValidatedQuery(event, customValidate); | ||
return data; | ||
}) | ||
); | ||
|
||
app.use( | ||
"/zod", | ||
eventHandler(async (event) => { | ||
const data = await getValidatedQuery(event, zodValidate); | ||
return data; | ||
}) | ||
); | ||
}); | ||
|
||
describe("custom validator", () => { | ||
it("Valid", async () => { | ||
const res = await request.get("/custom?field=value"); | ||
expect(res.body).toEqual({ field: "value", default: "default" }); | ||
expect(res.status).toEqual(200); | ||
}); | ||
|
||
it("Invalid", async () => { | ||
const res = await request.get("/custom?invalid=true"); | ||
expect(res.text).include("Invalid key"); | ||
expect(res.status).toEqual(400); | ||
}); | ||
}); | ||
|
||
describe("zod validator", () => { | ||
it("Valid", async () => { | ||
const res = await request.get("/zod?field=value"); | ||
expect(res.body).toEqual({ field: "value", default: "default" }); | ||
expect(res.status).toEqual(200); | ||
}); | ||
|
||
it("Invalid", async () => { | ||
const res = await request.get("/zod?invalid=true"); | ||
expect(res.status).toEqual(400); | ||
}); | ||
}); | ||
}); | ||
}); |