schema.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 (schema && typeof schema === "object" && schema.type === "object" && schema.additionalProperties === undefined) {
  19. schema.additionalProperties = false
  20. }
  21. // Add examples and default descriptions for string fields with defaults
  22. if (schema && typeof schema === "object" && "type" in schema && schema.type === "string" && schema?.default) {
  23. if (!schema.examples) {
  24. schema.examples = [schema.default]
  25. }
  26. schema.description = [schema.description || "", `default: \`${schema.default}\``]
  27. .filter(Boolean)
  28. .join("\n\n")
  29. .trim()
  30. }
  31. },
  32. }) as Record<string, unknown> & {
  33. allowComments?: boolean
  34. allowTrailingCommas?: boolean
  35. }
  36. // used for json lsps since config supports jsonc
  37. result.allowComments = true
  38. result.allowTrailingCommas = true
  39. await Bun.write(file, JSON.stringify(result, null, 2))