A post about schemas
This feature allows you to get your schema description in the form of a type-safe Zod schema, providing:
Learn more: https://zod.dev
To pull your Zod schema, you will:
cms-rendererapps/
web/
app/
page.tsx
scripts/
generated-schema.ts # script file
generated-schema.tsimport { fetchAllCustomSchemaFields, saveZodSchemaCode } from 'cms-renderer/lib/custom-schemas';
import { cmsConfig } from '../lib/cms-config';
async function main() {
const { cmsUrl, websiteId } = cmsConfig;
if (!cmsUrl) {
throw new Error(
'[generate-schemas] NEXT_PUBLIC_CMS_API_URL is not set. Set it in your environment or .env file.'
);
}
if (!websiteId) {
throw new Error(
'[generate-schemas] CMS_WEBSITE_ID is not set. Set it in your environment or .env file.'
);
}
await saveZodSchemaCode(
await fetchAllCustomSchemaFields(cmsConfig),
'./generated/cms-schemas.ts'
);
console.log('[generate-schemas] Done.');
}
main().catch((err) => {
console.error('[generate-schemas] Failed:', err);
process.exit(1);
});
export const cmsConfig = {
cmsUrl: process.env.NEXT_PUBLIC_CMS_API_URL,
apiKey: process.env.CMS_API_KEY,
websiteId: '...',
};
package.json ConfigurationNote: If you don't have a
tsconfig.json, remove the--tsconfigflag.
{
"name": "web",
"version": "0.1.0",
"type": "module",
"private": true,
"scripts": {
"generate-schemas": "tsx --tsconfig tsconfig.json scripts/generate-schemas.ts",
"...": "..."
},
"dependencies": {
bun run generate-schemas
Example output:
tsx --tsconfig tsconfig.json scripts/generate-schemas.ts
[generate-schemas] Done.
apps/
web/
app/
page.tsx
scripts/
generated-schema.ts
generated/
cms-schemas.ts # generated Zod schema
page.tsximport type { PetFoodPost, SiteConfig } from '@/generated/cms-schemas';
import { petFoodPostSchema } from '@/generated/cms-schemas';
// Use for type-safe parsing
petFoodPostSchema.parse(obj);
bun run generate-schemas