Skip to content

Commit

Permalink
fix(28015): fix custom validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Nov 27, 2024
1 parent a7efc8c commit ff1a72f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 33 deletions.
2 changes: 1 addition & 1 deletion hivemq-edge/src/frontend/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,8 +65,8 @@ const MappingForm: FC<MappingFormProps> = ({ adapterId, adapterType, type, onSub
formContext={context}
onSubmit={onFormSubmit}
customValidate={
type === MappingType.OUTWARD && config.features.ADAPTER_MAPPINGS_IN_WORKSPACE
? (customMappingValidate as CustomValidator<unknown, RJSFSchema, FormContextType>)
type === MappingType.OUTWARD && adapterType
? (customMappingValidate(adapterType) as CustomValidator<unknown, RJSFSchema, FormContextType>)
: undefined
}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/**
*
Expand Down Expand Up @@ -36,36 +40,42 @@ export const customValidate =
return errors
}

export const customMappingValidate = (formData: Record<string, FieldMappingsModel[]>, errors: FormValidation) => {
const outwardMappings = formData['mqttToOpcuaMappings']
export const customMappingValidate =
(adapterType: string) => (formData: Record<string, FieldMappingsModel[]>, 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
}
Expand Down

0 comments on commit ff1a72f

Please sign in to comment.