1
0
Fork 0

Validate correct Number format

* src/validate/formatter/Number.js
  (parse): Validate number format.

* test/validate/formatter/NumberTest.js: Modify accordingly.
master
Mike Gerwitz 2016-11-30 12:10:38 -05:00
parent 5947e7646e
commit ab3f5f4cb6
2 changed files with 18 additions and 2 deletions

View File

@ -109,11 +109,19 @@ module.exports = Trait( 'Number' )
* @param {string} data data to parse
*
* @return {string} data formatted for storage
*
* @throws Error if number is not of a valid format
*/
'virtual abstract override public parse': function( data )
{
var cleaned = this.__super( data ).replace( /[ ,]/g, '' ),
parts = this.split( cleaned );
var cleaned = this.__super( data ).replace( /[ ,]/g, '' );
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 );
},

View File

@ -42,6 +42,10 @@ describe( 'validate.formatter.Number', function()
// strip decimals
"1.234": [ "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" ],
" 1, ,.": [ "1.000", "1.000" ],
// non-numbers
"1.foo": false,
"123foo.012": false,
} );