<commit_before>const objectMaybeIncluding = (types) => {
	return Match.Where((value) => {
		Object.keys(types).forEach(field => {
			if (value[field] != null) {
				try {
					check(value[field], types[field]);
				} catch (error) {
					error.path = field;
					throw error;
				}
			}
		});

		return true;
	});
};

const validateAttachmentsFields = attachmentFields => {
	check(attachmentFields, objectMaybeIncluding({
		short: Boolean
	}));

	check(attachmentFields, Match.ObjectIncluding({
		title: String,
		value: String
	}));
};

const validateAttachment = attachment => {
	check(attachment, objectMaybeIncluding({
		color: String,
		text: String,
		ts: String,
		thumb_url: String,
		message_link: String,
		collapsed: Boolean,
		author_name: String,
		author_link: String,
		author_icon: String,
		title: String,
		title_link: String,
		title_link_download: Boolean,
		image_url: String,
		audio_url: String,
		video_url: String,
		fields: Array
	}));

	if (attachment.fields && attachment.fields.length) {
		attachment.fields.map(validateAttachmentsFields);
	}
};

const validateBodyAttachments = attachments => attachments.map(validateAttachment);

RocketChat.sendMessage = function(user, message, room, upsert = false) {
	if (!user || !message || !room._id) {
		return false;
	}

	check(message, objectMaybeIncluding({
		_id: String,
		msg: String,
		text: String,
		alias: String,
		emoji: String,
		avatar: String,
		attachments: Array
	}));

	if (Array.isArray(message.attachments) && message.attachments.length) {
		validateBodyAttachments(message.attachments);
	}

	if (!message.ts) {
		message.ts = new Date();
	}
	const { _id, username, name } = user;
	message.u = {
		_id,<commit_msg>Fix the attachments and fields incorrectly failing on validation (#10573)<commit_after>const objectMaybeIncluding = (types) => {
	return Match.Where((value) => {
		Object.keys(types).forEach(field => {
			if (value[field] != null) {
				try {
					check(value[field], types[field]);
				} catch (error) {
					error.path = field;
					throw error;
				}
			}
		});

		return true;
	});
};

const validateAttachmentsFields = attachmentFields => {
	check(attachmentFields, objectMaybeIncluding({
		short: Boolean
	}));

	check(attachmentFields, Match.ObjectIncluding({
		title: String,
		value: String
	}));
};

const validateAttachment = attachment => {
	check(attachment, objectMaybeIncluding({
		color: String,
		text: String,
		ts: String,
		thumb_url: String,
		message_link: String,
		collapsed: Boolean,
		author_name: String,
		author_link: String,
		author_icon: String,
		title: String,
		title_link: String,
		title_link_download: Boolean,
		image_url: String,
		audio_url: String,
		video_url: String,
		fields: [Match.Any]
	}));

	if (attachment.fields && attachment.fields.length) {
		attachment.fields.map(validateAttachmentsFields);
	}
};

const validateBodyAttachments = attachments => attachments.map(validateAttachment);

RocketChat.sendMessage = function(user, message, room, upsert = false) {
	if (!user || !message || !room._id) {
		return false;
	}

	check(message, objectMaybeIncluding({
		_id: String,
		msg: String,
		text: String,
		alias: String,
		emoji: String,
		avatar: String,
		attachments: [Match.Any]
	}));

	if (Array.isArray(message.attachments) && message.attachments.length) {
		validateBodyAttachments(message.attachments);
	}

	if (!message.ts) {
		message.ts = new Date();
	}
	const { _id, username, name } = user;
	message.u = {
		_id,
{'fraction_non_alphanumeric': 0.13584147041929925, 'fraction_numerical': 0.0014359563469270534, 'mean_word_length': 4.245481927710843, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7656364', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1145, 'n_words': 252}