mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 23:57:32 +00:00
Move logic from services to controllers & entity validator
This commit is contained in:
parent
749eefda96
commit
99e2b241e4
@ -1,5 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const { isObject } = require('lodash/fp');
|
||||
const { ValidationError } = require('@strapi/utils').errors;
|
||||
|
||||
const { parseBody } = require('./transform');
|
||||
|
||||
/**
|
||||
@ -51,6 +54,11 @@ const createCollectionTypeController = ({
|
||||
const { query } = ctx.request;
|
||||
|
||||
const { data, files } = parseBody(ctx);
|
||||
|
||||
if (!isObject(data)) {
|
||||
throw new ValidationError('Missing "data" payload in the request body');
|
||||
}
|
||||
|
||||
const sanitizedInputData = await sanitizeInput(data, ctx);
|
||||
|
||||
const entity = await service.create({ ...query, data: sanitizedInputData, files });
|
||||
@ -69,6 +77,11 @@ const createCollectionTypeController = ({
|
||||
const { query } = ctx.request;
|
||||
|
||||
const { data, files } = parseBody(ctx);
|
||||
|
||||
if (!isObject(data)) {
|
||||
throw new ValidationError('Missing "data" payload in the request body');
|
||||
}
|
||||
|
||||
const sanitizedInputData = await sanitizeInput(data, ctx);
|
||||
|
||||
const entity = await service.update(id, { ...query, data: sanitizedInputData, files });
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
const { isObject } = require('lodash/fp');
|
||||
const { ValidationError } = require('@strapi/utils').errors;
|
||||
|
||||
const { parseBody } = require('./transform');
|
||||
|
||||
/**
|
||||
@ -34,6 +37,11 @@ const createSingleTypeController = ({
|
||||
async update(ctx) {
|
||||
const { query } = ctx.request;
|
||||
const { data, files } = parseBody(ctx);
|
||||
|
||||
if (!isObject(data)) {
|
||||
throw new ValidationError('Missing "data" payload in the request body');
|
||||
}
|
||||
|
||||
const sanitizedInputData = await sanitizeInput(data, ctx);
|
||||
|
||||
const entity = await service.createOrUpdate({ ...query, data: sanitizedInputData, files });
|
||||
|
||||
@ -1,12 +1,11 @@
|
||||
'use strict';
|
||||
|
||||
const { propOr, isObject } = require('lodash/fp');
|
||||
const { propOr } = require('lodash/fp');
|
||||
|
||||
const {
|
||||
hasDraftAndPublish,
|
||||
constants: { PUBLISHED_AT_ATTRIBUTE },
|
||||
} = require('@strapi/utils').contentTypes;
|
||||
const { ValidationError } = require('@strapi/utils').errors;
|
||||
|
||||
const {
|
||||
getPaginationInfo,
|
||||
@ -61,10 +60,6 @@ const createCollectionTypeService = ({ model, strapi, utils }) => {
|
||||
create(params = {}) {
|
||||
const { data } = params;
|
||||
|
||||
if (!isObject(data)) {
|
||||
throw new ValidationError(`Expecting body.data to be an object but found '${typeof data}'`);
|
||||
}
|
||||
|
||||
if (hasDraftAndPublish(model)) {
|
||||
setPublishedAt(data);
|
||||
}
|
||||
@ -75,10 +70,6 @@ const createCollectionTypeService = ({ model, strapi, utils }) => {
|
||||
update(entityId, params = {}) {
|
||||
const { data } = params;
|
||||
|
||||
if (!isObject(data)) {
|
||||
throw new ValidationError(`Expecting body.data to be an object, found "${typeof data}"`);
|
||||
}
|
||||
|
||||
return strapi.entityService.update(uid, entityId, { ...params, data });
|
||||
},
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const { isObject } = require('lodash/fp');
|
||||
const { ValidationError } = require('@strapi/utils').errors;
|
||||
|
||||
/**
|
||||
@ -28,10 +27,6 @@ const createSingleTypeService = ({ model, strapi, utils }) => {
|
||||
async createOrUpdate({ data, ...params } = {}) {
|
||||
const entity = await this.find(params);
|
||||
|
||||
if (!isObject(data)) {
|
||||
throw new ValidationError(`Expecting body.data to be an object but found '${typeof data}'`);
|
||||
}
|
||||
|
||||
if (!entity) {
|
||||
const count = await strapi.query(uid).count();
|
||||
if (count >= 1) {
|
||||
|
||||
@ -4,12 +4,13 @@
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
const { has, assoc, prop } = require('lodash/fp');
|
||||
const { has, assoc, prop, isObject } = require('lodash/fp');
|
||||
const strapiUtils = require('@strapi/utils');
|
||||
const validators = require('./validators');
|
||||
|
||||
const { yup, validateYupSchema } = strapiUtils;
|
||||
const { isMediaAttribute, isScalarAttribute, getWritableAttributes } = strapiUtils.contentTypes;
|
||||
const { ValidationError } = strapiUtils.errors;
|
||||
|
||||
const addMinMax = (attr, validator, data) => {
|
||||
if (Number.isInteger(attr.min) && (attr.required || (Array.isArray(data) && data.length > 0))) {
|
||||
@ -173,6 +174,14 @@ const createModelValidator = createOrUpdate => (model, data, { isDraft }) => {
|
||||
};
|
||||
|
||||
const createValidateEntity = createOrUpdate => async (model, data, { isDraft = false } = {}) => {
|
||||
if (!isObject(data)) {
|
||||
const { displayName } = model.info;
|
||||
|
||||
throw new ValidationError(
|
||||
`Invalid payload submitted for the ${createOrUpdate} of an entity of type ${displayName}. Expected an object, but got ${typeof data}`
|
||||
);
|
||||
}
|
||||
|
||||
const validator = createModelValidator(createOrUpdate)(model, data, { isDraft }).required();
|
||||
return validateYupSchema(validator, { strict: false, abortEarly: false })(data);
|
||||
};
|
||||
|
||||
@ -60,6 +60,10 @@ const wrapParams = async (params = {}, ctx = {}) => {
|
||||
const assignValidLocale = async data => {
|
||||
const { getValidLocale } = getService('content-types');
|
||||
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
data.locale = await getValidLocale(data.locale);
|
||||
} catch (e) {
|
||||
@ -97,7 +101,7 @@ const decorator = service => ({
|
||||
* @param {string} uid - Model uid
|
||||
* @param {object} opts - Query options object (params, data, files, populate)
|
||||
*/
|
||||
async create(uid, opts) {
|
||||
async create(uid, opts = {}) {
|
||||
const model = strapi.getModel(uid);
|
||||
|
||||
const { syncLocalizations, syncNonLocalizedAttributes } = getService('localizations');
|
||||
@ -123,7 +127,7 @@ const decorator = service => ({
|
||||
* @param {string} entityId
|
||||
* @param {object} opts - Query options object (params, data, files, populate)
|
||||
*/
|
||||
async update(uid, entityId, opts) {
|
||||
async update(uid, entityId, opts = {}) {
|
||||
const model = strapi.getModel(uid);
|
||||
|
||||
const { syncNonLocalizedAttributes } = getService('localizations');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user