fixing TODOs for date and email
This commit is contained in:
@@ -44,15 +44,16 @@ export function stringifyContent(collections: CollectionModel[], opts: GenerateO
|
|||||||
|
|
||||||
function stringifyField(field: CollectionField, collectionName: string) {
|
function stringifyField(field: CollectionField, collectionName: string) {
|
||||||
let schema: string;
|
let schema: string;
|
||||||
// if (collectionName === "relations") console.log(`${collectionName}:`, field);
|
|
||||||
switch (field.type) {
|
switch (field.type) {
|
||||||
case "bool":
|
case "bool":
|
||||||
schema = "z.boolean()";
|
schema = "z.boolean()";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "date":
|
case "date":
|
||||||
// TODO: implement min and max
|
const minConstraintDate = field.min ? `.min(new Date("${field.min}"))` : "";
|
||||||
schema = "z.string().pipe(z.coerce.date())";
|
const maxConstraintDate = field.max ? `.max(new Date("${field.max}"))` : "";
|
||||||
|
|
||||||
|
schema = `z.string().pipe(z.coerce.date()${minConstraintDate}${maxConstraintDate})`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "editor":
|
case "editor":
|
||||||
@@ -61,11 +62,14 @@ export function stringifyContent(collections: CollectionModel[], opts: GenerateO
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "email":
|
case "email":
|
||||||
// TODO: implement exceptDomains and onlyDomains
|
const onlyDomainsConstraint = createEmailDomainConstraint(field.onlyDomains, true);
|
||||||
schema = "z.string().email()";
|
const exceptDomainsConstraint = createEmailDomainConstraint(field.exceptDomains, false);
|
||||||
|
|
||||||
|
schema = `z.string().email()${onlyDomainsConstraint}${exceptDomainsConstraint}`;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "file":
|
case "file":
|
||||||
|
// if (collectionName === "tests") console.log(`${collectionName}:`, field);
|
||||||
const maxSelectFile = field.maxSelect;
|
const maxSelectFile = field.maxSelect;
|
||||||
// TODO: implement maxSize, mimeTypes, protected, thumbs
|
// TODO: implement maxSize, mimeTypes, protected, thumbs
|
||||||
schema = `z.string()${maxSelectFile === 1 ? "" : `.array()${maxSelectFile ? `.max(${maxSelectFile})` : ""}`}`;
|
schema = `z.string()${maxSelectFile === 1 ? "" : `.array()${maxSelectFile ? `.max(${maxSelectFile})` : ""}`}`;
|
||||||
@@ -126,6 +130,15 @@ export function stringifyContent(collections: CollectionModel[], opts: GenerateO
|
|||||||
return `${field.name}: ${schema}${(field as any).required ? "" : ".optional()"}`;
|
return `${field.name}: ${schema}${(field as any).required ? "" : ".optional()"}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Helpers */
|
||||||
|
|
||||||
|
const createEmailDomainConstraint = (domains: string[], isWhitelist: boolean) => {
|
||||||
|
if (!domains?.length) return "";
|
||||||
|
const domainsList = domains.map((domain) => `"${domain}"`).join(", ");
|
||||||
|
const messageType = isWhitelist ? "isn't one of the allowed ones" : "is one of the disallowed ones";
|
||||||
|
return `.refine((email) => { const domain = email.split("@")[1]; return domain && ${isWhitelist ? "" : "!"}[${domainsList}].includes(domain); }, { message: "Invalid email, email domain ${messageType}" })`;
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
collectionNames: `[\n\t${collections.map(({ name }) => `"${name}"`).join(",\n\t")},\n]`,
|
collectionNames: `[\n\t${collections.map(({ name }) => `"${name}"`).join(",\n\t")},\n]`,
|
||||||
enums: getCollectionSelectFields().map(stringifyEnum).join("\n\n"),
|
enums: getCollectionSelectFields().map(stringifyEnum).join("\n\n"),
|
||||||
|
|||||||
Reference in New Issue
Block a user