@@ -77,6 +77,111 @@ describe('HttpValidator', () => {
7777 } ) ;
7878 } ) ;
7979
80+ describe ( 'falsey primitive body values' , ( ) => {
81+ afterAll ( ( ) => {
82+ // Re-setup the mocks that were defined in the outer beforeAll
83+ jest . spyOn ( validators , 'validateQuery' ) . mockReturnValue ( E . left ( [ mockError ] ) ) ;
84+ jest . spyOn ( validators , 'validateBody' ) . mockReturnValue ( E . left ( [ mockError ] ) ) ;
85+ jest . spyOn ( validators , 'validateHeaders' ) . mockReturnValue ( E . left ( [ mockError ] ) ) ;
86+ jest . spyOn ( validators , 'validatePath' ) . mockReturnValue ( E . left ( [ mockError ] ) ) ;
87+ } ) ;
88+
89+ beforeEach ( ( ) => jest . restoreAllMocks ( ) ) ;
90+
91+ it ( 'accepts 0 as a valid integer body' , ( ) => {
92+ const result = validator . validateInput ( {
93+ resource : {
94+ method : 'post' ,
95+ path : '/' ,
96+ id : '1' ,
97+ request : {
98+ body : {
99+ id : faker . random . word ( ) ,
100+ required : true ,
101+ contents : [
102+ {
103+ id : faker . random . word ( ) ,
104+ mediaType : 'application/json' ,
105+ schema : { type : 'integer' } ,
106+ } ,
107+ ] ,
108+ } ,
109+ } ,
110+ responses : [ { id : faker . random . word ( ) , code : '200' } ] ,
111+ } ,
112+ element : {
113+ method : 'post' ,
114+ url : { path : '/' , query : { } } ,
115+ body : 0 ,
116+ headers : { 'content-type' : 'application/json' , 'content-length' : '1' } ,
117+ } ,
118+ } ) ;
119+ assertRight ( result ) ;
120+ } ) ;
121+
122+ it ( 'accepts false as a valid boolean body' , ( ) => {
123+ const result = validator . validateInput ( {
124+ resource : {
125+ method : 'post' ,
126+ path : '/' ,
127+ id : '1' ,
128+ request : {
129+ body : {
130+ id : faker . random . word ( ) ,
131+ required : true ,
132+ contents : [
133+ {
134+ id : faker . random . word ( ) ,
135+ mediaType : 'application/json' ,
136+ schema : { type : 'boolean' } ,
137+ } ,
138+ ] ,
139+ } ,
140+ } ,
141+ responses : [ { id : faker . random . word ( ) , code : '200' } ] ,
142+ } ,
143+ element : {
144+ method : 'post' ,
145+ url : { path : '/' , query : { } } ,
146+ body : false ,
147+ headers : { 'content-type' : 'application/json' , 'content-length' : '5' } ,
148+ } ,
149+ } ) ;
150+ assertRight ( result ) ;
151+ } ) ;
152+
153+ it ( 'accepts empty string as a valid string body' , ( ) => {
154+ const result = validator . validateInput ( {
155+ resource : {
156+ method : 'post' ,
157+ path : '/' ,
158+ id : '1' ,
159+ request : {
160+ body : {
161+ id : faker . random . word ( ) ,
162+ required : true ,
163+ contents : [
164+ {
165+ id : faker . random . word ( ) ,
166+ mediaType : 'application/json' ,
167+ schema : { type : 'string' } ,
168+ } ,
169+ ] ,
170+ } ,
171+ } ,
172+ responses : [ { id : faker . random . word ( ) , code : '200' } ] ,
173+ } ,
174+ element : {
175+ method : 'post' ,
176+ url : { path : '/' , query : { } } ,
177+ body : '' ,
178+ headers : { 'content-type' : 'application/json' , 'content-length' : '2' } ,
179+ } ,
180+ } ) ;
181+ assertRight ( result ) ;
182+ } ) ;
183+ } ) ;
184+
80185 describe ( 'headers validation in enabled' , ( ) => {
81186 describe ( 'request is not set' , ( ) => {
82187 it ( 'does not validate headers' , validate ( undefined , undefined , 0 ) ) ;
0 commit comments