-
-
Notifications
You must be signed in to change notification settings - Fork 232
chore(i18n): generate JSON Schema for locale files #1249
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Generate a JSON Schema from the canonical `en.json` that provides some cool editor DX niceties (autocomplete, typo detection via `additionalProperties: false`) for all i18n locale files. - Add `scripts/generate-i18n-schema.ts` to generate `i18n/schema.json` from en.json. This is a JSON Schema... schema. - Add `"$schema"` reference to this schema to all 27 locale files - Add `pnpm i18n:schema` and run it in the pre-commit hook (updates schema) and in CI (fails if schema is out of date) Note: in the pre-commit hooks I called the node script directly because pnpm adds a ~250ms overhead (on my machine) and it's important for DX for these hooks to be fast.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
2 Skipped Deployments
|
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
| interface JsonSchema { | ||
| $schema?: string | ||
| title?: string | ||
| description?: string | ||
| type: string | ||
| properties?: Record<string, JsonSchema> | ||
| additionalProperties?: boolean | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically I could pull in a dev dep to get a proper type for this... and we could pull in the JSON Schema meta-schema to validate our schema against the meta-schema... but none of that seemed worth it here.
📝 WalkthroughWalkthroughThis pull request introduces JSON Schema validation to the i18n locale infrastructure. It adds a Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 1✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 4
🧹 Nitpick comments (1)
scripts/generate-i18n-schema.ts (1)
24-39: Avoid silently treating non-string leaf values as strings.Right now, Line 27‑32 maps arrays/booleans/numbers/null to
{ type: 'string' }. If any non-string leaf slips intoen.json, the generated schema will be misleading and IDE validation will flag correct values. Consider asserting string-only leaves (or explicitly mapping types) so mismatches fail fast.💡 Suggested guard
for (const [key, value] of Object.entries(obj)) { if (value !== null && typeof value === 'object' && !Array.isArray(value)) { properties[key] = generateSubSchema(value as Json) - } else { - properties[key] = { type: 'string' } - } + continue + } + if (typeof value === 'string') { + properties[key] = { type: 'string' } + continue + } + throw new Error(`Unsupported i18n value type for "${key}"`) }
| "lint-staged": { | ||
| "i18n/locales/*": [ | ||
| "pnpm build:lunaria", | ||
| "node ./lunaria/lunaria.ts", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling node scripts directly where possible because in my testing on my machine pnpm ... adds a ~250ms overhead and it's important for good DX for these hooks to be as fast as possible.
I had to eliminate the teeny perf optimization that was `copy: true`. It doesn't seem to affect the run time much.
| (locale.file ? getFileName(locale.file) : undefined) ?? | ||
| (files[0] ? getFileName(files[0]) : undefined) | ||
| if (!json) return undefined | ||
| if (copy) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was a teeny minor perf optimization (for a script that is run on demand). I had to remove it in order to exclude $schema from the generated files.
It's still quite fast, so this seems fine to me.
|
I'm not sure if the review request for |
Summary
Generate a JSON Schema from the canonical
en.jsonthat provides some cool editor DX niceties (autocomplete, typo detection viaadditionalProperties: false) for all i18n locale files.Changes
scripts/generate-i18n-schema.tsthat generates a JSON Schema from the structure ofen.jsonand writes it toi18n/schema.json."$schema"reference to this schema to all 27 locale filespnpm i18n:schemaand run it in the pre-commit hook (updates schema) and in CI (fails if schema is out of date)Notes
en.jsonreference file as the source of truth.