mirror of
https://github.com/strapi/strapi.git
synced 2025-09-08 16:16:21 +00:00
Add alreadyUse and forbidden content type check on name
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
parent
a0c87af098
commit
d67d731ea9
@ -1,7 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
// contentTypes and components reserved names
|
// contentTypes and components reserved names
|
||||||
const RESERVED_MODEL_NAMES = ['admin'];
|
const RESERVED_MODEL_NAMES = ['admin', 'boolean', 'date', 'date-time', 'time', 'upload'];
|
||||||
// attribute reserved names
|
// attribute reserved names
|
||||||
const RESERVED_ATTRIBUTE_NAMES = ['_id', 'id', 'length', 'attributes', 'relations', 'changed'];
|
const RESERVED_ATTRIBUTE_NAMES = ['_id', 'id', 'length', 'attributes', 'relations', 'changed'];
|
||||||
|
|
||||||
|
@ -122,7 +122,7 @@ class DatabaseManager {
|
|||||||
|
|
||||||
getReservedNames() {
|
getReservedNames() {
|
||||||
return {
|
return {
|
||||||
model: constants.RESERVED_MODEL_NAMES,
|
models: constants.RESERVED_MODEL_NAMES,
|
||||||
attributes: [
|
attributes: [
|
||||||
...constants.RESERVED_ATTRIBUTE_NAMES,
|
...constants.RESERVED_ATTRIBUTE_NAMES,
|
||||||
...(strapi.db.connectors.default.defaultTimestamps || []),
|
...(strapi.db.connectors.default.defaultTimestamps || []),
|
||||||
|
@ -2,6 +2,7 @@ const { validateKind, validateUpdateContentTypeInput } = require('../content-typ
|
|||||||
|
|
||||||
describe('Content type validator', () => {
|
describe('Content type validator', () => {
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
|
contentTypes: {},
|
||||||
plugins: {
|
plugins: {
|
||||||
'content-type-builder': {
|
'content-type-builder': {
|
||||||
services: {
|
services: {
|
||||||
|
@ -8,6 +8,7 @@ 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
|
||||||
@ -38,6 +39,13 @@ const createContentTypeSchema = data => {
|
|||||||
|
|
||||||
const contentTypeSchema = createSchema(VALID_TYPES, VALID_RELATIONS[kind] || [], {
|
const contentTypeSchema = createSchema(VALID_TYPES, VALID_RELATIONS[kind] || [], {
|
||||||
modelType: modelTypes.CONTENT_TYPE,
|
modelType: modelTypes.CONTENT_TYPE,
|
||||||
|
}).shape({
|
||||||
|
name: yup
|
||||||
|
.string()
|
||||||
|
.test(alreadyUsedContentTypeName())
|
||||||
|
.test(forbiddenContentTypeNameValidator())
|
||||||
|
.min(1)
|
||||||
|
.required(),
|
||||||
});
|
});
|
||||||
|
|
||||||
return yup
|
return yup
|
||||||
@ -86,6 +94,37 @@ const validateUpdateContentTypeInput = data => {
|
|||||||
.catch(error => Promise.reject(formatYupErrors(error)));
|
.catch(error => Promise.reject(formatYupErrors(error)));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const forbiddenContentTypeNameValidator = () => {
|
||||||
|
const reservedNames = strapi.plugins['content-type-builder'].services.builder.getReservedNames()
|
||||||
|
.models;
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: 'forbiddenContentTypeName',
|
||||||
|
message: `Content Type name cannot be one of ${reservedNames.join(', ')}`,
|
||||||
|
test: value => {
|
||||||
|
if (reservedNames.includes(nameToSlug(value))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
const alreadyUsedContentTypeName = () => {
|
||||||
|
const usedNames = Object.values(strapi.contentTypes).map(ct => ct.modelName);
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: 'nameAlreadyUsed',
|
||||||
|
message: 'Content Type name `${value}` is already being used.',
|
||||||
|
test: value => {
|
||||||
|
if (usedNames.includes(nameToSlug(value))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates type kind
|
* Validates type kind
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user