Skip to content

Commit 81b2aa7

Browse files
authored
Merge pull request #3363 from owenconti/bug/3361-non-required-integers
Fixes #3361 - Check for null and undefined values in validateParam
2 parents 8f752ad + b941882 commit 81b2aa7

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

src/core/utils.js

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ export function highlight (el) {
228228

229229
var reset = function(el) {
230230
var text = el.textContent,
231-
pos = 0, // current position
231+
pos = 0, // current position
232232
next1 = text[0], // next character
233-
chr = 1, // current character
234-
prev1, // previous character
235-
prev2, // the one before the previous
236-
token = // current token content
237-
el.innerHTML = "", // (and cleaning the node)
233+
chr = 1, // current character
234+
prev1, // previous character
235+
prev2, // the one before the previous
236+
token = // current token content
237+
el.innerHTML = "", // (and cleaning the node)
238238

239239
// current token type:
240240
// 0: anything else (whitespaces / newlines)
@@ -274,11 +274,11 @@ export function highlight (el) {
274274
(tokenType > 8 && chr == "\n") ||
275275
[ // finalize conditions for other token types
276276
// 0: whitespaces
277-
/\S/[test](chr), // merged together
277+
/\S/[test](chr), // merged together
278278
// 1: operators
279-
1, // consist of a single character
279+
1, // consist of a single character
280280
// 2: braces
281-
1, // consist of a single character
281+
1, // consist of a single character
282282
// 3: (key)word
283283
!/[$\w]/[test](chr),
284284
// 4: regex
@@ -341,12 +341,12 @@ export function highlight (el) {
341341
// condition)
342342
tokenType = 11
343343
while (![
344-
1, // 0: whitespace
344+
1, // 0: whitespace
345345
// 1: operator or braces
346-
/[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr), // eslint-disable-line no-useless-escape
347-
/[\])]/[test](chr), // 2: closing brace
348-
/[$\w]/[test](chr), // 3: (key)word
349-
chr == "/" && // 4: regex
346+
/[\/{}[(\-+*=<>:;|\\.,?!&@~]/[test](chr), // eslint-disable-line no-useless-escape
347+
/[\])]/[test](chr), // 2: closing brace
348+
/[$\w]/[test](chr), // 3: (key)word
349+
chr == "/" && // 4: regex
350350
// previous token was an
351351
// opening brace or an
352352
// operator (otherwise
@@ -355,13 +355,13 @@ export function highlight (el) {
355355
// workaround for xml
356356
// closing tags
357357
prev1 != "<",
358-
chr == "\"", // 5: string with "
359-
chr == "'", // 6: string with '
358+
chr == "\"", // 5: string with "
359+
chr == "'", // 6: string with '
360360
// 7: xml comment
361361
chr+next1+text[pos+1]+text[pos+2] == "<!--",
362-
chr+next1 == "/*", // 8: multiline comment
363-
chr+next1 == "//", // 9: single-line comment
364-
chr == "#" // 10: hash-style comment
362+
chr+next1 == "/*", // 8: multiline comment
363+
chr+next1 == "//", // 9: single-line comment
364+
chr == "#" // 10: hash-style comment
365365
][--tokenType]);
366366
}
367367

@@ -451,13 +451,13 @@ export const propChecker = (props, nextProps, objectList=[], ignoreList=[]) => {
451451
}
452452

453453
export const validateNumber = ( val ) => {
454-
if ( !/^-?\d+(\.?\d+)?$/.test(val)) {
454+
if (!/^-?\d+(\.?\d+)?$/.test(val)) {
455455
return "Value must be a number"
456456
}
457457
}
458458

459459
export const validateInteger = ( val ) => {
460-
if ( !/^-?\d+$/.test(val)) {
460+
if (!/^-?\d+$/.test(val)) {
461461
return "Value must be an integer"
462462
}
463463
}
@@ -485,6 +485,10 @@ export const validateParam = (param, isXml) => {
485485
return errors
486486
}
487487

488+
if ( value === null || value === undefined ) {
489+
return errors
490+
}
491+
488492
if ( type === "number" ) {
489493
let err = validateNumber(value)
490494
if (!err) return errors

test/core/utils.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,61 @@ describe("utils", function(){
214214
})
215215

216216
it("validates numbers", function() {
217+
// string instead of a number
217218
param = fromJS({
218219
required: false,
219220
type: "number",
220221
value: "test"
221222
})
222223
result = validateParam( param, false )
223224
expect( result ).toEqual( ["Value must be a number"] )
225+
226+
// undefined value
227+
param = fromJS({
228+
required: false,
229+
type: "number",
230+
value: undefined
231+
})
232+
result = validateParam( param, false )
233+
expect( result ).toEqual( [] )
234+
235+
// null value
236+
param = fromJS({
237+
required: false,
238+
type: "number",
239+
value: null
240+
})
241+
result = validateParam( param, false )
242+
expect( result ).toEqual( [] )
224243
})
225244

226245
it("validates integers", function() {
246+
// string instead of integer
227247
param = fromJS({
228248
required: false,
229249
type: "integer",
230250
value: "test"
231251
})
232252
result = validateParam( param, false )
233253
expect( result ).toEqual( ["Value must be an integer"] )
254+
255+
// undefined value
256+
param = fromJS({
257+
required: false,
258+
type: "integer",
259+
value: undefined
260+
})
261+
result = validateParam( param, false )
262+
expect( result ).toEqual( [] )
263+
264+
// null value
265+
param = fromJS({
266+
required: false,
267+
type: "integer",
268+
value: null
269+
})
270+
result = validateParam( param, false )
271+
expect( result ).toEqual( [] )
234272
})
235273

236274
it("validates arrays", function() {

0 commit comments

Comments
 (0)