Skip to content

Commit

Permalink
fixed definition-validation for options (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz-Schrauth-GIP authored Dec 6, 2022
1 parent ce49c88 commit ab59db0
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions xo/xo-right-parameter.model.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit ab59db0

Please sign in to comment.