Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - disable slug change check #762

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 9 additions & 57 deletions studio/schemas/schemaTypes/slug.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import { groq } from "next-sanity";
import {
SlugValidationContext,
SlugValue,
ValidationContext,
defineField,
isSlug,
} from "sanity";
import { SlugValidationContext, defineField } from "sanity";

import { apiVersion } from "studio/env";
import {
buildDraftId,
buildPublishedId,
isPublished,
} from "studio/utils/documentUtils";
import { buildDraftId, buildPublishedId } from "studio/utils/documentUtils";

export function isSlugUniqueAcrossDocuments(
slug: string,
Expand Down Expand Up @@ -40,30 +30,6 @@ export function isSlugUniqueAcrossDocuments(
});
}

/**
Validate that slug has not been changed after initial publication
*/
async function validateSlugNotChangedAfterPublication(
value: SlugValue | undefined,
{ document, getClient }: ValidationContext,
) {
if (document === undefined || isPublished(document)) {
return true;
}
const publishedDocument = await getClient({ apiVersion }).getDocument(
buildPublishedId(document._id),
);
if (
publishedDocument !== undefined &&
"slug" in publishedDocument &&
isSlug(publishedDocument.slug) &&
publishedDocument.slug.current !== value?.current
) {
return "Can not be changed after publication";
}
return true;
}

function slugify(input: string): string {
return (
input
Expand Down Expand Up @@ -98,27 +64,13 @@ function createSlugField(source: string) {
isUnique: isSlugUniqueAcrossDocuments,
},
validation: (rule) =>
rule
.required()
.custom(validateSlugNotChangedAfterPublication)
.custom((value) => {
if (value?.current === undefined) return true;
return (
encodeURIComponent(value.current) === value.current ||
"Slug can only consist of latin letters (a-z, A-Z), digits (0-9), hyphen (-), underscore (_), full stop (.) and tilde (~)"
);
}),
readOnly: ({ document }) => {
/*
make slugs read-only after initial publish
to avoid breaking shared links
if document is already draft, this is handled through validation instead
if new slugs are needed, redirects can be used instead
*/
return document !== undefined && isPublished(document);
},
rule.required().custom((value) => {
if (value?.current === undefined) return true;
return (
encodeURIComponent(value.current) === value.current ||
"Slug can only consist of latin letters (a-z, A-Z), digits (0-9), hyphen (-), underscore (_), full stop (.) and tilde (~)"
);
}),
});
}

Expand Down