区间验证

dew*_*ewe 5 javascript validation

我们有三个不同的网页,其中包含用于添加主实体细节的网格.

详细对象可以用javascript表示

var detail = {
     Description: 'Detail',
     MinPercentage: 0,
     MaxPercentage: 20
}
Run Code Online (Sandbox Code Playgroud)

现在,我们想在发送到服务器之前验证这些细节.

验证

  • 一定不能有任何交集.即细节('Detail1',0,20)和细节('Detail2',15,30)无效,因为15到20之间是共同的.
  • 详细信息将值从给定的最小值保持为给定的最大值.iedetail('Detail1',0,20)和detail('Detail2',20,40)保持0到40之间的值.如果给定minimum是0并且给定最大值是40,则它们是有效的.

来自功能的期望

  • 因为我想编写一个在多个地方使用的函数,所以应该尽可能通用.

然后,我写了一个名为areIntervalsValid的函数,但我不知道如何处理错误输入的调用,抛出异常,返回最佳结构化结果,我也想知道执行验证的最佳方法是什么.


// Returns array of detail object to test.
var getDetails = function () {
    var detail1 = { Description: 'Detail1', MinPercentage: 0, MaxPercentage: 20 }
    var detail2 = { Description: 'Detail2', MinPercentage: 40, MaxPercentage: 60 }
    var detail3 = { Description: 'Detail3', MinPercentage: 60, MaxPercentage: 72 }
    var detail4 = { Description: 'Detail4', MinPercentage: 72, MaxPercentage: 100 }
    var detail5 = { Description: 'Detail5', MinPercentage: 20, MaxPercentage: 40 }

    return new Array(detail1, detail2, detail3, detail4, detail5);
}

// Performs type checking, logical validation, and requirements validation.
var areIntervalsValid = function (items, min, max, minProperty, maxProperty) {
    // Returned object.
    var result = {
        Success: false,
        Message: ''
    }

    // Checks arguments have expected types.
    var validateFunctionCall = function () {
        if (!Array.isArray(items) || typeof min !== 'number' || typeof max !== 'number' || typeof minProperty !== 'string' || typeof maxProperty !== 'string')
            throw 'An error occurred while processing validation.';
        if (!items.length || min > max)
            throw 'An error occurred while processing validation.';
    }

    // Checks [minProperty] of detail that has minimum [minProperty] == min
    // and [maxProperty] of detail that has maximum [minProperty]
    var validateIntervalBasics = function () {
        if (items[0][minProperty] != min || items[items.length - 1][maxProperty] != max)
            throw 'Start and end values of interval do not match minimum - maximum values.';
    }

    // Checks @item has [minProperty] and [maxProperty].
    var validateHasProperty = function (item) {
        if (!item.hasOwnProperty(minProperty) || !item.hasOwnProperty(maxProperty)) {
            throw 'An error occurred while processing validation.';
        }
    }

    try {
        validateFunctionCall();

        // Sorts array of details in according to [minProperty].
        items.sort(function (item1, item2) { return item1[minProperty] > item2[minProperty] });

        validateIntervalBasics();

        var totalDiff = 0, currentItem;

        // Algorithm part. 
        for (var i = 0; i < items.length; i++) {
            currentItem = items[i];
            validateHasProperty(currentItem);
            totalDiff += currentItem[maxProperty] - currentItem[minProperty];
            if (i != items.length - 1 && currentItem[maxProperty] > items[i + 1][minProperty]) { // Finds intersections.
                throw "There are intersected values: " + currentItem[maxProperty] + " - " + items[i + 1][minProperty];
            }
        }

        // Checks second validation.
        if (totalDiff != max - min) {
            throw 'Total interval sum is not equal to ' + (max - min);
        }

        result.Success = true;

        return result;

    } catch (e) {
        console.log(e);
        result.Message = e;

        return result;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我调用这样的函数:

areIntervalsValid(getDetails(), 0, 100, "MinPercentage", "MaxPercentage");
Run Code Online (Sandbox Code Playgroud)

我该怎样做才能使功能更可靠,更通用,更快速?