-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added id params for uuid and slug --------- Co-authored-by: Steven Rafferty <[email protected]>
- Loading branch information
Showing
5 changed files
with
185 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "stoker", | ||
"type": "module", | ||
"version": "1.0.9", | ||
"version": "1.1.0", | ||
"packageManager": "[email protected]", | ||
"description": "Utilities for hono and @hono/zod-openapi", | ||
"author": "w3cj <[email protected]>", | ||
|
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,14 @@ | ||
import { z } from "@hono/zod-openapi"; | ||
|
||
const IdUUIDParamsSchema = z.object({ | ||
id: z.string().uuid().openapi({ | ||
param: { | ||
name: "id", | ||
in: "path", | ||
}, | ||
required: ["id"], | ||
example: "4651e634-a530-4484-9b09-9616a28f35e3", | ||
}), | ||
}); | ||
|
||
export default IdUUIDParamsSchema; |
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,60 @@ | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import SlugParamsSchema from "./slug-params"; | ||
|
||
describe("slug-params", () => { | ||
it("allows letters", () => { | ||
const slug = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
const { data, error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(data?.slug).toBe(slug); | ||
expect(error).toBeUndefined(); | ||
}); | ||
it("allows numbers", () => { | ||
const slug = "0123456789"; | ||
const { data, error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(data?.slug).toBe(slug); | ||
expect(error).toBeUndefined(); | ||
}); | ||
it("allows numbers and letters", () => { | ||
const slug = "0123456789abcdeABCDE"; | ||
const { data, error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(data?.slug).toBe(slug); | ||
expect(error).toBeUndefined(); | ||
}); | ||
it("allows dashes", () => { | ||
const slug = "test-slug-here"; | ||
const { data, error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(data?.slug).toBe(slug); | ||
expect(error).toBeUndefined(); | ||
}); | ||
it("allows underscores", () => { | ||
const slug = "test_slug_here"; | ||
const { data, error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(data?.slug).toBe(slug); | ||
expect(error).toBeUndefined(); | ||
}); | ||
it("does not allow special characters only", () => { | ||
const slug = "!@#$%^&*()+-="; | ||
const { error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(error).toBeDefined(); | ||
}); | ||
it("does not allow special characters with allowed characters", () => { | ||
const slug = "abc-DEF_ABC-!@#$%^&*()+-="; | ||
const { error } = SlugParamsSchema.safeParse({ | ||
slug, | ||
}); | ||
expect(error).toBeDefined(); | ||
}); | ||
}); |
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 { z } from "@hono/zod-openapi"; | ||
|
||
// Regular expression to validate slug format: alphanumeric, underscores, and dashes | ||
const slugReg = /^[\w-]+$/; | ||
const SLUG_ERROR_MESSAGE = "Slug can only contain letters, numbers, dashes, and underscores"; | ||
|
||
const SlugParamsSchema = z.object({ | ||
slug: z.string() | ||
.regex(slugReg, SLUG_ERROR_MESSAGE) | ||
.openapi({ | ||
param: { | ||
name: "slug", | ||
in: "path", | ||
}, | ||
required: ["slug"], | ||
example: "my-cool-article", | ||
}), | ||
}); | ||
|
||
export default SlugParamsSchema; |