diff --git a/hivemq-edge/src/frontend/src/config/index.ts b/hivemq-edge/src/frontend/src/config/index.ts index ad6807b2a7..561ba7779b 100644 --- a/hivemq-edge/src/frontend/src/config/index.ts +++ b/hivemq-edge/src/frontend/src/config/index.ts @@ -71,7 +71,7 @@ const config: configType = { /** * Mappings (inward and outwards) are available from the workspace and hidden in the configuration */ - ADAPTER_MAPPINGS_IN_WORKSPACE: import.meta.env.VITE_FLAG_ADAPTER_MAPPINGS_IN_WORKSPACE === 'true', + ADAPTER_MAPPINGS_IN_WORKSPACE: true, }, documentation: { diff --git a/hivemq-edge/src/frontend/src/modules/Mappings/components/MappingForm.tsx b/hivemq-edge/src/frontend/src/modules/Mappings/components/MappingForm.tsx index 19302ee792..0f80cd1979 100644 --- a/hivemq-edge/src/frontend/src/modules/Mappings/components/MappingForm.tsx +++ b/hivemq-edge/src/frontend/src/modules/Mappings/components/MappingForm.tsx @@ -13,8 +13,6 @@ import { MappingContext } from '@/modules/ProtocolAdapters/types.ts' import { CustomValidator, RJSFSchema } from '@rjsf/utils' import { FormContextType } from '@rjsf/utils/src/types.ts' -import config from '@/config' - interface MappingFormProps { adapterId: string adapterType?: string @@ -67,8 +65,8 @@ const MappingForm: FC = ({ adapterId, adapterType, type, onSub formContext={context} onSubmit={onFormSubmit} customValidate={ - type === MappingType.OUTWARD && config.features.ADAPTER_MAPPINGS_IN_WORKSPACE - ? (customMappingValidate as CustomValidator) + type === MappingType.OUTWARD && adapterType + ? (customMappingValidate(adapterType) as CustomValidator) : undefined } /> diff --git a/hivemq-edge/src/frontend/src/modules/ProtocolAdapters/utils/validation-utils.ts b/hivemq-edge/src/frontend/src/modules/ProtocolAdapters/utils/validation-utils.ts index e84c7f77cd..3ca86d7d51 100644 --- a/hivemq-edge/src/frontend/src/modules/ProtocolAdapters/utils/validation-utils.ts +++ b/hivemq-edge/src/frontend/src/modules/ProtocolAdapters/utils/validation-utils.ts @@ -5,6 +5,10 @@ import { TFunction } from 'i18next' import { AdapterConfig } from '@/modules/ProtocolAdapters/types.ts' import i18n from '@/config/i18n.config.ts' +import { + getOutwardMappingRootProperty, + getOutwardMappingRootPropertyKey, +} from '@/modules/Workspace/utils/adapter.utils.ts' /** * @@ -36,36 +40,42 @@ export const customValidate = return errors } -export const customMappingValidate = (formData: Record, errors: FormValidation) => { - const outwardMappings = formData['mqttToOpcuaMappings'] +export const customMappingValidate = + (adapterType: string) => (formData: Record, errors: FormValidation) => { + const key = getOutwardMappingRootProperty(adapterType) + const outwardMappingsKey = getOutwardMappingRootPropertyKey(adapterType) + // @ts-ignore + const outwardMappings = formData[key][outwardMappingsKey] as FieldMappingsModel[] - return outwardMappings.reduce((errors, currentMapping, index) => { - const { metadata, fieldMapping } = currentMapping - if (!metadata) { - errors.mqttToOpcuaMappings?.[index]?.fieldMapping?.addError( - i18n.t('components:rjsf.MqttTransformationField.validation.error.noValidation') - ) - return errors - } + if (!outwardMappings.length) return errors - const { destination } = metadata - if (!destination) { - // TODO[NVL] This is not necessarily an error - errors.mqttToOpcuaMappings?.[index]?.fieldMapping?.addError( - i18n.t('components:rjsf.MqttTransformationField.validation.error.noSchema') - ) - return errors - } + return outwardMappings.reduce((errors, currentMapping, index) => { + const { metadata, fieldMapping } = currentMapping + if (!metadata) { + errors?.[key]?.[outwardMappingsKey]?.[index]?.fieldMapping?.addError( + i18n.t('components:rjsf.MqttTransformationField.validation.error.noValidation') + ) + return errors + } - const countRequired = destination.length + const { destination } = metadata + if (!destination) { + // TODO[NVL] This is not necessarily an error + errors?.[key]?.[outwardMappingsKey]?.[index]?.fieldMapping?.addError( + i18n.t('components:rjsf.MqttTransformationField.validation.error.noSchema') + ) + return errors + } - if (fieldMapping?.length !== countRequired) { - errors.mqttToOpcuaMappings?.[index]?.fieldMapping?.addError( - i18n.t('components:rjsf.MqttTransformationField.validation.error.missingMapping') - ) - return errors - } + const countRequired = destination.length - return errors - }, errors) -} + if (fieldMapping?.length !== countRequired) { + errors?.[key]?.[outwardMappingsKey]?.[index]?.fieldMapping?.addError( + i18n.t('components:rjsf.MqttTransformationField.validation.error.missingMapping') + ) + return errors + } + + return errors + }, errors) + } diff --git a/hivemq-edge/src/frontend/src/modules/Workspace/utils/adapter.utils.ts b/hivemq-edge/src/frontend/src/modules/Workspace/utils/adapter.utils.ts index 8fd0017717..525c36d349 100644 --- a/hivemq-edge/src/frontend/src/modules/Workspace/utils/adapter.utils.ts +++ b/hivemq-edge/src/frontend/src/modules/Workspace/utils/adapter.utils.ts @@ -23,6 +23,9 @@ export const getInwardMappingRootProperty = (adapterType: string) => `${adapterT export const getOutwardMappingRootProperty = (adapterType: string) => `${MQTT_PROPERTY_STUB.outward}${capitalize(adapterType)}` +export const getOutwardMappingRootPropertyKey = (adapterType: string) => + `${getOutwardMappingRootProperty(adapterType)}${MQTT_PROPERTY_STUB.mapping}` + export const isBidirectional = (adapter: ProtocolAdapter | undefined) => { return Boolean(adapter?.capabilities?.includes('WRITE')) }