blob: bcb49d8b898c0e7de9f9e7e9e5b404adca7e212b [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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 Landoa5445102018-03-04 14:53:33 +020021import * as _ from "lodash";
22
Michael Landoed64b5e2017-06-09 03:19:04 +030023class basePattern {
24 pattern:RegExp;
25 base:number;
26
27 constructor(pattern:RegExp, base:number) {
28 this.pattern = pattern;
29 this.base = base;
30 }
31}
32
33export interface IMapRegex {
34 integer:RegExp;
35 boolean:RegExp;
36 float:RegExp;
37 string:RegExp;
38}
39
40export class ValidationUtils {
41
42 static '$inject' = [
43 'IntegerNoLeadingZeroValidationPattern',
44 'FloatValidationPattern',
45 'CommentValidationPattern',
46 'BooleanValidationPattern',
47 'NumberValidationPattern',
48 'LabelValidationPattern',
49 ];
50 private trueRegex:string = '[t][r][u][e]|[t]|[o][n]|[y]|[y][e][s]|[1]';
51 private falseRegex:string = '[f][a][l][s][e]|[f]|[o][f][f]|[n]|[n][o]|[0]';
52 private heatBooleanValidationPattern:RegExp = new RegExp('^(' + this.trueRegex + '|' + this.falseRegex + ')$');
53
54
55 constructor(private IntegerNoLeadingZeroValidationPattern:RegExp,
56 private FloatValidationPattern:RegExp,
57 private CommentValidationPattern:RegExp,
58 private BooleanValidationPattern:RegExp,
59 private NumberValidationPattern:RegExp,
60 private LabelValidationPattern:RegExp) {
61 }
62
63 public stripAndSanitize(text:string):string {
64 if (!text) {
65 return null;
66 }
aribeiro7010ea92021-02-15 17:24:11 +000067 return text.replace(/\s+/g, ' ').replace(/%[A-Fa-f0-9]{2}/g, '')
68 .replace(/&/g, "&").replace(/>/g, ">")
69 .replace(/</g, "&lt;").replace(/"/g, "&quot;")
70 .replace(/'/g, "&apos;").trim();
Michael Landoed64b5e2017-06-09 03:19:04 +030071 }
72
73 public getValidationPattern = (validationType:string, parameterType?:string):RegExp => {
74 switch (validationType) {
75 case 'integer':
76 return this.IntegerNoLeadingZeroValidationPattern;
77 case 'float':
78 return this.FloatValidationPattern;
79 case 'number':
80 return this.NumberValidationPattern;
81 case 'string':
82 return this.CommentValidationPattern;
83 case 'boolean':
84 {
85 //Bug Fix DE197437 [Patch]Mismatch between BE to FE regarding supported characters in Boolean filed
86 if (parameterType && parameterType === 'heat') {
87 return this.heatBooleanValidationPattern;
88 }
89 else {
90 return this.BooleanValidationPattern;
91 }
92 }
93
94 case 'label':
95 return this.LabelValidationPattern;
96 case 'category':
97 return this.LabelValidationPattern;
98 default :
99 return null;
100 }
101 };
102
ys969316a9fce2020-01-19 13:50:02 +0200103 public static getPropertyListPatterns():IMapRegex {
Michael Landoed64b5e2017-06-09 03:19:04 +0300104 return {
ys969316a9fce2020-01-19 13:50:02 +0200105 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]+))*$/,
106 string: /^$|^"[\u0000-\u0021\u0023-\u00BF]+"(\s*,?\s*"[\u0000-\u0021\u0023-\u00BF]+")*$/,
107 boolean: /^$|^([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])(,?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]))*$/,
108 float: /^$|^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?(,?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?f?)*$/
Michael Landoed64b5e2017-06-09 03:19:04 +0300109 };
110 }
111
ys969316a9fce2020-01-19 13:50:02 +0200112 public static getPropertyMapPatterns():IMapRegex {
Michael Landoed64b5e2017-06-09 03:19:04 +0300113 return {
ys969316a9fce2020-01-19 13:50:02 +0200114 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]+)+)*$/,
115 string: /^$|^"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*"(\s*,?\s*"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*")*$/,
116 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]))*$/,
117 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?)*$/
Michael Landoed64b5e2017-06-09 03:19:04 +0300118 };
119 }
120
ys969316a9fce2020-01-19 13:50:02 +0200121 public static validateUniqueKeys(viewValue: string): boolean {
Michael Landoed64b5e2017-06-09 03:19:04 +0300122 if (!viewValue) {
123 return true; //allow empty value
124 }
125
126 let json:string = "{" + viewValue.replace(/\s\s+/g, ' ') + "}";
127 try {
128 let obj:any = JSON.parse(json);
129 /*
130 //Method #1 : check json string length before & after parsing
131 let newJson:string = JSON.stringify(obj);
132 if (newJson.length < json.length) {
133 return false;
134 }*/
135
136 //Method #2 : check how many times we can find "KEY": in json string
137 let result:boolean = true;
138 Object.keys(obj).forEach((key:string) => {
139 result = result && json.split('"' + key + '":').length === 2;
140 });
141 return result;
142
143 } catch (e) {
144 return false; //not a valid JSON
145 }
146
147 //return true;
148 }
149
150 public validateJson = (json:string):boolean => {
151 try {
152 JSON.parse(json);
153 return true;
154 } catch (err) {
155 console.log('invalid json');
156 return false;
157 }
158 };
159
160 public validateIntRange = (value:string):boolean => {
161
162 let base8 = new basePattern(/^([-+]?0o[0-7]+)$/, 8);
163 let base10 = new basePattern(/^(0|[-+]?[1-9][0-9]*)$/, 10);
164 let base16 = new basePattern(/^([-+]?0x[0-9a-fA-F]+)$/, 16);
165
166 let min:number = -0x80000000;
167 let max:number = 0x7fffffff;
168 let intPatterns:Array<basePattern> = [base8, base10, base16];
169 let matchedBase = _.find(intPatterns, (item)=> {
170 return item.pattern.test(value);
171 });
172
173 let parsed:number = parseInt(value.replace('o', ''), matchedBase.base);
174 if (parsed) {
175 return min <= parsed && max >= parsed;
176 }
177 }
178}