Skip to content

Commit

Permalink
Merge pull request #161 from canopas/fix-update-form-author-issue
Browse files Browse the repository at this point in the history
Fix author validation issue
  • Loading branch information
cp-dharti-r committed Jun 27, 2024
2 parents 326345d + 08b4a55 commit d037e0a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
"strapi": {
"uuid": "b6b5fb58-ed76-44ab-8910-3857abbf3b9f"
}
}
}
40 changes: 32 additions & 8 deletions admin/src/api/post/content-types/post/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const puppeteer = require("puppeteer");

module.exports = {
async beforeCreate(event) {
await modifyContentAndSetErrorMsg(event);
await modifyContentAndSetErrorMsg(event, false);
},

async beforeUpdate(event) {
await modifyContentAndSetErrorMsg(event);
await modifyContentAndSetErrorMsg(event, true);
},

async afterUpdate(event) {
Expand Down Expand Up @@ -61,12 +61,12 @@ module.exports = {
},
};

async function modifyContentAndSetErrorMsg(event) {
async function modifyContentAndSetErrorMsg(event, is_from_update) {
const result = event.params.data;

if (result) {
// validate input fields
validateFields(result);
validateFields(result, is_from_update);

// generate table of contents
await generateTOC(result, event);
Expand Down Expand Up @@ -146,58 +146,82 @@ function generateRandomNumber() {
return Math.floor(Math.random() * 9000000000) + 1000000000;
}

function validateFields(result) {
// set required message for summary,tags and meta_description
function validateFields(result, is_from_update) {
// no need to validate if it's only going to publish or unpublish
if (Object.keys(result).length <= 3) {
return;
}

if (!result.title) {
const error = new YupValidationError({
path: "title",
message: "This value is required.",
});
throw error;
}

if (result.tags && result.tags.length == 0) {
const error = new YupValidationError({
path: "tags",
message: "This value is required.",
});
throw error;
}
if (result.author.connect.length == 0) {

// required author when add post
if (!is_from_update && result.author.connect.length == 0) {
const error = new YupValidationError({
path: "author",
message: "This value is required.",
});
throw error;
}
// required author when update post
if (
is_from_update &&
result.author.disconnect.length > 0 &&
result.author.connect.length == 0
) {
const error = new YupValidationError({
path: "author",
message: "This value is required.",
});
throw error;
}

if (!result.summary) {
const error = new YupValidationError({
path: "summary",
message: "This value is required.",
});
throw error;
}

if (result.summary && result.summary.length > 200) {
const error = new YupValidationError({
path: "summary",
message: "Allow max 200 chars only",
});
throw error;
}

if (!result.meta_description) {
const error = new YupValidationError({
path: "meta_description",
message: "This value is required.",
});
throw error;
}

if (result.meta_description && result.meta_description.length > 160) {
const error = new YupValidationError({
path: "meta_description",
message: "Allow max 160 chars only",
});
throw error;
}
if (result.blog_content == '') {

if (result.blog_content == "") {
const error = new YupValidationError({
path: "blog_content",
message: "This value is required.",
Expand Down

0 comments on commit d037e0a

Please sign in to comment.