Skip to content

Commit

Permalink
feat(138): add custom format validation rule for topics
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Oct 3, 2023
1 parent 3aa11f8 commit aeca708
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { useParams } from 'react-router-dom'
import { IChangeEvent } from '@rjsf/core'
import { RJSFSchema } from '@rjsf/utils'
import Form from '@rjsf/chakra-ui'
import validator from '@rjsf/validator-ajv8'

import { ApiError, Adapter, ProtocolAdapter } from '@/api/__generated__'
import { useGetAdapterTypes } from '@/api/hooks/useProtocolAdapters/useGetAdapterTypes.tsx'
Expand All @@ -30,7 +29,7 @@ import { FieldTemplate } from '../templates/FieldTemplate.tsx'
import { ObjectFieldTemplate } from '../templates/ObjectFieldTemplate.tsx'
import { BaseInputTemplate } from '../templates/BaseInputTemplate.tsx'
import useGetUiSchema from '../../hooks/useGetUISchema.ts'
import { customValidate } from '../../utils/validation-utils.ts'
import { customFormatsValidator, customValidate } from '../../utils/validation-utils.ts'

interface AdapterInstanceDrawerProps {
adapterType?: string
Expand Down Expand Up @@ -108,7 +107,7 @@ const AdapterInstanceDrawer: FC<AdapterInstanceDrawerProps> = ({
templates={{ ObjectFieldTemplate, FieldTemplate, BaseInputTemplate }}
liveValidate
onSubmit={onValidate}
validator={validator}
validator={customFormatsValidator}
showErrorList={'bottom'}
onError={(errors) => console.log('XXXXXXX', errors)}
formData={defaultValues}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Adapter } from '@/api/__generated__'
import { TFunction } from 'i18next'

import { AdapterConfig } from '@/modules/ProtocolAdapters/types.ts'
import { customizeValidator } from '@rjsf/validator-ajv8'

/**
*
Expand All @@ -23,7 +24,6 @@ export const customValidate =
(jsonSchema: RJSFSchema, existingAdapters: Adapter[] | undefined, t: TFunction) =>
(formData: Record<string, unknown>, errors: FormValidation<AdapterConfig>, uiSchema?: UiSchema<AdapterConfig>) => {
// Check for uniqueness of `id` ONLY if `format` = `identifier` and not `ui:disabled`

if (
!uiSchema?.id?.['ui:disabled'] &&
(jsonSchema.properties?.['id'] as StrictRJSFSchema)?.format === 'identifier'
Expand All @@ -34,3 +34,9 @@ export const customValidate =
}
return errors
}

export const customFormatsValidator = customizeValidator({
customFormats: {
'mqtt-topic': /^[^+#$]*$/,
},
})

0 comments on commit aeca708

Please sign in to comment.