Repository navigation
Description
Request a feature
Support target: "openapi-3.0" in toJsonSchema() and in the Standard JSON Schema ~standard.jsonSchema.input() / .output() methods.
🤷 Motivation
OpenAPI 3.0 documents are still common, and their Schema Object is a JSON Schema draft-04 superset with its own keywords. The Standard JSON Schema spec lists "openapi-3.0" as one of its standard targets, next to "draft-2020-12" and "draft-07". A tool that builds an OpenAPI 3.0 document from Standard JSON Schema validators can ask zod, sury and valibot (through toStandardJsonSchema from @valibot/to-json-schema) for that target today. ArkType throws:
import { type } from "arktype"
type("string | null")["~standard"].jsonSchema.input({ target: "openapi-3.0" })
// ParseError: JSONSchema target 'openapi-3.0' is not supported (must be "draft-2020-12" or "draft-07")The spec says libraries should throw for a target they don't support, so this is correct behaviour; the request is to support the target. type.toJsonSchema({ target: "openapi-3.0" }) is rejected by the types and, at runtime, returns draft 2020-12 output without $schema.
The check sits in validateStandardJsonSchemaTarget:
arktype/ark/schema/roots/root.ts
Lines 713 to 728 in 3fddc2a
| const supportedJsonSchemaTargets: ToJsonSchema.Target[] = [ | |
| "draft-2020-12", | |
| "draft-07" | |
| ] | |
| export const writeInvalidJsonSchemaTargetMessage = (target: string): string => | |
| `JSONSchema target '${target}' is not supported (must be ${supportedJsonSchemaTargets.map(t => `"${t}"`).join(" or ")})` | |
| const validateStandardJsonSchemaTarget = ( | |
| target: StandardJSONSchemaV1.Target | |
| ): ToJsonSchema.Target => { | |
| if (!includes(supportedJsonSchemaTargets, target)) | |
| throwParseError(writeInvalidJsonSchemaTargetMessage(target)) | |
| return target | |
| } |
Without the target, the caller has to post-process ArkType's draft-07 output into OpenAPI 3.0, while the other validators already do that themselves.
💡 Solution
Add "openapi-3.0" to ToJsonSchema.Target and translate the keywords OpenAPI 3.0 lacks, like the "draft-07" branches already do for definitions and tuple items. For a few shapes, ArkType 2.2.9's "draft-07" output and what zod 4.6.7 writes for "openapi-3.0":
| Shape | ArkType, "draft-07" |
zod, "openapi-3.0" |
|---|---|---|
type("0 < number < 10") |
{"type":"number","exclusiveMaximum":10,"exclusiveMinimum":0} |
{"type":"number","minimum":0,"exclusiveMinimum":true,"maximum":10,"exclusiveMaximum":true} |
type("'cat'") |
{"const":"cat"} |
{"type":"string","enum":["cat"]} |
type("string | null") |
{"anyOf":[{"type":"string"},{"type":"null"}]} |
{"nullable":true,"type":"string"} |
type(["string", "number"]) |
{"type":"array","minItems":2,"items":[{"type":"string"},{"type":"number"}],"additionalItems":false} |
{"type":"array","items":{"anyOf":[{"type":"string"},{"type":"number"}]},"minItems":2,"maxItems":2} |
($schema omitted from the ArkType column.)
In short:
- Exclusive bounds become
minimum/maximumwith booleanexclusiveMinimum/exclusiveMaximum. constbecomes a single-valueenum.nullin a union becomesnullable: trueon the remaining branch, since 3.0 has no"null"type.- No
$schemakeyword. - Tuples have no exact 3.0 form, because
itemsmust be a single schema.sury,valibotandzodall approximate a tuple asitems: { anyOf: [...] }plusminItems/maxItems. Throwing, or deferring to the existingfallbackhandlers, would be alternatives.
For comparison, sury and valibot write the same nullable, enum and tuple forms. valibot throws for exclusive bounds (The "gt_value" action is not supported for OpenAPI 3.0.).
Related: #1328 (OpenAPI-compatible enum output for unions), #1465 (OpenAPI discriminator).
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone (merged or closed)
Activity
pullfrog commentedon Oct 22, 2026
feat(sets): write OpenAPI Schema Objects for target "openapi-3.0"
docs(changelog): thank @MichaelDeBoey for #1707
ssalbdivad commentedon Nov 18, 2026
Fixed in 3.0.0-beta.0 (#1728). Try it with
npm i arktype@next.