@@ -315,6 +315,109 @@ test('Encapsulation isolation for getSchemas', t => {
315
315
} )
316
316
} )
317
317
318
+ test ( 'Encapsulation isolation for $ref to shared schema' , t => {
319
+ t . plan ( 10 )
320
+ const fastify = Fastify ( )
321
+
322
+ const commonSchemaAbsoluteUri = {
323
+ $id : 'http://example.com/asset.json' ,
324
+ type : 'object' ,
325
+ definitions : {
326
+ id : {
327
+ $id : '#uuid' ,
328
+ type : 'string' ,
329
+ format : 'uuid'
330
+ } ,
331
+ email : {
332
+ $id : '#email' ,
333
+ type : 'string' ,
334
+ format : 'email'
335
+ }
336
+ }
337
+ }
338
+
339
+ fastify . register ( ( instance , opts , next ) => {
340
+ instance . addSchema ( commonSchemaAbsoluteUri )
341
+ instance . route ( {
342
+ method : 'POST' ,
343
+ url : '/id' ,
344
+ schema : {
345
+ body : {
346
+ type : 'object' ,
347
+ properties : { id : { $ref : 'http://example.com/asset.json#uuid' } } ,
348
+ required : [ 'id' ]
349
+ }
350
+ } ,
351
+ handler : ( req , reply ) => { reply . send ( 'id is ok' ) }
352
+ } )
353
+ next ( )
354
+ } )
355
+
356
+ fastify . register ( ( instance , opts , next ) => {
357
+ instance . addSchema ( commonSchemaAbsoluteUri )
358
+ instance . route ( {
359
+ method : 'POST' ,
360
+ url : '/email' ,
361
+ schema : {
362
+ body : {
363
+ type : 'object' ,
364
+ properties : { email : { $ref : 'http://example.com/asset.json#/definitions/email' } } ,
365
+ required : [ 'email' ]
366
+ }
367
+ } ,
368
+ handler : ( req , reply ) => { reply . send ( 'email is ok' ) }
369
+ } )
370
+ next ( )
371
+ } )
372
+
373
+ const requestId = { id : '550e8400-e29b-41d4-a716-446655440000' }
374
+ const requestEmail = { email : 'foo@bar.it' }
375
+
376
+ fastify . inject ( {
377
+ method : 'POST' ,
378
+ url : '/id' ,
379
+ payload : requestId
380
+ } , ( err , res ) => {
381
+ t . error ( err )
382
+ t . strictEqual ( res . statusCode , 200 )
383
+ } )
384
+ fastify . inject ( {
385
+ method : 'POST' ,
386
+ url : '/id' ,
387
+ payload : requestEmail
388
+ } , ( err , res ) => {
389
+ t . error ( err )
390
+ t . strictEqual ( res . statusCode , 400 )
391
+ t . deepEqual ( JSON . parse ( res . payload ) , {
392
+ error : 'Bad Request' ,
393
+ message : 'body should have required property \'id\'' ,
394
+ statusCode : 400
395
+ } )
396
+ } )
397
+
398
+ fastify . inject ( {
399
+ method : 'POST' ,
400
+ url : '/email' ,
401
+ payload : requestEmail
402
+ } , ( err , res ) => {
403
+ t . error ( err )
404
+ t . strictEqual ( res . statusCode , 200 )
405
+ } )
406
+ fastify . inject ( {
407
+ method : 'POST' ,
408
+ url : '/email' ,
409
+ payload : requestId
410
+ } , ( err , res ) => {
411
+ t . error ( err )
412
+ t . strictEqual ( res . statusCode , 400 )
413
+ t . deepEqual ( JSON . parse ( res . payload ) , {
414
+ error : 'Bad Request' ,
415
+ message : 'body should have required property \'email\'' ,
416
+ statusCode : 400
417
+ } )
418
+ } )
419
+ } )
420
+
318
421
test ( 'JSON Schema validation keywords' , t => {
319
422
t . plan ( 2 )
320
423
const fastify = Fastify ( )
@@ -742,11 +845,11 @@ test('Get schema anyway should not add `properties` if anyOf is present', t => {
742
845
fastify . ready ( t . error )
743
846
} )
744
847
745
- test ( 'Shared schema should be pass to serializer ($ref to shared schema /definitions)' , t => {
848
+ test ( 'Shared schema should be pass to serializer and validator ($ref to shared schema /definitions)' , t => {
746
849
t . plan ( 2 )
747
850
const fastify = Fastify ( )
748
851
749
- const schemaAsset = {
852
+ fastify . addSchema ( {
750
853
$id : 'http://example.com/asset.json' ,
751
854
$schema : 'http://json-schema.org/draft-07/schema#' ,
752
855
title : 'Physical Asset' ,
@@ -774,9 +877,9 @@ test('Shared schema should be pass to serializer ($ref to shared schema /definit
774
877
format : 'email'
775
878
}
776
879
}
777
- }
880
+ } )
778
881
779
- const schemaPoint = {
882
+ fastify . addSchema ( {
780
883
$id : 'http://example.com/point.json' ,
781
884
$schema : 'http://json-schema.org/draft-07/schema#' ,
782
885
title : 'Longitude and Latitude Values' ,
@@ -802,9 +905,9 @@ test('Shared schema should be pass to serializer ($ref to shared schema /definit
802
905
type : 'number'
803
906
}
804
907
}
805
- }
908
+ } )
806
909
807
- const schemaResponse = {
910
+ const schemaLocations = {
808
911
$id : 'http://example.com/locations.json' ,
809
912
$schema : 'http://json-schema.org/draft-07/schema#' ,
810
913
title : 'List of Asset locations' ,
@@ -813,36 +916,35 @@ test('Shared schema should be pass to serializer ($ref to shared schema /definit
813
916
default : [ ]
814
917
}
815
918
816
- fastify . addSchema ( schemaAsset )
817
- fastify . addSchema ( schemaPoint )
818
-
819
- const response = [
820
- { id : 'id1' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } } ,
821
- { id : 'id2' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } }
919
+ const locations = [
920
+ { id : '550e8400-e29b-41d4-a716-446655440000' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } } ,
921
+ { id : '550e8400-e29b-41d4-a716-446655440000' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } }
822
922
]
823
- fastify . get ( '/' , {
923
+ fastify . post ( '/' , {
824
924
schema : {
825
- response : { 200 : schemaResponse }
925
+ body : schemaLocations ,
926
+ response : { 200 : schemaLocations }
826
927
}
827
928
} , ( req , reply ) => {
828
- reply . send ( response . map ( i => Object . assign ( { serializer : 'remove me' } , i ) ) )
929
+ reply . send ( locations . map ( i => Object . assign ( { serializer : 'remove me' } , i ) ) )
829
930
} )
830
931
831
932
fastify . inject ( {
832
- method : 'GET' ,
833
- url : '/'
933
+ method : 'POST' ,
934
+ url : '/' ,
935
+ payload : locations
834
936
} , ( err , res ) => {
835
937
t . error ( err )
836
- response . forEach ( _ => delete _ . remove )
837
- t . deepEqual ( JSON . parse ( res . payload ) , response )
938
+ locations . forEach ( _ => delete _ . remove )
939
+ t . deepEqual ( JSON . parse ( res . payload ) , locations )
838
940
} )
839
941
} )
840
942
841
- test ( 'Shared schema should be pass to serializer ($ref to shared schema $id)' , t => {
943
+ test ( 'Shared schema should be pass to serializer and validator ($ref to shared schema $id)' , t => {
842
944
t . plan ( 2 )
843
945
const fastify = Fastify ( )
844
946
845
- const schemaAsset = {
947
+ fastify . addSchema ( {
846
948
$id : 'http://example.com/asset.json' ,
847
949
$schema : 'http://json-schema.org/draft-07/schema#' ,
848
950
title : 'Physical Asset' ,
@@ -870,9 +972,9 @@ test('Shared schema should be pass to serializer ($ref to shared schema $id)', t
870
972
format : 'email'
871
973
}
872
974
}
873
- }
975
+ } )
874
976
875
- const schemaPoint = {
977
+ fastify . addSchema ( {
876
978
$id : 'http://example.com/point.json' ,
877
979
$schema : 'http://json-schema.org/draft-07/schema#' ,
878
980
title : 'Longitude and Latitude Values' ,
@@ -898,9 +1000,9 @@ test('Shared schema should be pass to serializer ($ref to shared schema $id)', t
898
1000
type : 'number'
899
1001
}
900
1002
}
901
- }
1003
+ } )
902
1004
903
- const schemaResponse = {
1005
+ const schemaLocations = {
904
1006
$id : 'http://example.com/locations.json' ,
905
1007
$schema : 'http://json-schema.org/draft-07/schema#' ,
906
1008
title : 'List of Asset locations' ,
@@ -909,29 +1011,57 @@ test('Shared schema should be pass to serializer ($ref to shared schema $id)', t
909
1011
default : [ ]
910
1012
}
911
1013
912
- fastify . addSchema ( schemaAsset )
913
- fastify . addSchema ( schemaPoint )
914
-
915
- const response = [
916
- { id : 'id1' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } } ,
917
- { id : 'id2' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } }
1014
+ const locations = [
1015
+ { id : '550e8400-e29b-41d4-a716-446655440000' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } } ,
1016
+ { id : '550e8400-e29b-41d4-a716-446655440000' , model : 'mod' , location : { latitude : 10 , longitude : 10 , email : 'foo@bar.it' } }
918
1017
]
919
- fastify . get ( '/' , {
1018
+
1019
+ fastify . post ( '/' , {
920
1020
schema : {
921
- response : { 200 : schemaResponse }
1021
+ body : schemaLocations ,
1022
+ response : { 200 : schemaLocations }
922
1023
}
923
1024
} , ( req , reply ) => {
924
- reply . send ( response . map ( i => Object . assign ( { serializer : 'remove me' } , i ) ) )
1025
+ reply . send ( locations . map ( i => Object . assign ( { serializer : 'remove me' } , i ) ) )
925
1026
} )
926
1027
927
1028
fastify . inject ( {
928
- method : 'GET' ,
929
- url : '/'
1029
+ method : 'POST' ,
1030
+ url : '/' ,
1031
+ payload : locations
930
1032
} , ( err , res ) => {
931
1033
t . error ( err )
932
- response . forEach ( _ => delete _ . remove )
933
- t . deepEqual ( JSON . parse ( res . payload ) , response )
1034
+ locations . forEach ( _ => delete _ . remove )
1035
+ t . deepEqual ( JSON . parse ( res . payload ) , locations )
1036
+ } )
1037
+ } )
1038
+
1039
+ test ( 'Use shared schema and $ref' , t => {
1040
+ t . plan ( 1 )
1041
+ const fastify = Fastify ( )
1042
+
1043
+ fastify . addSchema ( {
1044
+ $id : 'http://example.com/ref-to-external-validator.json' ,
1045
+ type : 'object' ,
1046
+ properties : {
1047
+ hello : { type : 'string' }
1048
+ }
934
1049
} )
1050
+
1051
+ const body = {
1052
+ type : 'array' ,
1053
+ items : { $ref : 'http://example.com/ref-to-external-validator.json#' } ,
1054
+ default : [ ]
1055
+ }
1056
+
1057
+ fastify . route ( {
1058
+ method : 'POST' ,
1059
+ url : '/' ,
1060
+ schema : { body } ,
1061
+ handler : ( _ , r ) => { r . send ( 'ok' ) }
1062
+ } )
1063
+
1064
+ fastify . ready ( t . error )
935
1065
} )
936
1066
937
1067
test ( 'Use shared schema and $ref to /definitions' , t => {
0 commit comments