Repository navigation
Replies: 2 comments
|
import { type } from "arktype"
const file = type("File").configure({
toJsonSchema: {
fallback: {
proto: (ctx) => ({ ...ctx.base, type: "string", format: "binary" }),
},
},
})
// import `file` anywhere and reuse it directly
const UploadSchema = type({ attachment: file })
UploadSchema.toJsonSchema()
// attachment: { type: "string", format: "binary" }, no fallback passed here at allConfig applied at the "type" level like this carries through to every scope that references |
|
A version-specific correction to the A contained alternative, without global configuration, is to export a configured scope and build the containing schemas through it: // upload-types.ts
import { scope } from 'arktype'
export const uploads = scope(
{
FileUpload: 'File',
Upload: { attachment: 'FileUpload' }
},
{
toJsonSchema: {
fallback: {
proto: ctx => {
// Do not silently document every unsupported class as a file.
if (ctx.proto !== File) {
throw new Error(`Unsupported prototype: ${ctx.proto.name}`)
}
return { ...ctx.base, type: 'string', format: 'binary' }
}
}
}
}
)
export const { FileUpload, Upload } = uploads.export()import { uploads, Upload } from './upload-types.js'
Upload.toJsonSchema()
// properties.attachment: { type: 'string', format: 'binary' }
const NestedUpload = uploads.type({ inner: { files: 'FileUpload[]' } })
NestedUpload.toJsonSchema()
// properties.inner.properties.files.items:
// { type: 'string', format: 'binary' }The scope boundary matters: importing Verified on ArkType 2.2.9 / TypeScript 5.9.3 / Node 24.22.1: the original metadata example fails both strict type checking and serialization; the scope example type-checks and produces the expected schema for direct, nested-array and recursive properties. A real |
Uh oh!
There was an error while loading. Please reload this page.
As toJsonSchema documentation says It seems like I've got to define a
fallbackeach time when I want to converttype('File')into a proper OAS-compatible file schema withformat: "binary"attribute. My question is if it it's possible to define an Arktypetypethat I can import and reuse, or if I can modify the built-inFiletype sotoJsonSchemaworks similar to.filefrom Zod 4 and transparent to thetoJsonSchemafunction? Thank you!All reactions