Enhancement: Better content type creation for Astro #704

This commit is contained in:
Elio Struyf
2023-11-11 11:19:11 -08:00
parent e1f6c90fc0
commit a8407920bc
5 changed files with 43 additions and 56 deletions
+1 -32
View File
@@ -3,8 +3,7 @@ import { join } from 'path';
import { createServer } from 'vite';
import zod from 'astro/zod';
const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion, ZodArray } =
zod;
const { ZodDefault, ZodObject, ZodOptional, ZodString, ZodEffects, ZodEnum, ZodUnion } = zod;
/**
* Process the Zod field
@@ -73,26 +72,6 @@ function generateFieldInfo(name, type) {
required: !isFieldOptional
};
switch (fieldInfo.type) {
case 'ZodString':
fieldInfo.type = 'string';
break;
case 'ZodNumber':
fieldInfo.type = 'number';
break;
case 'ZodBoolean':
fieldInfo.type = 'boolean';
break;
case 'ZodDate':
fieldInfo.type = 'datetime';
break;
case 'ZodArray':
fieldInfo.type = 'choice';
break;
default:
break;
}
if (fieldType instanceof ZodObject) {
const subFields = extractFieldInfoFromShape(fieldType);
@@ -111,16 +90,6 @@ function generateFieldInfo(name, type) {
fieldInfo.options = fieldType.options;
}
if (fieldType instanceof ZodArray) {
if (fieldInfo.name === 'tags') {
fieldInfo.type = 'tags';
} else if (fieldInfo.name === 'categories') {
fieldInfo.type = 'categories';
} else {
fieldInfo.options = fieldType.options;
}
}
if (fieldType instanceof ZodString) {
// https://github.com/StefanTerdell/zod-to-json-schema/blob/master/src/parsers/string.ts#L45
if (fieldType._def.checks && fieldType._def.checks.length > 0) {