schema.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env bun
  2. import "zod-openapi/extend"
  3. import { Config } from "../src/config/config"
  4. import { zodToJsonSchema } from "zod-to-json-schema"
  5. const file = process.argv[2]
  6. const result = zodToJsonSchema(Config.Info, {
  7. /**
  8. * We'll use the `default` values of the field as the only value in `examples`.
  9. * This will ensure no docs are needed to be read, as the configuration is
  10. * self-documenting.
  11. *
  12. * See https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.9.5
  13. */
  14. postProcess(jsonSchema) {
  15. const schema = jsonSchema as typeof jsonSchema & {
  16. examples?: unknown[]
  17. }
  18. if (schema && typeof schema === "object" && "type" in schema && schema.type === "string" && schema?.default) {
  19. if (!schema.examples) {
  20. schema.examples = [schema.default]
  21. }
  22. schema.description = [schema.description || "", `default: \`${schema.default}\``]
  23. .filter(Boolean)
  24. .join("\n\n")
  25. .trim()
  26. }
  27. return jsonSchema
  28. },
  29. })
  30. await Bun.write(file, JSON.stringify(result, null, 2))