mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 08:19:07 +00:00
Merge pull request #6097 from strapi/chore/ctb-verify-pluralized-ct-name
Check pluralize name in ctb
This commit is contained in:
commit
75bce78f95
@ -1,4 +1,8 @@
|
||||
const { validateKind, validateUpdateContentTypeInput } = require('../content-type');
|
||||
const {
|
||||
validateKind,
|
||||
validateUpdateContentTypeInput,
|
||||
validateContentTypeInput,
|
||||
} = require('../content-type');
|
||||
|
||||
describe('Content type validator', () => {
|
||||
global.strapi = {
|
||||
@ -58,6 +62,27 @@ describe('Content type validator', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Prevents use of names without plural form', () => {
|
||||
test('Throws when using name without plural form', async () => {
|
||||
const data = {
|
||||
contentType: {
|
||||
name: 'news',
|
||||
attributes: {
|
||||
title: {
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
await validateContentTypeInput(data).catch(err => {
|
||||
expect(err).toMatchObject({
|
||||
'contentType.name': [expect.stringMatching('cannot be pluralized')],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateUpdateContentTypeInput', () => {
|
||||
test('Deletes empty defaults', async () => {
|
||||
const data = {
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
const _ = require('lodash');
|
||||
const yup = require('yup');
|
||||
const { formatYupErrors } = require('strapi-utils');
|
||||
const { formatYupErrors, nameToSlug } = require('strapi-utils');
|
||||
const pluralize = require('pluralize');
|
||||
|
||||
const createSchema = require('./model-schema');
|
||||
const { removeEmptyDefaults, removeDeletedUIDTargetFields } = require('./data-transform');
|
||||
const { nestedComponentSchema } = require('./component');
|
||||
const { modelTypes, DEFAULT_TYPES, typeKinds } = require('./constants');
|
||||
const { nameToSlug } = require('strapi-utils');
|
||||
|
||||
/**
|
||||
* Allowed relation per type kind
|
||||
@ -42,6 +42,7 @@ const createContentTypeSchema = (data, { isEdition = false } = {}) => {
|
||||
}).shape({
|
||||
name: yup
|
||||
.string()
|
||||
.test(hasPluralName)
|
||||
.test(alreadyUsedContentTypeName(isEdition))
|
||||
.test(forbiddenContentTypeNameValidator())
|
||||
.min(1)
|
||||
@ -105,11 +106,25 @@ const forbiddenContentTypeNameValidator = () => {
|
||||
if (reservedNames.includes(nameToSlug(value))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
const hasPluralName = {
|
||||
name: 'hasPluralName',
|
||||
message:
|
||||
'Content Type name `${value}` cannot be pluralized. \nSuggestion: add Item after the name (e.g News -> NewsItem).',
|
||||
test: value => {
|
||||
if (pluralize.singular(value) === pluralize(value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
};
|
||||
|
||||
const alreadyUsedContentTypeName = isEdition => {
|
||||
const usedNames = Object.values(strapi.contentTypes).map(ct => ct.modelName);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user