schema.ts 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env bun
  2. import { z } from "zod/v4"
  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. /**
  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. override(input) {
  15. const schema = input.jsonSchema
  16. if (schema && typeof schema === "object" && "type" in schema && schema.type === "string" && schema?.default) {
  17. if (!schema.examples) {
  18. schema.examples = [schema.default]
  19. }
  20. schema.description = [schema.description || "", `default: \`${schema.default}\``]
  21. .filter(Boolean)
  22. .join("\n\n")
  23. .trim()
  24. }
  25. },
  26. }) as Record<string, unknown> & {
  27. allowComments?: boolean
  28. allowTrailingCommas?: boolean
  29. }
  30. // used for json lsps since config supports jsonc
  31. result.allowComments = true
  32. result.allowTrailingCommas = true
  33. await Bun.write(file, JSON.stringify(result, null, 2))