Merge pull request #3703 from heldersepu/ft/validation
Add validateMaximum & validateMinimum
This commit is contained in:
@@ -470,6 +470,18 @@ export const propChecker = (props, nextProps, objectList=[], ignoreList=[]) => {
|
|||||||
|| objectList.some( objectPropName => !eq(props[objectPropName], nextProps[objectPropName])))
|
|| objectList.some( objectPropName => !eq(props[objectPropName], nextProps[objectPropName])))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const validateMaximum = ( val, max ) => {
|
||||||
|
if (val > max) {
|
||||||
|
return "Value must be less than Maximum"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const validateMinimum = ( val, min ) => {
|
||||||
|
if (val < min) {
|
||||||
|
return "Value must be greater than Minimum"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const validateNumber = ( val ) => {
|
export const validateNumber = ( val ) => {
|
||||||
if (!/^-?\d+(\.?\d+)?$/.test(val)) {
|
if (!/^-?\d+(\.?\d+)?$/.test(val)) {
|
||||||
return "Value must be a number"
|
return "Value must be a number"
|
||||||
@@ -529,6 +541,8 @@ export const validateParam = (param, isXml) => {
|
|||||||
let errors = []
|
let errors = []
|
||||||
let value = isXml && param.get("in") === "body" ? param.get("value_xml") : param.get("value")
|
let value = isXml && param.get("in") === "body" ? param.get("value_xml") : param.get("value")
|
||||||
let required = param.get("required")
|
let required = param.get("required")
|
||||||
|
let maximum = param.get("maximum")
|
||||||
|
let minimum = param.get("minimum")
|
||||||
let type = param.get("type")
|
let type = param.get("type")
|
||||||
let format = param.get("format")
|
let format = param.get("format")
|
||||||
let maxLength = param.get("maxLength")
|
let maxLength = param.get("maxLength")
|
||||||
@@ -564,6 +578,16 @@ export const validateParam = (param, isXml) => {
|
|||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (maximum || maximum === 0) {
|
||||||
|
let err = validateMaximum(value, maximum)
|
||||||
|
if (err) errors.push(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minimum || minimum === 0) {
|
||||||
|
let err = validateMinimum(value, minimum)
|
||||||
|
if (err) errors.push(err)
|
||||||
|
}
|
||||||
|
|
||||||
if ( type === "string" ) {
|
if ( type === "string" ) {
|
||||||
let err
|
let err
|
||||||
if (format === "date-time") {
|
if (format === "date-time") {
|
||||||
|
|||||||
@@ -1,7 +1,23 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
import expect from "expect"
|
import expect from "expect"
|
||||||
import { fromJS, OrderedMap } from "immutable"
|
import { fromJS, OrderedMap } from "immutable"
|
||||||
import { mapToList, validateMinLength, validateMaxLength, validateDateTime, validateGuid, validateNumber, validateInteger, validateParam, validateFile, fromJSOrdered, getAcceptControllingResponse, createDeepLinkPath, escapeDeepLinkPath } from "core/utils"
|
import {
|
||||||
|
mapToList,
|
||||||
|
validateMinLength,
|
||||||
|
validateMaxLength,
|
||||||
|
validateDateTime,
|
||||||
|
validateGuid,
|
||||||
|
validateNumber,
|
||||||
|
validateInteger,
|
||||||
|
validateParam,
|
||||||
|
validateFile,
|
||||||
|
validateMaximum,
|
||||||
|
validateMinimum,
|
||||||
|
fromJSOrdered,
|
||||||
|
getAcceptControllingResponse,
|
||||||
|
createDeepLinkPath,
|
||||||
|
escapeDeepLinkPath
|
||||||
|
} from "core/utils"
|
||||||
import win from "core/window"
|
import win from "core/window"
|
||||||
|
|
||||||
describe("utils", function() {
|
describe("utils", function() {
|
||||||
@@ -72,6 +88,36 @@ describe("utils", function() {
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("validateMaximum", function() {
|
||||||
|
let errorMessage = "Value must be less than Maximum"
|
||||||
|
|
||||||
|
it("doesn't return for valid input", function() {
|
||||||
|
expect(validateMaximum(9, 10)).toBeFalsy()
|
||||||
|
expect(validateMaximum(19, 20)).toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns a message for invalid input", function() {
|
||||||
|
expect(validateMaximum(1, 0)).toEqual(errorMessage)
|
||||||
|
expect(validateMaximum(10, 9)).toEqual(errorMessage)
|
||||||
|
expect(validateMaximum(20, 19)).toEqual(errorMessage)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("validateMinimum", function() {
|
||||||
|
let errorMessage = "Value must be greater than Minimum"
|
||||||
|
|
||||||
|
it("doesn't return for valid input", function() {
|
||||||
|
expect(validateMinimum(2, 1)).toBeFalsy()
|
||||||
|
expect(validateMinimum(20, 10)).toBeFalsy()
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns a message for invalid input", function() {
|
||||||
|
expect(validateMinimum(-1, 0)).toEqual(errorMessage)
|
||||||
|
expect(validateMinimum(1, 2)).toEqual(errorMessage)
|
||||||
|
expect(validateMinimum(10, 20)).toEqual(errorMessage)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("validateNumber", function() {
|
describe("validateNumber", function() {
|
||||||
let errorMessage = "Value must be a number"
|
let errorMessage = "Value must be a number"
|
||||||
|
|
||||||
@@ -558,14 +604,47 @@ describe("utils", function() {
|
|||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
// valid number
|
// valid number with min and max
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: true,
|
||||||
type: "number",
|
type: "number",
|
||||||
value: 10
|
value: 10,
|
||||||
|
minimum: 5,
|
||||||
|
maximum: 99
|
||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [] )
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// valid negative number with min and max
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: -10,
|
||||||
|
minimum: -50,
|
||||||
|
maximum: -5
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// invalid number with maximum:0
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: 1,
|
||||||
|
maximum: 0
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be less than Maximum"] )
|
||||||
|
|
||||||
|
// invalid number with minimum:0
|
||||||
|
param = fromJS({
|
||||||
|
required: true,
|
||||||
|
type: "number",
|
||||||
|
value: -10,
|
||||||
|
minimum: 0
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be greater than Minimum"] )
|
||||||
})
|
})
|
||||||
|
|
||||||
it("validates optional numbers", function() {
|
it("validates optional numbers", function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user