Skip to content

Commit

Permalink
Updated validation for message
Browse files Browse the repository at this point in the history
  • Loading branch information
DigneshGujarathi committed Jan 20, 2021
1 parent f6e8bbc commit 8a6d361
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/containers/Template/Form/Template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,6 @@ import { CREATE_MEDIA_MESSAGE } from '../../../graphql/mutations/Chat';
import { Checkbox } from '../../../components/UI/Form/Checkbox/Checkbox';
import { USER_LANGUAGES } from '../../../graphql/queries/Organization';

const validation = {
language: Yup.object().nullable().required('Language is required.'),
label: Yup.string().required('Title is required.').max(50, 'Title length is too long.'),
body: Yup.string()
.transform((current, original) => {
return original.getCurrentContent().getPlainText();
})
.required('Message is required.'),
type: Yup.object()
.nullable()
.when('attachmentURL', {
is: (val) => val && val !== '',
then: Yup.object().required('Type is required.'),
}),
attachmentURL: Yup.string()
.nullable()
.when('type', {
is: (val) => val && val.id,
then: Yup.string().required('Attachment URL is required.'),
}),
};

const HSMValidation = {
example: Yup.string()
.transform((current, original) => {
Expand Down Expand Up @@ -436,6 +414,31 @@ const Template: React.SFC<TemplateProps> = (props) => {
return data;
};

const validation = {
language: Yup.object().nullable().required('Language is required.'),
label: Yup.string().required('Title is required.').max(50, 'Title length is too long.'),
body: Yup.string()
.transform((current, original) => {
return original.getCurrentContent().getPlainText();
})
.when('type', {
is: (val) => (!defaultAttribute.isHsm && !val) || defaultAttribute.isHsm,
then: Yup.string().required('Message is required.'),
}),
type: Yup.object()
.nullable()
.when('attachmentURL', {
is: (val) => val && val !== '',
then: Yup.object().required('Type is required.'),
}),
attachmentURL: Yup.string()
.nullable()
.when('type', {
is: (val) => val && val.id,
then: Yup.string().required('Attachment URL is required.'),
}),
};

const validationObj = defaultAttribute.isHsm ? { ...validation, ...HSMValidation } : validation;
const FormSchema = Yup.object().shape(validationObj, [['type', 'attachmentURL']]);

Expand Down

0 comments on commit 8a6d361

Please sign in to comment.