fix required attributes on post requests

This commit is contained in:
Mark Kaylor 2022-05-16 10:00:20 +02:00
parent ef8449f693
commit d413a018b5

View File

@ -36,23 +36,24 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
];
const attributesForRequest = _.omit(attributes, attributesToOmit);
const requiredAttributes = Object.entries(attributesForRequest)
.filter(([, attribute]) => attribute.required)
.map(([attributeName, attribute]) => {
return { [attributeName]: attribute };
});
// Get a list of required attribute names
const requiredAttributes = Object.entries(attributesForRequest).reduce((acc, attribute) => {
const [attributeKey, attributeValue] = attribute;
const requestAttributes =
routeMethods.includes('POST') && requiredAttributes.length
? Object.assign({}, ...requiredAttributes)
: attributesForRequest;
if (attributeValue.required) {
acc.push(attributeKey);
}
return acc;
}, []);
if (hasLocalizationPath) {
schemas = {
...schemas,
[`${pascalCase(uniqueName)}LocalizationRequest`]: {
required: requiredAttributes,
type: 'object',
properties: cleanSchemaAttributes(requestAttributes, { isRequest: true }),
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true }),
},
};
}
@ -62,10 +63,12 @@ const getAllSchemasForContentType = ({ routeInfo, attributes, uniqueName }) => {
...schemas,
[`${pascalCase(uniqueName)}Request`]: {
type: 'object',
required: ['data'],
properties: {
data: {
required: requiredAttributes,
type: 'object',
properties: cleanSchemaAttributes(requestAttributes, { isRequest: true }),
properties: cleanSchemaAttributes(attributesForRequest, { isRequest: true }),
},
},
},