Fix certain default values breaking VFC
[sdc.git] / catalog-ui / src / app / utils / validation-utils.ts
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
21 import * as _ from 'lodash';
22
23 class 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
33 export interface IMapRegex {
34     integer: RegExp;
35     boolean: RegExp;
36     float: RegExp;
37     string: RegExp;
38 }
39
40 export 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     constructor(private IntegerNoLeadingZeroValidationPattern: RegExp,
55                 private FloatValidationPattern: RegExp,
56                 private CommentValidationPattern: RegExp,
57                 private BooleanValidationPattern: RegExp,
58                 private NumberValidationPattern: RegExp,
59                 private LabelValidationPattern: RegExp) {
60     }
61
62     public static getPropertyListPatterns(): IMapRegex {
63         return {
64             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]+))*$/,
65             string: /^$|^"[\u0000-\u0021\u0023-\u00BF]+"(\s*,?\s*"[\u0000-\u0021\u0023-\u00BF]+")*$/,
66             boolean: /^$|^([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee])(,?([Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]))*$/,
67             float: /^$|^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?(,?[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?f?)*$/
68         };
69     }
70
71     public static getPropertyMapPatterns(): IMapRegex {
72         return {
73             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]+)+)*$/,
74             string: /^$|^"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*"(\s*,?\s*"\w+"\s?:\s?"[\u0000-\u0021\u0023-\u00BF]*")*$/,
75             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]))*$/,
76             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?)*$/
77         };
78     }
79
80     public static validateUniqueKeys(viewValue: string): boolean {
81         if (!viewValue) {
82             return true; // allow empty value
83         }
84
85         const json: string = '{' + viewValue.replace(/\s\s+/g, ' ') + '}';
86         try {
87             const obj: any = JSON.parse(json);
88             /*
89              //Method #1 : check json string length before & after parsing
90              let newJson:string = JSON.stringify(obj);
91              if (newJson.length < json.length) {
92              return false;
93              }*/
94
95             // Method #2 : check how many times we can find "KEY": in json string
96             let result: boolean = true;
97             Object.keys(obj).forEach((key: string) => {
98                 result = result && json.split('"' + key + '":').length === 2;
99             });
100             return result;
101
102         } catch (e) {
103             return false; // not a valid JSON
104         }
105
106         // return true;
107     }
108
109     public stripAndSanitize(text: string): string {
110         if (!text) {
111             return null;
112         }
113         return text.replace(/\s+/g, ' ').replace(/%[A-Fa-f0-9]{2}/g, '')
114         .replace(/&/g, '&amp;').replace(/>/g, '&gt;')
115         .replace(/</g, '&lt;').replace(/"/g, '&quot;')
116         .replace(/'/g, '&apos;').trim();
117     }
118
119     public getValidationPattern = (validationType: string, parameterType?: string): RegExp => {
120         switch (validationType) {
121             case 'integer':
122                 return this.IntegerNoLeadingZeroValidationPattern;
123             case 'float':
124                 return this.FloatValidationPattern;
125             case 'number':
126                 return this.NumberValidationPattern;
127             case 'string':
128                 return this.CommentValidationPattern;
129             case 'boolean': {
130                 // Bug Fix DE197437 [Patch]Mismatch between BE to FE regarding supported characters in Boolean filed
131                 if (parameterType && parameterType === 'heat') {
132                     return this.heatBooleanValidationPattern;
133                 } else {
134                     return this.BooleanValidationPattern;
135                 }
136             }
137
138             case 'label':
139                 return this.LabelValidationPattern;
140             case 'category':
141                 return this.LabelValidationPattern;
142             default :
143                 return null;
144         }
145     }
146
147     public validateJson = (json: string): boolean => {
148         try {
149             JSON.parse(json);
150             return true;
151         } catch (err) {
152             console.log('invalid json');
153             return false;
154         }
155     }
156
157     public validateIntRange = (value: string): boolean => {
158
159         const base8 = new basePattern(/^([-+]?0o[0-7]+)$/, 8);
160         const base10 = new basePattern(/^(0|[-+]?[1-9][0-9]*)$/, 10);
161         const base16 = new basePattern(/^([-+]?0x[0-9a-fA-F]+)$/, 16);
162
163         const min: number = -0x80000000;
164         const max: number = 0x7fffffff;
165         const intPatterns: basePattern[] = [base8, base10, base16];
166         const matchedBase = _.find(intPatterns, (item) => {
167             return item.pattern.test(value);
168         });
169
170         const parsed: number = parseInt(value.replace('o', ''), matchedBase.base);
171         if (parsed) {
172             return min <= parsed && max >= parsed;
173         }
174     }
175 }