/*-------------------------------------------------------------------------------------------------------------- * Copyright (c) insite-gmbh. All rights reserved. * Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------------------------*/ export class PlcValidator { /** * checks if value is valid for the specified type and - if so - casts this type on value * - for all invalid values, a function (returning an error) is returned, so the returned * value can be checked by the calling function which can then create an appropriate error * message * @param value the value to be validated, always a string because it was set by the user * in a 'text'-type input * @param type the desired typeof(value) => the type of the returned value if the input is valid * @param arraysize the maximum size of the array if type == 'chararray' to which it will * be filled up with \u0000 (for other types, this value is not used) * * @returns a function if the value was invalid, else the formatted value (=> of specified type) */ public static formatAndValidate(value: string, type: string, arraysize: number): any { // typeof(invalid) is a function to enable returning false as valid boolean value let invalid = () => { return Error('invalid value'); }; switch(type.toLowerCase()) { case 'number': if (String(Number(value)) === value) { return Number(value); } else { return invalid; } case 'chararray': if (value.length <= arraysize) { let charArray = Array.from(value); while (charArray.length < arraysize) { charArray.push('\u0000'); // inflate array to same size as preserved space } return charArray.join(''); } else { console.log('value: ' + value+' - length: '+value.length+'- maxlength: '+arraysize); return invalid; } case 'string': return value; case 'boolean': return (value === 'true'); case 'date': let testDate = new Date(value); if (testDate.getSeconds() !== NaN) { return testDate; } else { return invalid; } default: return invalid; } } /** * checks if a specified value is in the range defined by min and max * if min and/or max are '', the respective checks will always return true * @param value the value to be checked * @param type the type for which the check should occur: * - number: casts Number(), then checks if min <= value <= max * - string/chararray: casts Number() on min & max and checks if * value.length() is in the resulting range * - date: casts new Date() on all other parameters and checks if * the date created with value is between the other two * - for all other types, true is returned * @param min the minimum value => value should be equal or higher * @param max the maximum value => value should be equal or lower * * @returns false, if the value did not meet all of the specified criteria, else true */ public static validMinMax(value: string, type: string, min: string, max: string): boolean { switch (type.toLowerCase()) { case 'number': if ((min !== '' && Number(value) < Number(min)) || (max !== '' && Number(value) > Number(max))) { // return false if min/max is defined and value is lower/higher respectively return false; } else { return true; } case 'string': case 'chararray': // this enables defining a length spectrum different to 0-arraysize (what is read from the plc) // for chararrays, e.g. when you want at least 2 symbols if ((min !== '' && value.length < Number(min)) || (max !== '' && value.length > Number(max))) { // return false if min/max is defined and length of value is out of range return false; } else { return true; } case 'date': let date = new Date(value); if (date.getSeconds() === NaN) { return false; // validate date to write } else { let mindate = new Date(min); let highenough = (min === '' || (mindate.getSeconds() !== NaN && mindate.valueOf() <= date.valueOf())); // highenough if min not defined or (min valid date and mindate smaller/equal than value) let maxdate = new Date(max); let lowenough = (max === '' || (maxdate.getSeconds() !== NaN && maxdate.valueOf() >= date.valueOf())); // lowenough if max not defined or (max valid date and maxdate higher/equal than value) return (highenough && lowenough); } default: return true; } } }