fix: Cannot unset collection description, closes #7553

This commit is contained in:
Tom Moor
2024-09-07 18:42:39 -04:00
parent f3fe73057a
commit f984ee0fcc
3 changed files with 21 additions and 13 deletions

View File

@@ -19,7 +19,7 @@ export const CollectionsCreateSchema = BaseSchema.extend({
.regex(ValidateColor.regex, { message: ValidateColor.message })
.nullish(),
description: z.string().nullish(),
data: ProsemirrorSchema.nullish(),
data: ProsemirrorSchema({ allowEmpty: true }).nullish(),
permission: z
.nativeEnum(CollectionPermission)
.nullish()
@@ -152,7 +152,7 @@ export const CollectionsUpdateSchema = BaseSchema.extend({
body: BaseIdSchema.extend({
name: z.string().optional(),
description: z.string().nullish(),
data: ProsemirrorSchema.nullish(),
data: ProsemirrorSchema({ allowEmpty: true }).nullish(),
icon: zodIconType().nullish(),
permission: z.nativeEnum(CollectionPermission).nullish(),
color: z

View File

@@ -33,7 +33,7 @@ export const CommentsCreateSchema = BaseSchema.extend({
parentCommentId: z.string().uuid().optional(),
/** Create comment with this data */
data: ProsemirrorSchema,
data: ProsemirrorSchema(),
}),
});
@@ -42,7 +42,7 @@ export type CommentsCreateReq = z.infer<typeof CommentsCreateSchema>;
export const CommentsUpdateSchema = BaseSchema.extend({
body: BaseIdSchema.extend({
/** Update comment with this data */
data: ProsemirrorSchema,
data: ProsemirrorSchema(),
}),
});

View File

@@ -11,12 +11,20 @@ export const BaseSchema = z.object({
file: z.custom<formidable.File>().optional(),
});
export const ProsemirrorSchema = z.custom<TProsemirrorData>((val) => {
try {
const node = Node.fromJSON(schema, val);
node.check();
return !ProsemirrorHelper.isEmpty(node, schema);
} catch (_e) {
return false;
}
}, "not valid data");
/**
* Returns a Zod schema for validating a Prosemirror document.
*
* @param allowEmpty - Whether to allow an empty document.
*/
export const ProsemirrorSchema = (options?: { allowEmpty: boolean }) =>
z.custom<TProsemirrorData>((val) => {
try {
const node = Node.fromJSON(schema, val);
node.check();
return options?.allowEmpty
? true
: !ProsemirrorHelper.isEmpty(node, schema);
} catch (_e) {
return false;
}
}, "Invalid data");