Skip to content

Commit

Permalink
fix optional parameter with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
MarvinOehlerkingCap committed Nov 8, 2023
1 parent 33381e6 commit e76eab9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]',
Expand Down Expand Up @@ -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]',
Expand Down Expand Up @@ -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');
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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.`
);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
) {}

Expand Down

0 comments on commit e76eab9

Please sign in to comment.