403Webshell
Server IP : 80.87.202.40  /  Your IP : 216.73.216.169
Web Server : Apache
System : Linux rospirotorg.ru 5.14.0-539.el9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Dec 5 22:26:13 UTC 2024 x86_64
User : bitrix ( 600)
PHP Version : 8.2.27
Disable Function : NONE
MySQL : OFF |  cURL : ON |  WGET : ON |  Perl : ON |  Python : OFF |  Sudo : ON |  Pkexec : ON
Directory :  /home/bitrix/ext_www/cvetdv.ru/bitrix/js/im/v2/model/src/utils/

Upload File :
current_dir [ Writeable] document_root [ Writeable]

 

Command :


[ Back ]     

Current File : /home/bitrix/ext_www/cvetdv.ru/bitrix/js/im/v2/model/src/utils/validate.js
import { Type } from 'main.core';

import { convertObjectKeysToCamelCase } from './format';

import type { JsonObject, JsonValue } from 'main.core';

export type FieldsConfig = FieldsConfigItem[];
type FieldsConfigItem = {
	fieldName: string | Array<string>,
	targetFieldName: string,
	checkFunction?: CheckFunction | Array<CheckFunction>, // OR logic
	formatFunction?: FormatFunction,
};
type CheckFunction = (target: JsonValue) => boolean;
type FormatFunction = (target: JsonValue, currentResult: JsonObject, rawFields: JsonObject) => JsonValue;

export const formatFieldsWithConfig = (fields: JsonObject, config: FieldsConfig): JsonObject => {
	const resultObject = {};
	const rawFields = convertObjectKeysToCamelCase(fields);

	config.forEach((fieldConfig: FieldsConfigItem) => {
		const { fieldName, targetFieldName, checkFunction, formatFunction } = fieldConfig;

		// check if field exists
		const foundFieldName = getValidFieldName(rawFields, fieldName);
		if (!foundFieldName)
		{
			return;
		}

		// validate value
		if (!isFieldValueValid(rawFields[foundFieldName], checkFunction))
		{
			return;
		}

		// format value
		resultObject[targetFieldName] = formatFieldValue({
			fieldValue: rawFields[foundFieldName],
			formatFunction,
			currentResult: resultObject,
			rawFields: fields,
		});
	});

	return resultObject;
};

const getValidFieldName = (fields: JsonObject, fieldName: string | string[]): string | null => {
	let fieldNameList = fieldName;
	if (Type.isStringFilled(fieldNameList))
	{
		fieldNameList = [fieldNameList];
	}

	for (const singleField of fieldNameList)
	{
		if (!Type.isUndefined(fields[singleField]))
		{
			return singleField;
		}
	}

	return null;
};

const isFieldValueValid = (field: JsonValue, checkFunction: CheckFunction | Array<CheckFunction>): boolean => {
	let checkFunctionList = checkFunction;
	if (Type.isUndefined(checkFunctionList))
	{
		return true;
	}

	if (Type.isFunction(checkFunctionList))
	{
		checkFunctionList = [checkFunctionList];
	}

	return checkFunctionList.some((singleFunction) => singleFunction(field));
};

type FormatFieldValueParams = {
	fieldValue: JsonValue,
	formatFunction: FormatFunction,
	currentResult: JsonObject,
	rawFields: JsonObject
};
const formatFieldValue = (params: FormatFieldValueParams) => {
	const { fieldValue, formatFunction, currentResult, rawFields } = params;
	if (Type.isUndefined(formatFunction))
	{
		return fieldValue;
	}

	return formatFunction(fieldValue, currentResult, rawFields);
};

Youez - 2016 - github.com/yon3zu
LinuXploit