codekingpro/portable-devtools
114k
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10import {sprintf} from 'sources/utils';11import _ from 'lodash';12import pgAdmin from 'sources/pgadmin';13 14/* Validate value for min max range */15export function minMaxValidator(label, value, minValue, maxValue) {16 if((_.isUndefined(value) || _.isNull(value) || String(value) === ''))17 return null;18 if (!_.isUndefined(minValue) && value < minValue) {19 return sprintf(pgAdmin.Browser.messages.MUST_GR_EQ, label, minValue);20 } else if (!_.isUndefined(maxValue) && value > maxValue) {21 return sprintf(pgAdmin.Browser.messages.MUST_LESS_EQ, label, maxValue);22 }23 return null;24}25 26/* Validate value to check if it is a number */27export function numberValidator(label, value) {28 if((_.isUndefined(value) || _.isNull(value) || String(value) === ''))29 return null;30 if (!/^-?\d+(\.?\d*)$/.test(value)) {31 return sprintf(pgAdmin.Browser.messages.MUST_BE_NUM, label);32 }33 return null;34}35 36/* Validate value to check if it is an integer */37export function integerValidator(label, value) {38 if((_.isUndefined(value) || _.isNull(value) || String(value) === ''))39 return null;40 if (!/^-?\d*$/.test(value)) {41 return sprintf(pgAdmin.Browser.messages.MUST_BE_INT, label);42 }43 return null;44}45 46/* Validate value to check if it is empty */47export function emptyValidator(label, value) {48 if(isEmptyString(value)) {49 return sprintf(pgAdmin.Browser.messages.CANNOT_BE_EMPTY, label);50 }51 return null;52}53 54export function isEmptyString(value) {55 return _.isUndefined(value) || _.isNull(value) || String(value).trim() === '' || String(value).replace(/(^\s+)|(\s+$)/g, '') == '';56}57 58/* Validate rows to check for any duplicate rows based on uniqueCols-columns array */59export function checkUniqueCol(rows, uniqueCols) {60 if(uniqueCols) {61 for(const [i, row] of rows.entries()) {62 for(const checkRow of rows.slice(0, i)) {63 if(_.isEqual(_.pick(checkRow, uniqueCols), _.pick(row, uniqueCols))) return i;64 }65 }66 }67 return -1;68}69 