mirror of
https://github.com/strapi/strapi.git
synced 2025-12-24 05:34:33 +00:00
Improve error when association fail and handle configurable key in models
This commit is contained in:
parent
6d8b81ecbd
commit
92c891e341
@ -55,4 +55,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -46,4 +46,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -43,4 +43,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -43,4 +43,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -43,4 +43,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -47,4 +47,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -44,4 +44,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -43,4 +43,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -43,4 +43,4 @@
|
||||
"npm": ">= 5.3.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
@ -40,7 +40,7 @@ module.exports = {
|
||||
if (strapi.models[name]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.exist' }] }]);
|
||||
if (!_.isNaN(parseFloat(name[0]))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.name' }] }]);
|
||||
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes);
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes, name, plugin);
|
||||
|
||||
if (!_.isEmpty(attributesErrors)) {
|
||||
return ctx.badRequest(null, [{ messages: attributesErrors }]);
|
||||
@ -95,7 +95,7 @@ module.exports = {
|
||||
if (plugin && !strapi.plugins[_.toLower(plugin)]) return ctx.badRequest(null, [{ message: [{ id: 'request.error.plugin.name' }] }]);
|
||||
if (plugin && !strapi.plugins[_.toLower(plugin)].models[_.toLower(model)]) return ctx.badRequest(null, [{ message: [{ id: 'request.error.model.unknow' }] }]);
|
||||
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes);
|
||||
const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes, name.toLowerCase(), plugin);
|
||||
|
||||
if (!_.isEmpty(attributesErrors)) {
|
||||
return ctx.badRequest(null, [{ messages: attributesErrors }]);
|
||||
|
||||
@ -118,11 +118,26 @@ module.exports = {
|
||||
path.resolve(strapi.config.appPath, 'api', target, 'models', filename);
|
||||
},
|
||||
|
||||
formatAttributes: attributes => {
|
||||
formatAttributes: (attributes, name, plugin) => {
|
||||
const errors = [];
|
||||
const attrs = {};
|
||||
|
||||
_.forEach(attributes, attribute => {
|
||||
const target = Object.keys((plugin ? strapi.plugins : strapi.api) || {})
|
||||
.filter(x => _.includes(Object.keys((plugin ? strapi.plugins : strapi.api)[x].models), name))[0];
|
||||
|
||||
const model = plugin ? strapi.plugins[target].models[name] : strapi.api[target].models[name];
|
||||
|
||||
// Only select configurable attributes.
|
||||
const attributesConfigurable = attributes.filter(attribute => _.get(model.attributes, [attribute.name, 'configurable'], true) !== false);
|
||||
const attributesNotConfigurable = Object.keys(model.attributes)
|
||||
.filter(attribute => _.get(model.attributes, [attribute, 'configurable'], true) === false)
|
||||
.reduce((acc, attribute) => {
|
||||
acc[attribute] = model.attributes[attribute];
|
||||
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
_.forEach(attributesConfigurable, attribute => {
|
||||
if (_.has(attribute, 'params.type')) {
|
||||
attrs[attribute.name] = attribute.params;
|
||||
} else if (_.has(attribute, 'params.target')) {
|
||||
@ -162,7 +177,9 @@ module.exports = {
|
||||
}
|
||||
});
|
||||
|
||||
return [attrs, errors];
|
||||
Object.assign(attributesNotConfigurable, attrs);
|
||||
|
||||
return [attributesNotConfigurable, errors];
|
||||
},
|
||||
|
||||
clearRelations: (model, source) => {
|
||||
|
||||
@ -8,35 +8,31 @@
|
||||
"username": {
|
||||
"type": "string",
|
||||
"minLength": 3,
|
||||
"unique": true
|
||||
"unique": true,
|
||||
"configurable": false
|
||||
},
|
||||
"email": {
|
||||
"type": "email",
|
||||
"minLength": 6,
|
||||
"unique": true
|
||||
"unique": true,
|
||||
"configurable": false
|
||||
},
|
||||
"provider": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"configurable": false
|
||||
},
|
||||
"password": {
|
||||
"type": "password",
|
||||
"minLength": 6
|
||||
"minLength": 6,
|
||||
"configurable": false
|
||||
},
|
||||
"resetPasswordToken": {
|
||||
"type": "string"
|
||||
"type": "string",
|
||||
"configurable": false
|
||||
},
|
||||
"role": {
|
||||
"type": "integer"
|
||||
},
|
||||
"posts": {
|
||||
"collection": "post",
|
||||
"via": "authors"
|
||||
},
|
||||
"children": {
|
||||
"collection": "user",
|
||||
"via": "children",
|
||||
"dominant": true,
|
||||
"plugin": "users-permissions"
|
||||
"type": "integer",
|
||||
"configurable": false
|
||||
}
|
||||
},
|
||||
"connection": "default"
|
||||
|
||||
@ -225,43 +225,48 @@ module.exports = {
|
||||
*/
|
||||
|
||||
defineAssociations: function (model, definition, association, key) {
|
||||
// Initialize associations object
|
||||
if (definition.associations === undefined) {
|
||||
definition.associations = [];
|
||||
}
|
||||
try {
|
||||
// Initialize associations object
|
||||
if (definition.associations === undefined) {
|
||||
definition.associations = [];
|
||||
}
|
||||
|
||||
// Exclude non-relational attribute
|
||||
if (!association.hasOwnProperty('collection') && !association.hasOwnProperty('model')) {
|
||||
return undefined;
|
||||
}
|
||||
// Exclude non-relational attribute
|
||||
if (!association.hasOwnProperty('collection') && !association.hasOwnProperty('model')) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// Get relation nature
|
||||
const infos = this.getNature(association, key, undefined, model.toLowerCase());
|
||||
const details = _.get(strapi.models, `${association.model || association.collection}.attributes.${association.via}`, {});
|
||||
// Get relation nature
|
||||
const infos = this.getNature(association, key, undefined, model.toLowerCase());
|
||||
const details = _.get(strapi.models, `${association.model || association.collection}.attributes.${association.via}`, {});
|
||||
|
||||
// Build associations object
|
||||
if (association.hasOwnProperty('collection')) {
|
||||
definition.associations.push({
|
||||
alias: key,
|
||||
type: 'collection',
|
||||
collection: association.collection,
|
||||
via: association.via || undefined,
|
||||
nature: infos.nature,
|
||||
autoPopulate: _.get(association, 'autoPopulate', true),
|
||||
dominant: details.dominant !== true,
|
||||
plugin: association.plugin || undefined,
|
||||
});
|
||||
} else if (association.hasOwnProperty('model')) {
|
||||
definition.associations.push({
|
||||
alias: key,
|
||||
type: 'model',
|
||||
model: association.model,
|
||||
via: association.via || undefined,
|
||||
nature: infos.nature,
|
||||
autoPopulate: _.get(association, 'autoPopulate', true),
|
||||
dominant: details.dominant !== true,
|
||||
plugin: association.plugin || undefined,
|
||||
});
|
||||
// Build associations object
|
||||
if (association.hasOwnProperty('collection')) {
|
||||
definition.associations.push({
|
||||
alias: key,
|
||||
type: 'collection',
|
||||
collection: association.collection,
|
||||
via: association.via || undefined,
|
||||
nature: infos.nature,
|
||||
autoPopulate: _.get(association, 'autoPopulate', true),
|
||||
dominant: details.dominant !== true,
|
||||
plugin: association.plugin || undefined,
|
||||
});
|
||||
} else if (association.hasOwnProperty('model')) {
|
||||
definition.associations.push({
|
||||
alias: key,
|
||||
type: 'model',
|
||||
model: association.model,
|
||||
via: association.via || undefined,
|
||||
nature: infos.nature,
|
||||
autoPopulate: _.get(association, 'autoPopulate', true),
|
||||
dominant: details.dominant !== true,
|
||||
plugin: association.plugin || undefined,
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
strapi.log.error(`Something went wrong in the model \`${_.upperFirst(model)}\` with the attribute \`${key}\``);
|
||||
strapi.stop();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user