@@ -460,6 +460,39 @@ test.group('Literal node', () => {
460
460
const fn = compiler . compile ( )
461
461
assert . deepEqual ( await fn ( data , meta , refs , errorReporter ) , 'VIRK' )
462
462
} )
463
+
464
+ test ( 'convert empty string to null' , async ( { assert } ) => {
465
+ assert . plan ( 2 )
466
+
467
+ const compiler = new Compiler (
468
+ {
469
+ type : 'root' ,
470
+ schema : {
471
+ type : 'literal' ,
472
+ bail : true ,
473
+ fieldName : '*' ,
474
+ validations : [ ] ,
475
+ propertyName : '*' ,
476
+ allowNull : false ,
477
+ isOptional : false ,
478
+ } ,
479
+ } ,
480
+ { convertEmptyStringsToNull : true }
481
+ )
482
+
483
+ const data = ''
484
+ const meta = { }
485
+ const refs = { }
486
+ const errorReporter = new ErrorReporterFactory ( ) . create ( )
487
+
488
+ const fn = compiler . compile ( )
489
+ try {
490
+ await fn ( data , meta , refs , errorReporter )
491
+ } catch ( error ) {
492
+ assert . equal ( error . message , 'Validation failure' )
493
+ assert . deepEqual ( error . messages , [ 'value is required' ] )
494
+ }
495
+ } )
463
496
} )
464
497
465
498
test . group ( 'Literal node | optional: true' , ( ) => {
@@ -738,6 +771,33 @@ test.group('Literal node | optional: true', () => {
738
771
const output = await fn ( data , meta , refs , errorReporter )
739
772
assert . deepEqual ( output , undefined )
740
773
} )
774
+
775
+ test ( 'convert empty string to null' , async ( { assert } ) => {
776
+ const compiler = new Compiler (
777
+ {
778
+ type : 'root' ,
779
+ schema : {
780
+ type : 'literal' ,
781
+ bail : true ,
782
+ fieldName : '*' ,
783
+ validations : [ ] ,
784
+ propertyName : '*' ,
785
+ allowNull : false ,
786
+ isOptional : true ,
787
+ } ,
788
+ } ,
789
+ { convertEmptyStringsToNull : true }
790
+ )
791
+
792
+ const data = ''
793
+ const meta = { }
794
+ const refs = { }
795
+ const errorReporter = new ErrorReporterFactory ( ) . create ( )
796
+
797
+ const fn = compiler . compile ( )
798
+ const output = await fn ( data , meta , refs , errorReporter )
799
+ assert . isUndefined ( output )
800
+ } )
741
801
} )
742
802
743
803
test . group ( 'Literal node | allowNull: true' , ( ) => {
@@ -1023,4 +1083,31 @@ test.group('Literal node | allowNull: true', () => {
1023
1083
const output = await fn ( data , meta , refs , errorReporter )
1024
1084
assert . deepEqual ( output , null )
1025
1085
} )
1086
+
1087
+ test ( 'convert empty string to null' , async ( { assert } ) => {
1088
+ const compiler = new Compiler (
1089
+ {
1090
+ type : 'root' ,
1091
+ schema : {
1092
+ type : 'literal' ,
1093
+ bail : true ,
1094
+ fieldName : '*' ,
1095
+ validations : [ ] ,
1096
+ propertyName : '*' ,
1097
+ allowNull : true ,
1098
+ isOptional : false ,
1099
+ } ,
1100
+ } ,
1101
+ { convertEmptyStringsToNull : true }
1102
+ )
1103
+
1104
+ const data = ''
1105
+ const meta = { }
1106
+ const refs = { }
1107
+ const errorReporter = new ErrorReporterFactory ( ) . create ( )
1108
+
1109
+ const fn = compiler . compile ( )
1110
+ const output = await fn ( data , meta , refs , errorReporter )
1111
+ assert . isNull ( output )
1112
+ } )
1026
1113
} )
0 commit comments