From ab59db03be01b379597f04f637014645272dd9e3 Mon Sep 17 00:00:00 2001 From: Moritz-Schrauth-GIP <119330164+Moritz-Schrauth-GIP@users.noreply.github.com> Date: Tue, 6 Dec 2022 16:44:42 +0100 Subject: [PATCH] fixed definition-validation for options (#4) --- xo/xo-right-parameter.model.ts | 55 ++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/xo/xo-right-parameter.model.ts b/xo/xo-right-parameter.model.ts index d4eba4f..ac70028 100644 --- a/xo/xo-right-parameter.model.ts +++ b/xo/xo-right-parameter.model.ts @@ -1,20 +1,20 @@ -/* - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * Copyright 2022 GIP SmartMercial GmbH, Germany - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ +/* + * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + * Copyright 2022 GIP SmartMercial GmbH, Germany + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + */ import { XoArray, XoArrayClass, XoObject, XoObjectClass, XoProperty } from '@zeta/api'; import { I18nParam, I18nService } from '@zeta/i18n'; @@ -113,17 +113,20 @@ export class XoRightParameter extends XoObject { isDefinitionValid(def: string): RightParameterValueError { if (this.type === RightParameterType.OPTIONS) { - const match = /^(([a-zA-Z][a-zA-Z0-9._]+|[*]{1})([ ]*[,][ ]*){0,1})*$/.exec(def); - - if (!match) { + /* + Input is considered valid if it is a non-empty, comma-separated list of valid options. + An option is valid if + - it is "*" or + - it starts with a letter (lower- or uppercase), followed by at least one letter / number / "." / "_". + Substrings between commas are trimmed. I.e. " op1 , * , abc " is valid. + */ + const trimmedOptions = def.split(',') + .map(slice => slice.trim()); + const regexp = /^([a-zA-Z][a-zA-Z0-9._]+|[*])$/; + if (trimmedOptions.some(option => !regexp.test(option))) { return new RightParameterValueError('NOT COMMA SEPARATED LIST OF VALID OPTIONS'); } - - let arr = def.split(','); - arr = arr.filter(str => !!str); - arr = arr.map(str => str.trim()); - - if (arr.length === 0) { + if (trimmedOptions.length === 0) { return new RightParameterValueError('AT LEAST 1 VALID OPTION REQUIRED'); } return new RightParameterValueError();