I am using "@strapi/strapi": "^4.12.5", and have to say that this is one amazing open-source CMS.
There are things missing, like the one I need help with and will explain below.
I have the following content-type named Block with the schema as below
{
"kind": "collectionType",
"collectionName": "blocks",
"info": {
"singularName": "block",
"pluralName": "blocks",
"displayName": "Block"
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {
"i18n": {
"localized": true
}
},
"attributes": {
"container": {
"pluginOptions": {
"i18n": {
"localized": false
}
},
"type": "string",
"maxLength": 512,
"required": true,
"unique": false
},
"key": {
"pluginOptions": {
"i18n": {
"localized": false
}
},
"type": "string",
"maxLength": 512,
"required": true
},
"sections": {
"pluginOptions": {
"i18n": {
"localized": true
}
},
"type": "dynamiczone",
"components": [
"sections.rich-text"
],
"required": true
}
}
}
I want to make sure that the combination of container-key-locale cannot be present 2 times, so that combination is unique.
Example of the desired functionality:
- user inserts for locale en container=home, key=footer, anyOtherDataDoesntMatter
- user tries to insert again for locale en, container=home, key=footer, whateverRestOfData. This operation should fail with proper validation error that "container-key-locale" is unique.
This is the default generated src/api/block/controllers/block.ts controller
/**
* block controller
*/
import { factories } from '@strapi/strapi';
export default factories.createCoreController('api::block.block');
What is the best approach to add this validation logic and show a meaningful message to the admin UI ?
With the code and schema that I shared there is no validation in place and the content editor can add duplicate configs like the one I want to avoid.
PS: Strapi does not allow combinated keys or unique key per locale yet out of the box via the UI.
