Merge pull request #6097 from strapi/chore/ctb-verify-pluralized-ct-name

Check pluralize name in ctb
This commit is contained in:
Alexandre BODIN 2020-05-07 13:00:08 +02:00 committed by GitHub
commit 75bce78f95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -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 = {

View File

@ -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);