File tree 4 files changed +32
-3
lines changed
4 files changed +32
-3
lines changed Original file line number Diff line number Diff line change @@ -52,6 +52,9 @@ Supported types:
52
52
* `object'
53
53
* `null'
54
54
55
+ And nested ones, too.
56
+ ` Date ` instances are serialized with ` toISOString() ` .
57
+
55
58
## License
56
59
57
60
MIT
Original file line number Diff line number Diff line change @@ -14,12 +14,16 @@ const stringify = fastJson({
14
14
age : {
15
15
description : 'Age in years' ,
16
16
type : 'integer'
17
+ } ,
18
+ now : {
19
+ type : 'string'
17
20
}
18
21
}
19
22
} )
20
23
21
24
console . log ( stringify ( {
22
25
firstName : 'Matteo' ,
23
26
lastName : 'Collina' ,
24
- age : 32
27
+ age : 32 ,
28
+ now : new Date ( )
25
29
} ) )
Original file line number Diff line number Diff line change @@ -57,8 +57,13 @@ function $asNumber (i) {
57
57
}
58
58
}
59
59
60
- function $asString ( s ) {
61
- var str = s . toString ( )
60
+ function $asString ( str ) {
61
+ if ( str instanceof Date ) {
62
+ str = str . toISOString ( )
63
+ } else {
64
+ str = str . toString ( )
65
+ }
66
+
62
67
var result = ''
63
68
var last = 0
64
69
var l = str . length
Original file line number Diff line number Diff line change @@ -178,3 +178,20 @@ buildTest({
178
178
name : 'Dave'
179
179
} ]
180
180
} )
181
+
182
+ test ( 'render a a date in a string as JSON' , ( t ) => {
183
+ t . plan ( 2 )
184
+
185
+ const schema = {
186
+ title : 'a date in a string' ,
187
+ type : 'string'
188
+ }
189
+ const toStringify = new Date ( )
190
+
191
+ const validate = validator ( schema )
192
+ const stringify = build ( schema )
193
+ const output = stringify ( toStringify )
194
+
195
+ t . equal ( output , JSON . stringify ( toStringify ) )
196
+ t . ok ( validate ( JSON . parse ( output ) ) , 'valid schema' )
197
+ } )
You can’t perform that action at this time.
0 commit comments