Validate correct Number format
* src/validate/formatter/Number.js (parse): Validate number format. * test/validate/formatter/NumberTest.js: Modify accordingly.master
parent
5947e7646e
commit
ab3f5f4cb6
|
@ -109,11 +109,19 @@ module.exports = Trait( 'Number' )
|
||||||
* @param {string} data data to parse
|
* @param {string} data data to parse
|
||||||
*
|
*
|
||||||
* @return {string} data formatted for storage
|
* @return {string} data formatted for storage
|
||||||
|
*
|
||||||
|
* @throws Error if number is not of a valid format
|
||||||
*/
|
*/
|
||||||
'virtual abstract override public parse': function( data )
|
'virtual abstract override public parse': function( data )
|
||||||
{
|
{
|
||||||
var cleaned = this.__super( data ).replace( /[ ,]/g, '' ),
|
var cleaned = this.__super( data ).replace( /[ ,]/g, '' );
|
||||||
parts = this.split( cleaned );
|
|
||||||
|
if ( !/^[0-9]*(\.[0-9]*)?$/.test( cleaned ) )
|
||||||
|
{
|
||||||
|
throw Error( "Invalid number: " + data );
|
||||||
|
}
|
||||||
|
|
||||||
|
var parts = this.split( cleaned );
|
||||||
|
|
||||||
return parts.integer + this.scale( parts.significand, this._scale );
|
return parts.integer + this.scale( parts.significand, this._scale );
|
||||||
},
|
},
|
||||||
|
|
|
@ -42,6 +42,10 @@ describe( 'validate.formatter.Number', function()
|
||||||
// strip decimals
|
// strip decimals
|
||||||
"1.234": [ "1", "1" ],
|
"1.234": [ "1", "1" ],
|
||||||
" 1, ,.": [ "1", "1" ],
|
" 1, ,.": [ "1", "1" ],
|
||||||
|
|
||||||
|
// non-numbers
|
||||||
|
"foo": false,
|
||||||
|
"123foo": false,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,6 +66,10 @@ describe( 'validate.formatter.Number', function()
|
||||||
|
|
||||||
"12,345": [ "12345.000", "12,345.000" ],
|
"12,345": [ "12345.000", "12,345.000" ],
|
||||||
" 1, ,.": [ "1.000", "1.000" ],
|
" 1, ,.": [ "1.000", "1.000" ],
|
||||||
|
|
||||||
|
// non-numbers
|
||||||
|
"1.foo": false,
|
||||||
|
"123foo.012": false,
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue