Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N21-1187 Fix optional External Tool parameter with regex #4536

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading