From 230f018c7820542162e7ae4f9e52bf56fcd7011a Mon Sep 17 00:00:00 2001 From: ilgizar Date: Tue, 20 Mar 2018 15:37:18 +0500 Subject: [PATCH] Added validation of input parameters. --- public/app/plugins/panel/graph/align_yaxes.ts | 12 ++++++++++++ .../plugins/panel/graph/specs/align_yaxes.jest.ts | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/public/app/plugins/panel/graph/align_yaxes.ts b/public/app/plugins/panel/graph/align_yaxes.ts index b60d75e7b66..71bfcd8423d 100644 --- a/public/app/plugins/panel/graph/align_yaxes.ts +++ b/public/app/plugins/panel/graph/align_yaxes.ts @@ -6,6 +6,10 @@ import _ from 'lodash'; * @param align Y level */ export function alignYLevel(yaxis, alignLevel) { + if (isNaN(alignLevel) || !checkCorrectAxis(yaxis)) { + return; + } + var [yLeft, yRight] = yaxis; moveLevelToZero(yLeft, yRight, alignLevel); @@ -92,6 +96,14 @@ function restoreLevelFromZero(yLeft, yRight, alignLevel) { } } +function checkCorrectAxis(axis) { + return axis.length === 2 && checkCorrectAxes(axis[0]) && checkCorrectAxes(axis[1]); +} + +function checkCorrectAxes(axes) { + return 'min' in axes && 'max' in axes; +} + function checkOneSide(yLeft, yRight) { // on the one hand with respect to zero return (yLeft.min >= 0 && yRight.min >= 0) || (yLeft.max <= 0 && yRight.max <= 0); diff --git a/public/app/plugins/panel/graph/specs/align_yaxes.jest.ts b/public/app/plugins/panel/graph/specs/align_yaxes.jest.ts index ff540fd223f..963ecfbfa1f 100644 --- a/public/app/plugins/panel/graph/specs/align_yaxes.jest.ts +++ b/public/app/plugins/panel/graph/specs/align_yaxes.jest.ts @@ -197,4 +197,15 @@ describe('Graph Y axes aligner', function() { expect(yaxes).toMatchObject(expected); }); }); + + describe('on level not number value', () => { + it('Should ignore without errors', () => { + alignY = 'q'; + yaxes = [{ min: 5, max: 10 }, { min: 2, max: 4 }]; + expected = [{ min: 5, max: 10 }, { min: 2, max: 4 }]; + + alignYLevel(yaxes, alignY); + expect(yaxes).toMatchObject(expected); + }); + }); });