schema.ts 1.3 KB

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