diff --git a/packages/builder/src/components/design/settings/controls/ValidationEditor/ValidationDrawer.svelte b/packages/builder/src/components/design/settings/controls/ValidationEditor/ValidationDrawer.svelte index 2624c28e7f..8d9ca7e0cd 100644 --- a/packages/builder/src/components/design/settings/controls/ValidationEditor/ValidationDrawer.svelte +++ b/packages/builder/src/components/design/settings/controls/ValidationEditor/ValidationDrawer.svelte @@ -65,6 +65,14 @@ label: "Must not contain", value: "notContains", }, + MaxFileSize: { + label: "Max file size (MB)", + value: "maxFileSize", + }, + MaxUploadSize: { + label: "Max total upload size (MB)", + value: "maxUploadSize", + }, } const ConstraintMap = { ["string"]: [ @@ -94,7 +102,11 @@ Constraints.Equal, Constraints.NotEqual, ], - ["attachment"]: [Constraints.Required], + ["attachment"]: [ + Constraints.Required, + Constraints.MaxFileSize, + Constraints.MaxUploadSize, + ], ["link"]: [ Constraints.Required, Constraints.Contains, @@ -283,7 +295,7 @@ disabled={rule.constraint === "required"} on:change={e => (rule.value = e.detail)} /> - {:else if rule.type !== "array" && ["maxLength", "minLength", "regex", "notRegex", "contains", "notContains"].includes(rule.constraint)} + {:else if rule.type !== "array" && ["maxUploadSize", "maxFileSize", "maxLength", "minLength", "regex", "notRegex", "contains", "notContains"].includes(rule.constraint)} { return value == null || value.length <= limit } +// Evaluates a max file size (MB) constraint +const maxFileSizeHandler = (value, rule) => { + const limit = parseType(rule.value, "number") + return ( + value == null || + !value.some(attachment => attachment.size / 1000000 > limit) + ) +} + +// Evaluates a max total upload size (MB) constraint +const maxUploadSizeHandler = (value, rule) => { + const limit = parseType(rule.value, "number") + return ( + value == null || + value.reduce((acc, currentItem) => acc + currentItem.size, 0) / 1000000 <= + limit + ) +} + // Evaluates a min value constraint const minValueHandler = (value, rule) => { // Use same type as the value so that things can be compared @@ -330,6 +349,8 @@ const handlerMap = { contains: containsHandler, notContains: notContainsHandler, json: jsonHandler, + maxFileSize: maxFileSizeHandler, + maxUploadSize: maxUploadSizeHandler, } /**