Explorar el Código

Added validation of input parameters.

ilgizar hace 7 años
padre
commit
230f018c78

+ 12 - 0
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);

+ 11 - 0
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);
+    });
+  });
 });