schema.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env bun
  2. import { z } from "zod"
  3. import { Config } from "../src/config/config"
  4. const file = process.argv[2]
  5. console.log(file)
  6. const result = z.toJSONSchema(Config.Info, {
  7. io: "input", // Generate input shape (treats optional().default() as not required)
  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. override(ctx) {
  16. const schema = ctx.jsonSchema
  17. // Preserve strictness: set additionalProperties: false for objects
  18. if (
  19. schema &&
  20. typeof schema === "object" &&
  21. schema.type === "object" &&
  22. schema.additionalProperties === undefined
  23. ) {
  24. schema.additionalProperties = false
  25. }
  26. // Add examples and default descriptions for string fields with defaults
  27. if (
  28. schema &&
  29. typeof schema === "object" &&
  30. "type" in schema &&
  31. schema.type === "string" &&
  32. schema?.default
  33. ) {
  34. if (!schema.examples) {
  35. schema.examples = [schema.default]
  36. }
  37. schema.description = [schema.description || "", `default: \`${schema.default}\``]
  38. .filter(Boolean)
  39. .join("\n\n")
  40. .trim()
  41. }
  42. },
  43. }) as Record<string, unknown> & {
  44. allowComments?: boolean
  45. allowTrailingCommas?: boolean
  46. }
  47. // used for json lsps since config supports jsonc
  48. result.allowComments = true
  49. result.allowTrailingCommas = true
  50. await Bun.write(file, JSON.stringify(result, null, 2))