Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ const models = {

Generates either `jsonSchema7` or `openApi3` schema. See [`zod-to-json-schema`](https://github.com/StefanTerdell/zod-to-json-schema#options-object).

##### `BuildJsonSchemasOptions.refStrategy: `jsonSchema7`|`openApi3` = "jsonSchema7"`: _jsonSchema7_ (default) or _openApi3_

Uses either `root`, `relative` or `none` (default `root`) for the reference builder strategy. See [`zod-to-json-schema`](https://github.com/StefanTerdell/zod-to-json-schema#options-object) options object (`$refStrategy?`) for more information.

#### `BuildJsonSchemasResult<typeof models> = { schemas: JsonSchema[], $ref: $ref<typeof models> }`

The result of `buildJsonSchemas` has 2 components: an array of schemas that can be added directly to fastify using `fastify.addSchema`, and a `$ref` function that returns a `{ $ref: string }` object that can be used directly.
Expand Down
2 changes: 2 additions & 0 deletions src/JsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type BuildJsonSchemasOptions = {
readonly $id?: string;
readonly target?: `jsonSchema7` | `openApi3`;
readonly errorMessages?: boolean;
readonly refStrategy?: `root` | `relative` | `none`;
};

export type $Ref<M extends Models> = (key: SchemaKeyOrDescription<M>) => {
Expand Down Expand Up @@ -43,6 +44,7 @@ export const buildJsonSchemas = <M extends Models>(
target: opts.target,
basePath: [`${$id}#`],
errorMessages: opts.errorMessages,
$refStrategy: opts.refStrategy ?? `root`,
});

const jsonSchema: JsonSchema = {
Expand Down