@@ -544,3 +544,95 @@ test('non-date format should not affect data serialization (issue #491)', (t) =>
544
544
const data = { hello : 123n }
545
545
t . equal ( stringify ( data ) , '{"hello":"123"}' )
546
546
} )
547
+
548
+ test ( 'should serialize also an invalid string value, even if it is not a valid date' , ( t ) => {
549
+ t . plan ( 2 )
550
+
551
+ const schema = {
552
+ title : 'a date in a string' ,
553
+ type : 'string' ,
554
+ format : 'date-time' ,
555
+ nullable : true
556
+ }
557
+ const toStringify = 'invalid'
558
+
559
+ const validate = validator ( schema )
560
+ const stringify = build ( schema )
561
+ const output = stringify ( toStringify )
562
+
563
+ t . equal ( output , JSON . stringify ( toStringify ) )
564
+ t . not ( validate ( JSON . parse ( output ) ) , 'valid schema' )
565
+ } )
566
+
567
+ test ( 'should throw an error if value can not be transformed to date-time' , ( t ) => {
568
+ t . plan ( 2 )
569
+
570
+ const schema = {
571
+ title : 'a date in a string' ,
572
+ type : 'string' ,
573
+ format : 'date-time' ,
574
+ nullable : true
575
+ }
576
+ const toStringify = true
577
+
578
+ const validate = validator ( schema )
579
+ const stringify = build ( schema )
580
+
581
+ t . throws ( ( ) => stringify ( toStringify ) , new Error ( 'The value "true" cannot be converted to a date-time.' ) )
582
+ t . not ( validate ( toStringify ) )
583
+ } )
584
+
585
+ test ( 'should throw an error if value can not be transformed to date' , ( t ) => {
586
+ t . plan ( 2 )
587
+
588
+ const schema = {
589
+ title : 'a date in a string' ,
590
+ type : 'string' ,
591
+ format : 'date' ,
592
+ nullable : true
593
+ }
594
+ const toStringify = true
595
+
596
+ const validate = validator ( schema )
597
+ const stringify = build ( schema )
598
+
599
+ t . throws ( ( ) => stringify ( toStringify ) , new Error ( 'The value "true" cannot be converted to a date.' ) )
600
+ t . not ( validate ( toStringify ) )
601
+ } )
602
+
603
+ test ( 'should throw an error if value can not be transformed to time' , ( t ) => {
604
+ t . plan ( 2 )
605
+
606
+ const schema = {
607
+ title : 'a time in a string' ,
608
+ type : 'string' ,
609
+ format : 'time' ,
610
+ nullable : true
611
+ }
612
+ const toStringify = true
613
+
614
+ const validate = validator ( schema )
615
+ const stringify = build ( schema )
616
+
617
+ t . throws ( ( ) => stringify ( toStringify ) , new Error ( 'The value "true" cannot be converted to a time.' ) )
618
+ t . not ( validate ( toStringify ) )
619
+ } )
620
+
621
+ test ( 'should serialize also an invalid string value, even if it is not a valid time' , ( t ) => {
622
+ t . plan ( 2 )
623
+
624
+ const schema = {
625
+ title : 'a time in a string' ,
626
+ type : 'string' ,
627
+ format : 'time' ,
628
+ nullable : true
629
+ }
630
+ const toStringify = 'invalid'
631
+
632
+ const validate = validator ( schema )
633
+ const stringify = build ( schema )
634
+ const output = stringify ( toStringify )
635
+
636
+ t . equal ( output , JSON . stringify ( toStringify ) )
637
+ t . not ( validate ( JSON . parse ( output ) ) , 'valid schema' )
638
+ } )
0 commit comments