From 43ee488710ef78f9ed49dbfe6522f6796557ab1d Mon Sep 17 00:00:00 2001 From: Marie Gauthier Date: Thu, 14 Nov 2024 07:47:28 +0100 Subject: [PATCH] fix: check uniqueness of iris (slugs) with different languages (#71) --- schemas/documents/registry/creditClass.js | 2 ++ schemas/documents/shared/ecologicalImpact.js | 2 ++ schemas/documents/shared/sdg.js | 2 ++ utils/isUniqueSlug.js | 21 ++++++++++++++++++++ 4 files changed, 27 insertions(+) create mode 100644 utils/isUniqueSlug.js diff --git a/schemas/documents/registry/creditClass.js b/schemas/documents/registry/creditClass.js index 49c9106..5541970 100644 --- a/schemas/documents/registry/creditClass.js +++ b/schemas/documents/registry/creditClass.js @@ -1,4 +1,5 @@ import slugifyToIRI from '../../../utils/slugifyToIRI'; +import isUniqueSlug from '../../../utils/isUniqueSlug'; import toPlainText from '../../../utils/toPlainText'; export default { @@ -20,6 +21,7 @@ export default { validation: Rule => Rule.required(), options: { source: 'name', + isUnique: isUniqueSlug, slugify: input => { return `${slugifyToIRI(toPlainText(input))}CreditClass`; }, diff --git a/schemas/documents/shared/ecologicalImpact.js b/schemas/documents/shared/ecologicalImpact.js index 240adba..60dced3 100644 --- a/schemas/documents/shared/ecologicalImpact.js +++ b/schemas/documents/shared/ecologicalImpact.js @@ -1,4 +1,5 @@ import slugifyToIRI from '../../../utils/slugifyToIRI'; +import isUniqueSlug from '../../../utils/isUniqueSlug'; export default { title: 'Ecological Impact', @@ -19,6 +20,7 @@ export default { validation: Rule => Rule.required(), options: { source: 'name', + isUnique: isUniqueSlug, slugify: input => { return slugifyToIRI(input); }, diff --git a/schemas/documents/shared/sdg.js b/schemas/documents/shared/sdg.js index 4a69547..d87af79 100644 --- a/schemas/documents/shared/sdg.js +++ b/schemas/documents/shared/sdg.js @@ -1,4 +1,5 @@ import slugifyToIRI from '../../../utils/slugifyToIRI'; +import isUniqueSlug from '../../../utils/isUniqueSlug'; export default { title: 'SDG', @@ -19,6 +20,7 @@ export default { validation: Rule => Rule.required(), options: { source: 'title', + isUnique: isUniqueSlug, slugify: input => { return slugifyToIRI(input); }, diff --git a/utils/isUniqueSlug.js b/utils/isUniqueSlug.js new file mode 100644 index 0000000..2286d49 --- /dev/null +++ b/utils/isUniqueSlug.js @@ -0,0 +1,21 @@ +// Custom unique function for iri slugs that only checks the documents of the same type and the same language +export default function isUniqueSlug(slug, context) { + const { document, getClient } = context + const docType = document._type; + const language = document.language || 'en'; + + const query = ` + *[_type == $docType && language == $language && iri.current == $iri && _id != $id][0] + `; + + const params = { + docType, + language, + iri: slug, + id: document._id, + }; + + return getClient({ apiVersion: '2023-01-01' }) + .fetch(query, params) + .then((result) => !result); +} \ No newline at end of file