Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix author validation issue #161

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
cp-dharti-r marked this conversation as resolved.
Show resolved Hide resolved
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) {
cp-dharti-r marked this conversation as resolved.
Show resolved Hide resolved
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
Loading