@ammarahmed/notifee-react-native
Version:
Notifee - a feature rich notifications library for React Native.
46 lines (36 loc) • 1.24 kB
text/typescript
import { IOSCommunicationInfo } from '../../types/NotificationIOS';
import { isObject, isString } from '../../utils';
import validateIOSCommunicationInfoPerson from './validateIOSCommunicationInfoPerson';
export default function validateIOSCommunicationInfo(
communicationInfo: IOSCommunicationInfo,
): IOSCommunicationInfo {
if (!isObject(communicationInfo)) {
throw new Error('expected an object.');
}
if (
!isString(communicationInfo.conversationId) ||
communicationInfo.conversationId.length === 0
) {
throw new Error("'conversationId' expected a valid string value.");
}
if (!communicationInfo.sender || !isObject(communicationInfo.sender)) {
throw new Error("'sender' expected a valid object value.");
}
let sender;
try {
sender = validateIOSCommunicationInfoPerson(communicationInfo.sender);
} catch (e: any) {
throw new Error(`'sender' ${e.message}.`);
}
const out: IOSCommunicationInfo = {
conversationId: communicationInfo.conversationId,
sender,
};
if (communicationInfo.body) {
if (!isString(communicationInfo.body)) {
throw new Error("'body' expected a valid string value.");
}
out.body = communicationInfo.body;
}
return out;
}