Skip to content

Commit ac86cbd

Browse files
#8186 - Reject monomer group template with empty groupClass
1 parent 63d6e25 commit ac86cbd

3 files changed

Lines changed: 81 additions & 0 deletions

File tree

packages/ketcher-core/__tests__/application/editor/Editor.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
KetcherLogger,
1212
MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH,
1313
MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH_ERROR_MESSAGE,
14+
MONOMER_GROUP_TEMPLATE_CLASS_EMPTY_ERROR_MESSAGE,
1415
} from 'utilities';
1516

1617
describe('CoreEditor', () => {
@@ -597,6 +598,72 @@ describe('CoreEditor', () => {
597598
initialTemplatesCount + 1,
598599
);
599600
});
601+
602+
it('should reject monomer group template with empty class', () => {
603+
const presetWithEmptyClass = {
604+
root: {
605+
templates: [
606+
{
607+
$ref: 'monomerGroupTemplate-emptyClass',
608+
},
609+
],
610+
},
611+
'monomerGroupTemplate-emptyClass': {
612+
type: 'monomerGroupTemplate',
613+
id: '',
614+
name: 'TestPresetEmptyClass',
615+
class: '',
616+
templates: [],
617+
connections: [],
618+
},
619+
};
620+
621+
const initialTemplatesCount =
622+
editor.monomersLibraryParsedJson?.root.templates.length ?? 0;
623+
editor.updateMonomersLibrary(JSON.stringify(presetWithEmptyClass));
624+
625+
expect(errorSpy).toHaveBeenCalledWith(
626+
expect.stringContaining(
627+
MONOMER_GROUP_TEMPLATE_CLASS_EMPTY_ERROR_MESSAGE,
628+
),
629+
);
630+
expect(editor.monomersLibraryParsedJson?.root.templates.length).toBe(
631+
initialTemplatesCount,
632+
);
633+
});
634+
635+
it('should reject monomer group template with whitespace-only class', () => {
636+
const presetWithWhitespaceClass = {
637+
root: {
638+
templates: [
639+
{
640+
$ref: 'monomerGroupTemplate-whitespaceClass',
641+
},
642+
],
643+
},
644+
'monomerGroupTemplate-whitespaceClass': {
645+
type: 'monomerGroupTemplate',
646+
id: '',
647+
name: 'TestPresetWhitespaceClass',
648+
class: ' ',
649+
templates: [],
650+
connections: [],
651+
},
652+
};
653+
654+
const initialTemplatesCount =
655+
editor.monomersLibraryParsedJson?.root.templates.length ?? 0;
656+
editor.updateMonomersLibrary(JSON.stringify(presetWithWhitespaceClass));
657+
658+
expect(errorSpy).toHaveBeenCalledWith(
659+
expect.stringContaining(
660+
MONOMER_GROUP_TEMPLATE_CLASS_EMPTY_ERROR_MESSAGE,
661+
),
662+
);
663+
expect(editor.monomersLibraryParsedJson?.root.templates.length).toBe(
664+
initialTemplatesCount,
665+
);
666+
});
600667
});
601668

602669
describe('window blur handling', () => {

packages/ketcher-core/src/application/editor/Editor.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ import {
8080
IDT_ALIAS_LENGTH_ERROR_MESSAGE,
8181
MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH,
8282
MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH_ERROR_MESSAGE,
83+
MONOMER_GROUP_TEMPLATE_CLASS_EMPTY_ERROR_MESSAGE,
8384
isValidBilnAlias,
8485
isValidHelmAlias,
8586
isValidIdtAlias,
@@ -604,6 +605,16 @@ export class CoreEditor {
604605
return;
605606
}
606607

608+
if (
609+
templateDefinition.class !== undefined &&
610+
!templateDefinition.class.trim()
611+
) {
612+
KetcherLogger.error(
613+
`Editor::updateMonomersLibrary: Load of monomer group template "${templateDefinition.name}" (template: ${templateRef.$ref}) has failed. ${MONOMER_GROUP_TEMPLATE_CLASS_EMPTY_ERROR_MESSAGE} The template was not added to the library.`,
614+
);
615+
return;
616+
}
617+
607618
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
608619
this._monomersLibraryParsedJson![templateRef.$ref] = templateDefinition;
609620
if (

packages/ketcher-core/src/utilities/monomers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export const MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH = 200;
1818

1919
export const MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH_ERROR_MESSAGE = `The monomer group template name must not exceed ${MONOMER_GROUP_TEMPLATE_NAME_MAX_LENGTH} characters.`;
2020

21+
export const MONOMER_GROUP_TEMPLATE_CLASS_EMPTY_ERROR_MESSAGE =
22+
'The monomer group template class cannot be empty or whitespace.';
23+
2124
const HELM_ALIAS_REGEX = /^(?!.*\s)[A-Za-z0-9_*.[\]()-]+$/;
2225
const BILN_ALIAS_REGEX = /^[A-Za-z0-9_*-]+$/;
2326

0 commit comments

Comments
 (0)