diff options
Diffstat (limited to 'catalog-ui/src/app/utils/validation-utils.ts')
-rw-r--r-- | catalog-ui/src/app/utils/validation-utils.ts | 161 |
1 files changed, 79 insertions, 82 deletions
diff --git a/catalog-ui/src/app/utils/validation-utils.ts b/catalog-ui/src/app/utils/validation-utils.ts index bcb49d8b89..908ac18271 100644 --- a/catalog-ui/src/app/utils/validation-utils.ts +++ b/catalog-ui/src/app/utils/validation-utils.ts @@ -7,9 +7,9 @@ * 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. @@ -18,23 +18,23 @@ * ============LICENSE_END========================================================= */ -import * as _ from "lodash"; +import * as _ from 'lodash'; class basePattern { - pattern:RegExp; - base:number; + pattern: RegExp; + base: number; - constructor(pattern:RegExp, base:number) { + constructor(pattern: RegExp, base: number) { this.pattern = pattern; this.base = base; } } export interface IMapRegex { - integer:RegExp; - boolean:RegExp; - float:RegExp; - string:RegExp; + integer: RegExp; + boolean: RegExp; + float: RegExp; + string: RegExp; } export class ValidationUtils { @@ -47,60 +47,19 @@ export class ValidationUtils { 'NumberValidationPattern', 'LabelValidationPattern', ]; - private trueRegex:string = '[t][r][u][e]|[t]|[o][n]|[y]|[y][e][s]|[1]'; - private falseRegex:string = '[f][a][l][s][e]|[f]|[o][f][f]|[n]|[n][o]|[0]'; - private heatBooleanValidationPattern:RegExp = new RegExp('^(' + this.trueRegex + '|' + this.falseRegex + ')$'); - - - constructor(private IntegerNoLeadingZeroValidationPattern:RegExp, - private FloatValidationPattern:RegExp, - private CommentValidationPattern:RegExp, - private BooleanValidationPattern:RegExp, - private NumberValidationPattern:RegExp, - private LabelValidationPattern:RegExp) { - } - - public stripAndSanitize(text:string):string { - if (!text) { - return null; - } - return text.replace(/\s+/g, ' ').replace(/%[A-Fa-f0-9]{2}/g, '') - .replace(/&/g, "&").replace(/>/g, ">") - .replace(/</g, "<").replace(/"/g, """) - .replace(/'/g, "'").trim(); + private trueRegex: string = '[t][r][u][e]|[t]|[o][n]|[y]|[y][e][s]|[1]'; + private falseRegex: string = '[f][a][l][s][e]|[f]|[o][f][f]|[n]|[n][o]|[0]'; + private heatBooleanValidationPattern: RegExp = new RegExp('^(' + this.trueRegex + '|' + this.falseRegex + ')$'); + + constructor(private IntegerNoLeadingZeroValidationPattern: RegExp, + private FloatValidationPattern: RegExp, + private CommentValidationPattern: RegExp, + private BooleanValidationPattern: RegExp, + private NumberValidationPattern: RegExp, + private LabelValidationPattern: RegExp) { } - public getValidationPattern = (validationType:string, parameterType?:string):RegExp => { - switch (validationType) { - case 'integer': - return this.IntegerNoLeadingZeroValidationPattern; - case 'float': - return this.FloatValidationPattern; - case 'number': - return this.NumberValidationPattern; - case 'string': - return this.CommentValidationPattern; - case 'boolean': - { - //Bug Fix DE197437 [Patch]Mismatch between BE to FE regarding supported characters in Boolean filed - if (parameterType && parameterType === 'heat') { - return this.heatBooleanValidationPattern; - } - else { - return this.BooleanValidationPattern; - } - } - - case 'label': - return this.LabelValidationPattern; - case 'category': - return this.LabelValidationPattern; - default : - return null; - } - }; - - public static getPropertyListPatterns():IMapRegex { + public static getPropertyListPatterns(): IMapRegex { return { integer: /^$|^(0|[-+]?[1-9][0-9]*|[-+]?0x[0-9a-fA-F]+|[-+]?0o[0-7]+)(,?(0|[-+]?[1-9][0-9]*|[-+]?0x[0-9a-fA-F]+|[-+]?0o[0-7]+))*$/, string: /^$|^"[\u0000-\u0021\u0023-\u00BF]+"(\s*,?\s*"[\u0000-\u0021\u0023-\u00BF]+")*$/, @@ -109,7 +68,7 @@ export class ValidationUtils { }; } - public static getPropertyMapPatterns():IMapRegex { + public static getPropertyMapPatterns(): IMapRegex { return { integer: /^$|^"\w+"\s*:\s?(0|[-+]?[1-9][0-9]*|[-+]?0x[0-9a-fA-F]+|[-+]?0o[0-7]+)+(\s*,?\s*"\w+"\s?:\s?(0|[-+]?[1-9][0-9]*|[-+]?0x[0-9a-fA-F]+|[-+]?0o[0-7]+)+)*$/, string: /^$|^"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*"(\s*,?\s*"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*")*$/, @@ -120,12 +79,12 @@ export class ValidationUtils { public static validateUniqueKeys(viewValue: string): boolean { if (!viewValue) { - return true; //allow empty value + return true; // allow empty value } - let json:string = "{" + viewValue.replace(/\s\s+/g, ' ') + "}"; + const json: string = '{' + viewValue.replace(/\s\s+/g, ' ') + '}'; try { - let obj:any = JSON.parse(json); + const obj: any = JSON.parse(json); /* //Method #1 : check json string length before & after parsing let newJson:string = JSON.stringify(obj); @@ -133,21 +92,59 @@ export class ValidationUtils { return false; }*/ - //Method #2 : check how many times we can find "KEY": in json string - let result:boolean = true; - Object.keys(obj).forEach((key:string) => { + // Method #2 : check how many times we can find "KEY": in json string + let result: boolean = true; + Object.keys(obj).forEach((key: string) => { result = result && json.split('"' + key + '":').length === 2; }); return result; } catch (e) { - return false; //not a valid JSON + return false; // not a valid JSON } - //return true; + // return true; } - public validateJson = (json:string):boolean => { + public stripAndSanitize(text: string): string { + if (!text) { + return null; + } + return text.replace(/\s+/g, ' ').replace(/%[A-Fa-f0-9]{2}/g, '') + .replace(/&/g, '&').replace(/>/g, '>') + .replace(/</g, '<').replace(/"/g, '"') + .replace(/'/g, ''').trim(); + } + + public getValidationPattern = (validationType: string, parameterType?: string): RegExp => { + switch (validationType) { + case 'integer': + return this.IntegerNoLeadingZeroValidationPattern; + case 'float': + return this.FloatValidationPattern; + case 'number': + return this.NumberValidationPattern; + case 'string': + return this.CommentValidationPattern; + case 'boolean': { + // Bug Fix DE197437 [Patch]Mismatch between BE to FE regarding supported characters in Boolean filed + if (parameterType && parameterType === 'heat') { + return this.heatBooleanValidationPattern; + } else { + return this.BooleanValidationPattern; + } + } + + case 'label': + return this.LabelValidationPattern; + case 'category': + return this.LabelValidationPattern; + default : + return null; + } + } + + public validateJson = (json: string): boolean => { try { JSON.parse(json); return true; @@ -155,22 +152,22 @@ export class ValidationUtils { console.log('invalid json'); return false; } - }; + } - public validateIntRange = (value:string):boolean => { + public validateIntRange = (value: string): boolean => { - let base8 = new basePattern(/^([-+]?0o[0-7]+)$/, 8); - let base10 = new basePattern(/^(0|[-+]?[1-9][0-9]*)$/, 10); - let base16 = new basePattern(/^([-+]?0x[0-9a-fA-F]+)$/, 16); + const base8 = new basePattern(/^([-+]?0o[0-7]+)$/, 8); + const base10 = new basePattern(/^(0|[-+]?[1-9][0-9]*)$/, 10); + const base16 = new basePattern(/^([-+]?0x[0-9a-fA-F]+)$/, 16); - let min:number = -0x80000000; - let max:number = 0x7fffffff; - let intPatterns:Array<basePattern> = [base8, base10, base16]; - let matchedBase = _.find(intPatterns, (item)=> { + const min: number = -0x80000000; + const max: number = 0x7fffffff; + const intPatterns: basePattern[] = [base8, base10, base16]; + const matchedBase = _.find(intPatterns, (item) => { return item.pattern.test(value); }); - let parsed:number = parseInt(value.replace('o', ''), matchedBase.base); + const parsed: number = parseInt(value.replace('o', ''), matchedBase.base); if (parsed) { return min <= parsed && max >= parsed; } |