mirror of
https://github.com/strapi/strapi.git
synced 2025-09-04 14:23:03 +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';
|
||||
|
||||
// contentTypes and components reserved names
|
||||
const RESERVED_MODEL_NAMES = ['admin'];
|
||||
const RESERVED_MODEL_NAMES = ['admin', 'boolean', 'date', 'date-time', 'time', 'upload'];
|
||||
// attribute reserved names
|
||||
const RESERVED_ATTRIBUTE_NAMES = ['_id', 'id', 'length', 'attributes', 'relations', 'changed'];
|
||||
|
||||
|
@ -122,7 +122,7 @@ class DatabaseManager {
|
||||
|
||||
getReservedNames() {
|
||||
return {
|
||||
model: constants.RESERVED_MODEL_NAMES,
|
||||
models: constants.RESERVED_MODEL_NAMES,
|
||||
attributes: [
|
||||
...constants.RESERVED_ATTRIBUTE_NAMES,
|
||||
...(strapi.db.connectors.default.defaultTimestamps || []),
|
||||
|
@ -2,6 +2,7 @@ const { validateKind, validateUpdateContentTypeInput } = require('../content-typ
|
||||
|
||||
describe('Content type validator', () => {
|
||||
global.strapi = {
|
||||
contentTypes: {},
|
||||
plugins: {
|
||||
'content-type-builder': {
|
||||
services: {
|
||||
|
@ -8,6 +8,7 @@ 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
|
||||
@ -38,6 +39,13 @@ const createContentTypeSchema = data => {
|
||||
|
||||
const contentTypeSchema = createSchema(VALID_TYPES, VALID_RELATIONS[kind] || [], {
|
||||
modelType: modelTypes.CONTENT_TYPE,
|
||||
}).shape({
|
||||
name: yup
|
||||
.string()
|
||||
.test(alreadyUsedContentTypeName())
|
||||
.test(forbiddenContentTypeNameValidator())
|
||||
.min(1)
|
||||
.required(),
|
||||
});
|
||||
|
||||
return yup
|
||||
@ -86,6 +94,37 @@ const validateUpdateContentTypeInput = data => {
|
||||
.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
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user