diff --git a/apps/server/src/modules/tool/common/service/common-tool-validation.service.spec.ts b/apps/server/src/modules/tool/common/service/common-tool-validation.service.spec.ts index 43a2aba1a39..d3ca547a854 100644 --- a/apps/server/src/modules/tool/common/service/common-tool-validation.service.spec.ts +++ b/apps/server/src/modules/tool/common/service/common-tool-validation.service.spec.ts @@ -597,6 +597,7 @@ describe('CommonToolValidationService', () => { const setup = () => { const undefinedRegex: CustomParameter = customParameterFactory.build({ name: 'undefinedRegex', + isOptional: false, scope: CustomParameterScope.SCHOOL, type: CustomParameterType.STRING, regex: undefined, @@ -629,6 +630,7 @@ describe('CommonToolValidationService', () => { const setup = () => { const validRegex: CustomParameter = customParameterFactory.build({ name: 'validRegex', + isOptional: false, scope: CustomParameterScope.SCHOOL, type: CustomParameterType.STRING, regex: '[x]', @@ -661,6 +663,7 @@ describe('CommonToolValidationService', () => { const setup = () => { const validRegex: CustomParameter = customParameterFactory.build({ name: 'validRegex', + isOptional: false, scope: CustomParameterScope.SCHOOL, type: CustomParameterType.STRING, regex: '[x]', @@ -688,6 +691,39 @@ describe('CommonToolValidationService', () => { expect(func).toThrowError('tool_param_value_regex'); }); }); + + describe('when parameter is optional and a regex is given, but the param value is undefined', () => { + const setup = () => { + const optionalRegex: CustomParameter = customParameterFactory.build({ + name: 'optionalRegex', + isOptional: true, + scope: CustomParameterScope.SCHOOL, + type: CustomParameterType.STRING, + regex: '[x]', + }); + const { externalTool, schoolExternalTool } = createTools( + { + parameters: [optionalRegex], + }, + { + parameters: [{ name: 'optionalRegex', value: undefined }], + } + ); + + return { + externalTool, + schoolExternalTool, + }; + }; + + it('should return without error', () => { + const { externalTool, schoolExternalTool } = setup(); + + const func = () => service.checkCustomParameterEntries(externalTool, schoolExternalTool); + + expect(func).not.toThrowError('tool_param_value_regex'); + }); + }); }); }); }); diff --git a/apps/server/src/modules/tool/common/service/common-tool-validation.service.ts b/apps/server/src/modules/tool/common/service/common-tool-validation.service.ts index 9d315a97f34..3ee9ee7d465 100644 --- a/apps/server/src/modules/tool/common/service/common-tool-validation.service.ts +++ b/apps/server/src/modules/tool/common/service/common-tool-validation.service.ts @@ -87,7 +87,7 @@ export class CommonToolValidationService { } private checkParameterRegex(foundEntry: CustomParameterEntry, param: CustomParameter): void { - if (param.regex && !new RegExp(param.regex).test(foundEntry.value ?? '')) { + if (foundEntry.value !== undefined && param.regex && !new RegExp(param.regex).test(foundEntry.value ?? '')) { throw new ValidationError( `tool_param_value_regex: The given entry for the parameter with name ${foundEntry.name} does not fit the regex.` ); diff --git a/apps/server/src/modules/tool/external-tool/service/external-tool-validation.service.ts b/apps/server/src/modules/tool/external-tool/service/external-tool-validation.service.ts index 434e7fac86e..bf768a83a3e 100644 --- a/apps/server/src/modules/tool/external-tool/service/external-tool-validation.service.ts +++ b/apps/server/src/modules/tool/external-tool/service/external-tool-validation.service.ts @@ -1,6 +1,5 @@ -import { Inject, Injectable } from '@nestjs/common'; +import { Injectable } from '@nestjs/common'; import { ValidationError } from '@shared/common'; -import { IToolFeatures, ToolFeatures } from '../../tool-config'; import { ExternalTool } from '../domain'; import { ExternalToolLogoService } from './external-tool-logo.service'; import { ExternalToolParameterValidationService } from './external-tool-parameter-validation.service'; @@ -11,7 +10,6 @@ export class ExternalToolValidationService { constructor( private readonly externalToolService: ExternalToolService, private readonly externalToolParameterValidationService: ExternalToolParameterValidationService, - @Inject(ToolFeatures) private readonly toolFeatures: IToolFeatures, private readonly externalToolLogoService: ExternalToolLogoService ) {}