@@ -4,11 +4,47 @@ const { test } = require('tap')
44const  Fastify  =  require ( 'fastify' ) 
55const  enforceSchema  =  require ( '../index.js' ) 
66const  { 
7-  getErrrorMessage , 
7+  getErrorMessage , 
88 hasProperties, 
99 isSchemaTypeExcluded
1010}  =  require ( '../utils.js' ) 
1111
12+ test ( 'Should fail if no options passed: every endpoint should have a schema' ,  async  ( t )  =>  { 
13+  t . plan ( 1 ) 
14+ 
15+  const  fastify  =  Fastify ( ) 
16+ 
17+  await  fastify . register ( enforceSchema ) 
18+ 
19+  try  { 
20+  fastify . post ( '/foo' ,  { } ,  ( req ,  reply )  =>  { 
21+  reply . code ( 201 ) . send ( 'ok' ) 
22+  } ) 
23+  }  catch  ( error )  { 
24+  t . equal ( error . message ,  'schema missing at the path POST: "/foo"' ) 
25+  } 
26+ } ) 
27+ 
28+ test ( 'Should pass if no options passed and schema validation explicitly disabled' ,  async  ( t )  =>  { 
29+  t . plan ( 2 ) 
30+ 
31+  const  fastify  =  Fastify ( ) 
32+ 
33+  await  fastify . register ( enforceSchema ) 
34+  
35+  fastify . post ( '/foo' ,  {  schema : false  } ,  ( req ,  reply )  =>  { 
36+  reply . code ( 201 ) . send ( 'ok' ) 
37+  } ) 
38+ 
39+  const  res  =  await  fastify . inject ( { 
40+  method : 'POST' , 
41+  url : '/foo' 
42+  } ) 
43+ 
44+  t . equal ( res . statusCode ,  201 ) 
45+  t . equal ( res . payload ,  'ok' ) 
46+ } ) 
47+ 
1248test ( 'Should fail if schema is missing' ,  async  ( t )  =>  { 
1349 t . plan ( 1 ) 
1450
@@ -132,7 +168,6 @@ test('Should NOT fail if params schema is missing', async (t) => {
132168
133169 await  fastify . register ( enforceSchema ,  {  required : [ 'params' ]  } ) 
134170
135-  
136171 fastify . get ( '/foo' ,  {  schema : { }  } ,  ( req ,  reply )  =>  { 
137172 reply . code ( 200 ) . send ( 'ok' ) 
138173 } ) 
@@ -166,15 +201,15 @@ test('getErrorMessage should return a proper message', async (t) => {
166201 t . plan ( 3 ) 
167202
168203 t . equal ( 
169-  getErrrorMessage ( 'body' ,  {  path : '/bar' ,  method : 'PUT'  } ) , 
204+  getErrorMessage ( 'body' ,  {  path : '/bar' ,  method : 'PUT'  } ) , 
170205 'PUT: /bar is missing a body schema' 
171206 ) 
172207 t . equal ( 
173-  getErrrorMessage ( 'response' ,  {  path : '/bar' ,  method : 'PUT'  } ) , 
208+  getErrorMessage ( 'response' ,  {  path : '/bar' ,  method : 'PUT'  } ) , 
174209 'PUT: /bar is missing a response schema' 
175210 ) 
176211 t . equal ( 
177-  getErrrorMessage ( 'params' ,  {  path : '/bar' ,  method : 'PUT'  } ) , 
212+  getErrorMessage ( 'params' ,  {  path : '/bar' ,  method : 'PUT'  } ) , 
178213 'PUT: /bar is missing a params schema' 
179214 ) 
180215} ) 
0 commit comments