Michael Lando | dd60339 | 2017-07-12 00:54:52 +0300 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * SDC |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
| 6 | * ================================================================================ |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
Michael Lando | ed64b5e | 2017-06-09 03:19:04 +0300 | [diff] [blame] | 21 | class basePattern { |
| 22 | pattern:RegExp; |
| 23 | base:number; |
| 24 | |
| 25 | constructor(pattern:RegExp, base:number) { |
| 26 | this.pattern = pattern; |
| 27 | this.base = base; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | export interface IMapRegex { |
| 32 | integer:RegExp; |
| 33 | boolean:RegExp; |
| 34 | float:RegExp; |
| 35 | string:RegExp; |
| 36 | } |
| 37 | |
| 38 | export class ValidationUtils { |
| 39 | |
| 40 | static '$inject' = [ |
| 41 | 'IntegerNoLeadingZeroValidationPattern', |
| 42 | 'FloatValidationPattern', |
| 43 | 'CommentValidationPattern', |
| 44 | 'BooleanValidationPattern', |
| 45 | 'NumberValidationPattern', |
| 46 | 'LabelValidationPattern', |
| 47 | ]; |
| 48 | private trueRegex:string = '[t][r][u][e]|[t]|[o][n]|[y]|[y][e][s]|[1]'; |
| 49 | private falseRegex:string = '[f][a][l][s][e]|[f]|[o][f][f]|[n]|[n][o]|[0]'; |
| 50 | private heatBooleanValidationPattern:RegExp = new RegExp('^(' + this.trueRegex + '|' + this.falseRegex + ')$'); |
| 51 | |
| 52 | |
| 53 | constructor(private IntegerNoLeadingZeroValidationPattern:RegExp, |
| 54 | private FloatValidationPattern:RegExp, |
| 55 | private CommentValidationPattern:RegExp, |
| 56 | private BooleanValidationPattern:RegExp, |
| 57 | private NumberValidationPattern:RegExp, |
| 58 | private LabelValidationPattern:RegExp) { |
| 59 | } |
| 60 | |
| 61 | public stripAndSanitize(text:string):string { |
| 62 | if (!text) { |
| 63 | return null; |
| 64 | } |
| 65 | return text.replace(/\s+/g, ' ').replace(/%[A-Fa-f0-9]{2}/g, '').trim(); |
| 66 | } |
| 67 | |
| 68 | public getValidationPattern = (validationType:string, parameterType?:string):RegExp => { |
| 69 | switch (validationType) { |
| 70 | case 'integer': |
| 71 | return this.IntegerNoLeadingZeroValidationPattern; |
| 72 | case 'float': |
| 73 | return this.FloatValidationPattern; |
| 74 | case 'number': |
| 75 | return this.NumberValidationPattern; |
| 76 | case 'string': |
| 77 | return this.CommentValidationPattern; |
| 78 | case 'boolean': |
| 79 | { |
| 80 | //Bug Fix DE197437 [Patch]Mismatch between BE to FE regarding supported characters in Boolean filed |
| 81 | if (parameterType && parameterType === 'heat') { |
| 82 | return this.heatBooleanValidationPattern; |
| 83 | } |
| 84 | else { |
| 85 | return this.BooleanValidationPattern; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | case 'label': |
| 90 | return this.LabelValidationPattern; |
| 91 | case 'category': |
| 92 | return this.LabelValidationPattern; |
| 93 | default : |
| 94 | return null; |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | public getPropertyListPatterns():IMapRegex { |
| 99 | return { |
| 100 | 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]+))*$/, |
| 101 | string: /^"[\u0000-\u0021\u0023-\u00BF]+"(\s*,?\s*"[\u0000-\u0021\u0023-\u00BF]+")*$/, |
| 102 | boolean: /^([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])(,?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]))*$/, |
| 103 | float: /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?(,?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?f?)*$/ |
| 104 | }; |
| 105 | } |
| 106 | |
| 107 | public getPropertyMapPatterns():IMapRegex { |
| 108 | return { |
| 109 | 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]+)+)*$/, |
| 110 | string: /^"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*"(\s*,?\s*"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*")*$/, |
| 111 | boolean: /^"\w+"\s?:\s?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])(\s*,?\s*"\w+"\s?:\s?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]))*$/, |
| 112 | float: /^"\w+"\s?:\s?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?f?(\s*,?\s*"\w+"\s?:\s?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?f?)*$/ |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | public validateUniqueKeys(viewValue:string):boolean { |
| 117 | if (!viewValue) { |
| 118 | return true; //allow empty value |
| 119 | } |
| 120 | |
| 121 | let json:string = "{" + viewValue.replace(/\s\s+/g, ' ') + "}"; |
| 122 | try { |
| 123 | let obj:any = JSON.parse(json); |
| 124 | /* |
| 125 | //Method #1 : check json string length before & after parsing |
| 126 | let newJson:string = JSON.stringify(obj); |
| 127 | if (newJson.length < json.length) { |
| 128 | return false; |
| 129 | }*/ |
| 130 | |
| 131 | //Method #2 : check how many times we can find "KEY": in json string |
| 132 | let result:boolean = true; |
| 133 | Object.keys(obj).forEach((key:string) => { |
| 134 | result = result && json.split('"' + key + '":').length === 2; |
| 135 | }); |
| 136 | return result; |
| 137 | |
| 138 | } catch (e) { |
| 139 | return false; //not a valid JSON |
| 140 | } |
| 141 | |
| 142 | //return true; |
| 143 | } |
| 144 | |
| 145 | public validateJson = (json:string):boolean => { |
| 146 | try { |
| 147 | JSON.parse(json); |
| 148 | return true; |
| 149 | } catch (err) { |
| 150 | console.log('invalid json'); |
| 151 | return false; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | public validateIntRange = (value:string):boolean => { |
| 156 | |
| 157 | let base8 = new basePattern(/^([-+]?0o[0-7]+)$/, 8); |
| 158 | let base10 = new basePattern(/^(0|[-+]?[1-9][0-9]*)$/, 10); |
| 159 | let base16 = new basePattern(/^([-+]?0x[0-9a-fA-F]+)$/, 16); |
| 160 | |
| 161 | let min:number = -0x80000000; |
| 162 | let max:number = 0x7fffffff; |
| 163 | let intPatterns:Array<basePattern> = [base8, base10, base16]; |
| 164 | let matchedBase = _.find(intPatterns, (item)=> { |
| 165 | return item.pattern.test(value); |
| 166 | }); |
| 167 | |
| 168 | let parsed:number = parseInt(value.replace('o', ''), matchedBase.base); |
| 169 | if (parsed) { |
| 170 | return min <= parsed && max >= parsed; |
| 171 | } |
| 172 | } |
| 173 | } |