-
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.
helper function to generate params schema with custom names and zod s…
…tring validators among "uuid" | "nanoid" | "cuid" | "cuid2" | "ulid" (#7) add getParamsSchema; bump version --------- Co-authored-by: w3cj <[email protected]>
- Loading branch information
1 parent
1802bce
commit 4c3b857
Showing
3 changed files
with
100 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.2.7", | ||
"version": "1.3.0", | ||
"packageManager": "[email protected]", | ||
"description": "Utilities for hono and @hono/zod-openapi", | ||
"author": "w3cj <[email protected]>", | ||
|
@@ -200,6 +200,16 @@ | |
"default": "./dist/cjs/openapi/schemas/slug-params.js" | ||
} | ||
}, | ||
"./openapi/schemas/get-params-schema": { | ||
"import": { | ||
"types": "./dist/esm/openapi/schemas/get-params-schema.d.ts", | ||
"default": "./dist/esm/openapi/schemas/get-params-schema.js" | ||
}, | ||
"require": { | ||
"types": "./dist/cjs/openapi/schemas/get-params-schema.d.ts", | ||
"default": "./dist/cjs/openapi/schemas/get-params-schema.js" | ||
} | ||
}, | ||
"./openapi/schemas/create-message-object": { | ||
"import": { | ||
"types": "./dist/esm/openapi/schemas/create-message-object.d.ts", | ||
|
@@ -280,6 +290,9 @@ | |
"openapi/schemas/slug-params": [ | ||
"./dist/esm/openapi/schemas/slug-params.d.ts" | ||
], | ||
"openapi/schemas/get-params-schema": [ | ||
"./dist/esm/openapi/schemas/get-params-schema.d.ts" | ||
], | ||
"openapi/schemas/create-message-object": [ | ||
"./dist/esm/openapi/schemas/create-message-object.d.ts" | ||
], | ||
|
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,34 @@ | ||
import { z } from "@hono/zod-openapi"; | ||
|
||
type Validator = "uuid" | "nanoid" | "cuid" | "cuid2" | "ulid"; | ||
|
||
export interface ParamsSchema { | ||
name?: string; | ||
validator?: Validator | undefined; | ||
} | ||
|
||
const examples: Record<Validator, string> = { | ||
uuid: "4651e634-a530-4484-9b09-9616a28f35e3", | ||
nanoid: "V1StGXR8_Z5jdHi6B-myT", | ||
cuid: "cjld2cjxh0000qzrmn831i7rn", | ||
cuid2: "tz4a98xxat96iws9zmbrgj3a", | ||
ulid: "01ARZ3NDEKTSV4RRFFQ69G5FAV", | ||
}; | ||
|
||
const getParamsSchema = ({ | ||
name = "id", | ||
validator = "uuid", | ||
}: ParamsSchema) => { | ||
return z.object({ | ||
[name]: z.string()[validator]().openapi({ | ||
param: { | ||
name, | ||
in: "path", | ||
}, | ||
required: [name], | ||
example: examples[validator], | ||
}), | ||
}); | ||
}; | ||
|
||
export default getParamsSchema; |