From bd4e5e0a1ed3b78ac1c9471c300afb510f871a58 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Tue, 10 Sep 2024 11:15:23 +0200 Subject: [PATCH] fix(redirect): conditional required depending on destination type --- studio/schemas/documents/redirect.ts | 33 ++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/studio/schemas/documents/redirect.ts b/studio/schemas/documents/redirect.ts index 1f490aaf7..e177d2e1e 100644 --- a/studio/schemas/documents/redirect.ts +++ b/studio/schemas/documents/redirect.ts @@ -1,16 +1,34 @@ import { defineField, defineType, type Slug } from "sanity"; -import { SlugRule } from "@sanity/types"; +import { SanityDocument, SlugRule } from "@sanity/types"; import { pageBuilderID } from "../builders/pageBuilder"; import { blogId } from "./blog"; import { compensationsId } from "./compensations"; import PrefixedSlugInput from "../../components/PrefixedSlugInput"; -const slugValidator = (rule: SlugRule) => +const slugRequired = (rule: SlugRule) => rule.required().custom((value: Slug | undefined) => { if (!value || !value.current) return "Can't be blank"; return true; }); +const requiredIfDestinationType = ( + type: string, + document: SanityDocument | undefined, + value: unknown, +) => { + const destination = document?.destination; + if ( + typeof destination === "object" && + destination !== null && + "type" in destination && + destination.type === type && + value === undefined + ) { + return "Can't be blank"; + } + return true; +}; + export const redirectId = "redirect"; const redirect = defineType({ @@ -23,7 +41,7 @@ const redirect = defineType({ title: "Source", description: "Which url should this redirect apply for", type: "slug", - validation: slugValidator, + validation: (rule) => slugRequired(rule), components: { input: (props) => PrefixedSlugInput({ prefix: "/", ...props }), }, @@ -58,6 +76,10 @@ const redirect = defineType({ { type: compensationsId }, ], hidden: ({ parent }) => parent?.type !== "reference", + validation: (rule) => + rule.custom((value, { document }) => + requiredIfDestinationType("reference", document, value), + ), }), defineField({ name: "slug", @@ -65,7 +87,10 @@ const redirect = defineType({ description: "Where should the user be redirected?", type: "slug", hidden: ({ parent }) => parent?.type !== "slug", - validation: slugValidator, + validation: (rule) => + rule.custom((value, { document }) => + requiredIfDestinationType("slug", document, value), + ), options: { isUnique: () => { /*