feat: Validate newsfeed post and comment with zod (#221)
* added Zod validation to the newsfeed post and comment * updated the error message
This commit is contained in:
@@ -11,6 +11,8 @@
|
|||||||
NOTIFICATION_NAME,
|
NOTIFICATION_NAME,
|
||||||
triggerSendEmail
|
triggerSendEmail
|
||||||
} from '$lib/utils/services/notification/notification';
|
} from '$lib/utils/services/notification/notification';
|
||||||
|
import { createNewsfeedValidation } from '$lib/utils/functions/validator';
|
||||||
|
import { getTextFromHTML } from '$lib/utils/functions/toHtml';
|
||||||
|
|
||||||
export let author: Author | any = {};
|
export let author: Author | any = {};
|
||||||
export let courseId = '';
|
export let courseId = '';
|
||||||
@@ -22,9 +24,14 @@
|
|||||||
let newPost = '';
|
let newPost = '';
|
||||||
let isLoading = false;
|
let isLoading = false;
|
||||||
let createdFeed;
|
let createdFeed;
|
||||||
|
let errors = {};
|
||||||
|
|
||||||
const onPost = async () => {
|
const onPost = async () => {
|
||||||
if (!newPost) return;
|
errors = createNewsfeedValidation(getTextFromHTML(newPost));
|
||||||
|
|
||||||
|
if (Object.keys(errors).length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
isLoading = true;
|
isLoading = true;
|
||||||
try {
|
try {
|
||||||
if (edit) {
|
if (edit) {
|
||||||
@@ -105,6 +112,9 @@
|
|||||||
placeholder="Share an update with your students"
|
placeholder="Share an update with your students"
|
||||||
maxHeight={400}
|
maxHeight={400}
|
||||||
/>
|
/>
|
||||||
|
{#if errors.newPost}
|
||||||
|
<p class="text-sm text-red-500">{errors.newPost}</p>
|
||||||
|
{/if}
|
||||||
<div class="flex items-center justify-end py-2">
|
<div class="flex items-center justify-end py-2">
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
<PrimaryButton label="Cancel" variant={VARIANTS.OUTLINED} onClick={resetEditor} />
|
<PrimaryButton label="Cancel" variant={VARIANTS.OUTLINED} onClick={resetEditor} />
|
||||||
|
|||||||
@@ -14,6 +14,7 @@
|
|||||||
import { isHtmlValueEmpty } from '$lib/utils/functions/toHtml';
|
import { isHtmlValueEmpty } from '$lib/utils/functions/toHtml';
|
||||||
import pluralize from 'pluralize';
|
import pluralize from 'pluralize';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
import { addNewsfeedCommentValidation } from '$lib/utils/functions/validator';
|
||||||
|
|
||||||
export let feed: Feed;
|
export let feed: Feed;
|
||||||
export let editFeed: Feed;
|
export let editFeed: Feed;
|
||||||
@@ -29,6 +30,7 @@
|
|||||||
let comment = '';
|
let comment = '';
|
||||||
let areCommentsExpanded = false;
|
let areCommentsExpanded = false;
|
||||||
let isDeleteFeedModal = false;
|
let isDeleteFeedModal = false;
|
||||||
|
let errors = {};
|
||||||
|
|
||||||
let reactions = {
|
let reactions = {
|
||||||
smile: '😀',
|
smile: '😀',
|
||||||
@@ -55,9 +57,11 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleAddNewComment = (event) => {
|
const handleAddNewComment = (event) => {
|
||||||
if (!comment) return;
|
|
||||||
|
|
||||||
if (event.key === 'Enter' || event.type === 'click') {
|
if (event.key === 'Enter' || event.type === 'click') {
|
||||||
|
errors = addNewsfeedCommentValidation(comment);
|
||||||
|
if (Object.keys(errors).length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
addNewComment(comment, feed.id, author.id);
|
addNewComment(comment, feed.id, author.id);
|
||||||
comment = '';
|
comment = '';
|
||||||
@@ -226,6 +230,9 @@
|
|||||||
<Send size={24} />
|
<Send size={24} />
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
{#if errors.newComment}
|
||||||
|
<p class="text-sm text-red-500">{errors.newComment}</p>
|
||||||
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
<DeleteFeedConfirmation bind:openDeleteModal={isDeleteFeedModal} deleteFeed={handleDeleteFeed} />
|
<DeleteFeedConfirmation bind:openDeleteModal={isDeleteFeedModal} deleteFeed={handleDeleteFeed} />
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -4,9 +4,13 @@ import { validateEmail } from './validateEmail';
|
|||||||
|
|
||||||
const getOrgNameValidation = () =>
|
const getOrgNameValidation = () =>
|
||||||
z.string().min(5, { message: 'Organization must contain 5 or more characters' });
|
z.string().min(5, { message: 'Organization must contain 5 or more characters' });
|
||||||
|
|
||||||
const getSiteNameValidation = () =>
|
const getSiteNameValidation = () =>
|
||||||
z.string().min(5, { message: 'Site name must contain 5 or more characters' });
|
z.string().min(5, { message: 'Site name must contain 5 or more characters' });
|
||||||
|
|
||||||
|
const getNewsfeedValidation = () =>
|
||||||
|
z.string().min(5, { message: 'Field must contain 5 or more characters' });
|
||||||
|
|
||||||
const createQuizValidationSchema = z.object({
|
const createQuizValidationSchema = z.object({
|
||||||
title: z.string().min(6, {
|
title: z.string().min(6, {
|
||||||
message: 'Must be 6 or more characters long',
|
message: 'Must be 6 or more characters long',
|
||||||
@@ -199,6 +203,24 @@ export const onboardingValidation = (fields = {}, step) => {
|
|||||||
// return processErrors(error);
|
// return processErrors(error);
|
||||||
// };
|
// };
|
||||||
|
|
||||||
|
export const createNewsfeedValidation = (newPost) => {
|
||||||
|
const schema = z.object({
|
||||||
|
newPost: getNewsfeedValidation()
|
||||||
|
});
|
||||||
|
const { error } = schema.safeParse({ newPost });
|
||||||
|
|
||||||
|
return processErrors(error);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const addNewsfeedCommentValidation = (newComment) => {
|
||||||
|
const schema = z.object({
|
||||||
|
newComment: getNewsfeedValidation()
|
||||||
|
});
|
||||||
|
const { error } = schema.safeParse({ newComment });
|
||||||
|
|
||||||
|
return processErrors(error);
|
||||||
|
};
|
||||||
|
|
||||||
export const createOrgValidation = (fields = {}) => {
|
export const createOrgValidation = (fields = {}) => {
|
||||||
const schema = z.object({
|
const schema = z.object({
|
||||||
orgName: getOrgNameValidation(),
|
orgName: getOrgNameValidation(),
|
||||||
|
|||||||
Reference in New Issue
Block a user