schema.ts 1.0 KB

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