Unlock seamless workflows and faster delivery with our latest releases – get the details

Is your Microcopy unique?

By William Iommi

A custom validation to check if your Microcopy documents have unique keys and values for a specific namespace.

MicrocopyDocument.ts

const Microcopy = defineType({
  type: 'document',
  name: 'microcopy',
  title: 'Microcopy',
  preview: {
    select: {
      message: 'message',
      key: 'key',
      namespace: 'namespace',
    },
    prepare: ({ message, namespace, key }) => ({
      title: message,
      subtitle: `${namespace}:${key}`,
    }),
  },
  fields: [
    defineField({
      type: 'string',
      name: 'namespace',
      title: 'Namespace',
      options: {
        list: [
          { value: 'global', title: 'Global' },
          { value: 'account', title: 'Account' },
          { value: 'commerce', title: 'Commerce' },
        ],
      },
    }),
    defineField({
      type: 'string',
      name: 'key',
      title: 'Key',
      validation: (Rule) => Rule.custom(isUniqueMicrocopyKey),
    }),
    defineField({
      type: 'string',
      name: 'message',
      title: 'Message',
      validation: (Rule) => Rule.custom(isUniqueMicrocopyValue),
    }),
  ],
});

Utils.ts

const uniqueMicrocopyKeyQuery =
  '!defined(*[_type==$type && key==$value && namespace==$namespace && !(_id in [$draftId, $publishedId])][0]._id)';

const uniqueMicrocopyValueQuery =
  '!defined(*[_type==$type && message==$value && namespace==$namespace && !(_id in [$draftId, $publishedId])][0]._id)';

const executeQuery = async (
  ctx: ValidationContext,
  query: string,
  value?: string,
) => {
  const { document } = ctx;
  if (!value || !document || !document.namespace) return true;
  const id = document._id.replace('drafts.', '');
  const params = {
    type: 'microcopy',
    value,
    namespace: document.namespace,
    draftId: `drafts.${id}`,
    publishedId: id,
  };
  return await ctx
    .getClient({ apiVersion: '2023-01-01' })
    .fetch(query, params);
};

export const isUniqueMicrocopyKey = async (
  value: string | undefined,
  ctx: ValidationContext,
): Promise<CustomValidatorResult> => {
  const isUnique = await executeQuery(ctx, uniqueMicrocopyKeyQuery, value);
  return isUnique
    ? true
    : 'There is another microcopy with the same key for this namespace';
};

export const isUniqueMicrocopyValue = async (
  value: string | undefined,
  ctx: ValidationContext,
): Promise<CustomValidatorResult> => {
  const isUnique = await executeQuery(ctx, uniqueMicrocopyValueQuery, value);
  return isUnique
    ? true
    : 'There is another microcopy with the same value for this namespace';
};

Imagine you have a content model for your microcopy or storefront messages (let's call it whatever you want 😁). Imagine you also have a specific namespace field to select for every document.

With these kinds of messages, it's pretty easy to create duplicates. Maybe you end up with different documents with the same key or a different key but the value is the same.

This custom validation lets you check if you already have a Microcopy with the same key for the same namespace and shows also an error if the value is the same.

Of course, you are free to do differently. We are on Sanity after all 😍.
Maybe you can give only a warning or check across all Microcopy documents and not only inside a specific namespace.

Contributor

Other schemas by author