File tree 2 files changed +30
-9
lines changed
2 files changed +30
-9
lines changed Original file line number Diff line number Diff line change @@ -50,17 +50,14 @@ module.exports = class Serializer {
50
50
}
51
51
52
52
asNumber ( i ) {
53
- if ( typeof i !== 'number' ) {
54
- i = Number ( i )
55
- }
56
- // NaN !== NaN
57
- if ( i !== i ) { // eslint-disable-line no-self-compare
53
+ const num = Number ( i )
54
+ if ( Number . isNaN ( num ) ) {
58
55
throw new Error ( `The value "${ i } " cannot be converted to a number.` )
56
+ } else if ( ! Number . isFinite ( num ) ) {
57
+ return null
58
+ } else {
59
+ return '' + num
59
60
}
60
- if ( i === Infinity || i === - Infinity ) {
61
- return 'null'
62
- }
63
- return '' + i
64
61
}
65
62
66
63
asBoolean ( bool ) {
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ const t = require ( 'tap' )
4
+ const fjs = require ( '..' )
5
+ const schema = {
6
+ type : 'object' ,
7
+ properties : {
8
+ fullName : { type : 'string' } ,
9
+ phone : { type : 'number' }
10
+ }
11
+ }
12
+
13
+ const input = {
14
+ fullName : 'Jone' ,
15
+ phone : 'phone'
16
+ }
17
+
18
+ const render = fjs ( schema )
19
+
20
+ try {
21
+ render ( input )
22
+ } catch ( err ) {
23
+ t . equal ( err . message , 'The value "phone" cannot be converted to a number.' )
24
+ }
You can’t perform that action at this time.
0 commit comments