Merge pull request #3363 from owenconti/bug/3361-non-required-integers
Fixes #3361 - Check for null and undefined values in validateParam
This commit is contained in:
@@ -451,13 +451,13 @@ export const propChecker = (props, nextProps, objectList=[], ignoreList=[]) => {
|
||||
}
|
||||
|
||||
export const validateNumber = ( val ) => {
|
||||
if ( !/^-?\d+(\.?\d+)?$/.test(val)) {
|
||||
if (!/^-?\d+(\.?\d+)?$/.test(val)) {
|
||||
return "Value must be a number"
|
||||
}
|
||||
}
|
||||
|
||||
export const validateInteger = ( val ) => {
|
||||
if ( !/^-?\d+$/.test(val)) {
|
||||
if (!/^-?\d+$/.test(val)) {
|
||||
return "Value must be an integer"
|
||||
}
|
||||
}
|
||||
@@ -485,6 +485,10 @@ export const validateParam = (param, isXml) => {
|
||||
return errors
|
||||
}
|
||||
|
||||
if ( value === null || value === undefined ) {
|
||||
return errors
|
||||
}
|
||||
|
||||
if ( type === "number" ) {
|
||||
let err = validateNumber(value)
|
||||
if (!err) return errors
|
||||
|
||||
@@ -214,6 +214,7 @@ describe("utils", function(){
|
||||
})
|
||||
|
||||
it("validates numbers", function() {
|
||||
// string instead of a number
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: "number",
|
||||
@@ -221,9 +222,28 @@ describe("utils", function(){
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
expect( result ).toEqual( ["Value must be a number"] )
|
||||
|
||||
// undefined value
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: "number",
|
||||
value: undefined
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
expect( result ).toEqual( [] )
|
||||
|
||||
// null value
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: "number",
|
||||
value: null
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
expect( result ).toEqual( [] )
|
||||
})
|
||||
|
||||
it("validates integers", function() {
|
||||
// string instead of integer
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: "integer",
|
||||
@@ -231,6 +251,24 @@ describe("utils", function(){
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
expect( result ).toEqual( ["Value must be an integer"] )
|
||||
|
||||
// undefined value
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: "integer",
|
||||
value: undefined
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
expect( result ).toEqual( [] )
|
||||
|
||||
// null value
|
||||
param = fromJS({
|
||||
required: false,
|
||||
type: "integer",
|
||||
value: null
|
||||
})
|
||||
result = validateParam( param, false )
|
||||
expect( result ).toEqual( [] )
|
||||
})
|
||||
|
||||
it("validates arrays", function() {
|
||||
|
||||
Reference in New Issue
Block a user