Skip to content

Commit

Permalink
Merge pull request #104 from bcgov/bugfix/v0.3
Browse files Browse the repository at this point in the history
Improved roadmap validation
  • Loading branch information
kyle1morel authored Jun 27, 2024
2 parents 9969488 + 6d1f0f2 commit 851cac0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
33 changes: 20 additions & 13 deletions frontend/src/components/roadmap/Roadmap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { roadmapService, userService } from '@/services';
import { useConfigStore, useSubmissionStore, useTypeStore } from '@/store';
import { PermitNeeded, PermitStatus } from '@/utils/enums/housing';
import { roadmapTemplate } from '@/utils/templates';
import { delimitEmails } from '@/utils/utils';
import { delimitEmails, setEmptyStringsToNull } from '@/utils/utils';
import type { Ref } from 'vue';
import type { Document } from '@/types';
Expand All @@ -36,19 +36,26 @@ const initialFormValues: Ref<any> = ref();
const selectedFiles: Ref<Array<Document>> = ref([]);
// Form schema
const emailValidator = array()
.transform(function (value, originalValue) {
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? delimitEmails(originalValue) : [];
})
.of(string().email(({ value }) => `${value} is not a valid email`));
const emailValidator = (min: number, message: string) => {
return array()
.transform(function (value, originalValue) {
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? delimitEmails(originalValue) : [];
})
.min(min, message)
.of(
string()
.trim()
.email(({ value }) => `${value} is not a valid email`)
);
};
const formSchema = object({
to: emailValidator,
cc: emailValidator,
bcc: emailValidator,
to: emailValidator(1, 'To is required'),
cc: emailValidator(0, 'CC is not required'),
bcc: emailValidator(1, 'BCC is required'),
subject: string().required(),
body: string().required()
});
Expand All @@ -68,7 +75,7 @@ const confirmSubmit = (data: any) => {
await roadmapService.send(
props.activityId,
selectedFiles.value.map((x: Document) => x.documentId),
data
setEmptyStringsToNull(data)
);
toast.success('Roadmap sent');
} catch (e: any) {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/services/roadmapService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default {
if (emailData.cc && !Array.isArray(emailData.cc)) {
emailData.cc = delimitEmails(emailData.cc);
}
if (!emailData.cc) emailData.cc = [];
if (emailData.bcc && !Array.isArray(emailData.bcc)) {
emailData.bcc = delimitEmails(emailData.bcc);
}
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export function deepToRaw<T extends Record<string, any>>(sourceObj: T): T {
* @returns {string[]} An array of string values
*/
export function delimitEmails(value: string): Array<string> {
return value.split(/[\s;,]+/g).map((s) => s.trim());
return value
.split(/[\s;,]+/g)
.map((s) => s.trim())
.filter((s) => !!s);
}

/**
Expand Down

0 comments on commit 851cac0

Please sign in to comment.