2018-01-22 12:25:50 +01:00
|
|
|
import { get, isEmpty, isObject } from 'lodash';
|
2018-01-22 12:05:22 +01:00
|
|
|
|
2018-02-16 12:05:32 +01:00
|
|
|
export default function checkFormValidity(settingType, data, providerToEdit = '') {
|
2018-01-22 12:05:22 +01:00
|
|
|
const formErrors = [];
|
|
|
|
|
|
|
|
switch (settingType) {
|
|
|
|
case 'providers': {
|
|
|
|
const isProviderEnabled = get(data, 'enabled');
|
2018-02-16 12:05:32 +01:00
|
|
|
const keys = providerToEdit === 'email' ? [] : ['key', 'secret'];
|
2018-01-22 12:05:22 +01:00
|
|
|
|
2018-11-15 15:41:08 -06:00
|
|
|
keys.forEach(key => {
|
2018-01-22 12:05:22 +01:00
|
|
|
if (isProviderEnabled && isEmpty(get(data, key))) {
|
|
|
|
formErrors.push({ name: key, errors: [{ id: 'components.Input.error.validation.required' }] });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2018-01-22 12:25:50 +01:00
|
|
|
case 'email-templates': {
|
2018-11-15 15:41:08 -06:00
|
|
|
Object.keys(data.options).forEach((value) => {
|
2018-01-22 12:25:50 +01:00
|
|
|
if (isObject(data.options[value])) {
|
2018-11-15 15:41:08 -06:00
|
|
|
Object.keys(data.options[value]).forEach(subValue => {
|
2018-01-22 12:25:50 +01:00
|
|
|
if (isEmpty(get(data, ['options', value, subValue]))) {
|
|
|
|
formErrors.push({ name: `options.${value}.${subValue}`, errors: [{ id: 'components.Input.error.validation.required' }] });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value !== 'response_email' && isEmpty(get(data, ['options', value]))) {
|
|
|
|
formErrors.push({ name: `options.${value}`, errors: [{ id: 'components.Input.error.validation.required' }] });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2018-01-22 12:05:22 +01:00
|
|
|
default:
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return formErrors;
|
|
|
|
}
|