Fixes #3361 - Check for null and undefined values in validateParam
This commit is contained in:
@@ -228,13 +228,13 @@ export function highlight (el) {
|
|||||||
|
|
||||||
var reset = function(el) {
|
var reset = function(el) {
|
||||||
var text = el.textContent,
|
var text = el.textContent,
|
||||||
pos = 0, // current position
|
pos = 0, // current position
|
||||||
next1 = text[0], // next character
|
next1 = text[0], // next character
|
||||||
chr = 1, // current character
|
chr = 1, // current character
|
||||||
prev1, // previous character
|
prev1, // previous character
|
||||||
prev2, // the one before the previous
|
prev2, // the one before the previous
|
||||||
token = // current token content
|
token = // current token content
|
||||||
el.innerHTML = "", // (and cleaning the node)
|
el.innerHTML = "", // (and cleaning the node)
|
||||||
|
|
||||||
// current token type:
|
// current token type:
|
||||||
// 0: anything else (whitespaces / newlines)
|
// 0: anything else (whitespaces / newlines)
|
||||||
@@ -274,11 +274,11 @@ export function highlight (el) {
|
|||||||
(tokenType > 8 && chr == "\n") ||
|
(tokenType > 8 && chr == "\n") ||
|
||||||
[ // finalize conditions for other token types
|
[ // finalize conditions for other token types
|
||||||
// 0: whitespaces
|
// 0: whitespaces
|
||||||
/\S/[test](chr), // merged together
|
/\S/[test](chr), // merged together
|
||||||
// 1: operators
|
// 1: operators
|
||||||
1, // consist of a single character
|
1, // consist of a single character
|
||||||
// 2: braces
|
// 2: braces
|
||||||
1, // consist of a single character
|
1, // consist of a single character
|
||||||
// 3: (key)word
|
// 3: (key)word
|
||||||
!/[$\w]/[test](chr),
|
!/[$\w]/[test](chr),
|
||||||
// 4: regex
|
// 4: regex
|
||||||
@@ -341,12 +341,12 @@ export function highlight (el) {
|
|||||||
// condition)
|
// condition)
|
||||||
tokenType = 11
|
tokenType = 11
|
||||||
while (![
|
while (![
|
||||||
1, // 0: whitespace
|
1, // 0: whitespace
|
||||||
// 1: operator or braces
|
// 1: operator or braces
|
||||||
/[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr), // eslint-disable-line no-useless-escape
|
/[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr), // eslint-disable-line no-useless-escape
|
||||||
/[\])]/[test](chr), // 2: closing brace
|
/[\])]/[test](chr), // 2: closing brace
|
||||||
/[$\w]/[test](chr), // 3: (key)word
|
/[$\w]/[test](chr), // 3: (key)word
|
||||||
chr == "/" && // 4: regex
|
chr == "/" && // 4: regex
|
||||||
// previous token was an
|
// previous token was an
|
||||||
// opening brace or an
|
// opening brace or an
|
||||||
// operator (otherwise
|
// operator (otherwise
|
||||||
@@ -355,13 +355,13 @@ export function highlight (el) {
|
|||||||
// workaround for xml
|
// workaround for xml
|
||||||
// closing tags
|
// closing tags
|
||||||
prev1 != "<",
|
prev1 != "<",
|
||||||
chr == "\"", // 5: string with "
|
chr == "\"", // 5: string with "
|
||||||
chr == "'", // 6: string with '
|
chr == "'", // 6: string with '
|
||||||
// 7: xml comment
|
// 7: xml comment
|
||||||
chr+next1+text[pos+1]+text[pos+2] == "<!--",
|
chr+next1+text[pos+1]+text[pos+2] == "<!--",
|
||||||
chr+next1 == "/*", // 8: multiline comment
|
chr+next1 == "/*", // 8: multiline comment
|
||||||
chr+next1 == "//", // 9: single-line comment
|
chr+next1 == "//", // 9: single-line comment
|
||||||
chr == "#" // 10: hash-style comment
|
chr == "#" // 10: hash-style comment
|
||||||
][--tokenType]);
|
][--tokenType]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -451,13 +451,13 @@ export const propChecker = (props, nextProps, objectList=[], ignoreList=[]) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const validateInteger = ( val ) => {
|
export const validateInteger = ( val ) => {
|
||||||
if ( !/^-?\d+$/.test(val)) {
|
if (!/^-?\d+$/.test(val)) {
|
||||||
return "Value must be an integer"
|
return "Value must be an integer"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -485,6 +485,10 @@ export const validateParam = (param, isXml) => {
|
|||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( value === null || value === undefined ) {
|
||||||
|
return errors
|
||||||
|
}
|
||||||
|
|
||||||
if ( type === "number" ) {
|
if ( type === "number" ) {
|
||||||
let err = validateNumber(value)
|
let err = validateNumber(value)
|
||||||
if (!err) return errors
|
if (!err) return errors
|
||||||
|
|||||||
@@ -214,6 +214,7 @@ describe("utils", function(){
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("validates numbers", function() {
|
it("validates numbers", function() {
|
||||||
|
// string instead of a number
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: false,
|
required: false,
|
||||||
type: "number",
|
type: "number",
|
||||||
@@ -221,9 +222,28 @@ describe("utils", function(){
|
|||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Value must be a number"] )
|
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() {
|
it("validates integers", function() {
|
||||||
|
// string instead of integer
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: false,
|
required: false,
|
||||||
type: "integer",
|
type: "integer",
|
||||||
@@ -231,6 +251,24 @@ describe("utils", function(){
|
|||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Value must be an integer"] )
|
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() {
|
it("validates arrays", function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user