mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 00:39:49 +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', () => {
|
describe('Content type validator', () => {
|
||||||
global.strapi = {
|
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', () => {
|
describe('validateUpdateContentTypeInput', () => {
|
||||||
test('Deletes empty defaults', async () => {
|
test('Deletes empty defaults', async () => {
|
||||||
const data = {
|
const data = {
|
||||||
|
@ -2,13 +2,13 @@
|
|||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const yup = require('yup');
|
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 createSchema = require('./model-schema');
|
||||||
const { removeEmptyDefaults, removeDeletedUIDTargetFields } = require('./data-transform');
|
const { removeEmptyDefaults, removeDeletedUIDTargetFields } = require('./data-transform');
|
||||||
const { nestedComponentSchema } = require('./component');
|
const { nestedComponentSchema } = require('./component');
|
||||||
const { modelTypes, DEFAULT_TYPES, typeKinds } = require('./constants');
|
const { modelTypes, DEFAULT_TYPES, typeKinds } = require('./constants');
|
||||||
const { nameToSlug } = require('strapi-utils');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allowed relation per type kind
|
* Allowed relation per type kind
|
||||||
@ -42,6 +42,7 @@ const createContentTypeSchema = (data, { isEdition = false } = {}) => {
|
|||||||
}).shape({
|
}).shape({
|
||||||
name: yup
|
name: yup
|
||||||
.string()
|
.string()
|
||||||
|
.test(hasPluralName)
|
||||||
.test(alreadyUsedContentTypeName(isEdition))
|
.test(alreadyUsedContentTypeName(isEdition))
|
||||||
.test(forbiddenContentTypeNameValidator())
|
.test(forbiddenContentTypeNameValidator())
|
||||||
.min(1)
|
.min(1)
|
||||||
@ -105,11 +106,25 @@ const forbiddenContentTypeNameValidator = () => {
|
|||||||
if (reservedNames.includes(nameToSlug(value))) {
|
if (reservedNames.includes(nameToSlug(value))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
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 alreadyUsedContentTypeName = isEdition => {
|
||||||
const usedNames = Object.values(strapi.contentTypes).map(ct => ct.modelName);
|
const usedNames = Object.values(strapi.contentTypes).map(ct => ct.modelName);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user