@@ -755,6 +755,123 @@ types:
755
755
} )
756
756
} )
757
757
758
+ describe ( 'raml unions validation' , function ( ) {
759
+ function makeUnionDt ( ) {
760
+ return new wp . model . domain . UnionShape ( )
761
+ . withName ( 'item' )
762
+ . withAnyOf ( [
763
+ new wp . model . domain . ScalarShape ( )
764
+ . withDataType ( 'http://www.w3.org/2001/XMLSchema#string' ) ,
765
+ new wp . model . domain . ScalarShape ( )
766
+ . withDataType ( 'http://a.ml/vocabularies/shapes#number' )
767
+ . withMaximum ( 100 )
768
+ ] )
769
+ }
770
+
771
+ it ( 'should let valid requests through' , function ( ) {
772
+ const app = ospreyRouter ( )
773
+ const method = makeRequestMethod ( 'application/json' , makeUnionDt ( ) )
774
+ app . post ( '/' , ospreyMethodHandler ( method ) , function ( req , res ) {
775
+ expect ( req . body ) . to . equal ( 'hello' )
776
+ res . end ( 'success' )
777
+ } )
778
+
779
+ return makeFetcher ( app ) . fetch ( '/' , {
780
+ method : 'POST' ,
781
+ headers : {
782
+ 'Content-Type' : 'application/json; charset=utf-8'
783
+ } ,
784
+ body : '"hello"'
785
+ } )
786
+ . then ( function ( res ) {
787
+ expect ( res . body ) . to . equal ( 'success' )
788
+ expect ( res . status ) . to . equal ( 200 )
789
+ } )
790
+ } )
791
+
792
+ it ( 'should reject invalid requests with an error response' , function ( ) {
793
+ const app = ospreyRouter ( )
794
+ const method = makeRequestMethod ( 'application/json' , makeUnionDt ( ) )
795
+ app . post ( '/' , ospreyMethodHandler ( method ) )
796
+
797
+ app . use ( function ( err , req , res , next ) {
798
+ expect ( err . ramlValidation ) . to . equal ( true )
799
+ err . requestErrors . sort ( )
800
+ expect ( err . requestErrors [ 0 ] ) . to . deep . equal ( {
801
+ type : 'json' ,
802
+ keyword : 'type' ,
803
+ dataPath : '' ,
804
+ message : 'should be string' ,
805
+ data : 101 ,
806
+ schema : 'string'
807
+ } )
808
+ expect ( err . requestErrors [ 1 ] ) . to . deep . equal ( {
809
+ type : 'json' ,
810
+ keyword : 'maximum' ,
811
+ dataPath : '' ,
812
+ message : 'should be <= 100' ,
813
+ data : 101 ,
814
+ schema : 100
815
+ } )
816
+ expect ( err . requestErrors [ 2 ] . message ) . to . equal (
817
+ 'should match some schema in anyOf' )
818
+ return next ( err )
819
+ } )
820
+
821
+ return makeFetcher ( app ) . fetch ( '/' , {
822
+ method : 'POST' ,
823
+ headers : {
824
+ 'Content-Type' : 'application/json; charset=utf-8'
825
+ } ,
826
+ body : '101'
827
+ } )
828
+ . then ( function ( res ) {
829
+ expect ( res . status ) . to . equal ( 400 )
830
+ } )
831
+ } )
832
+
833
+ it ( 'should validate agains both union types' , function ( ) {
834
+ const app = ospreyRouter ( )
835
+ const method = makeRequestMethod ( 'application/json' , makeUnionDt ( ) )
836
+ app . post ( '/' , ospreyMethodHandler ( method ) )
837
+
838
+ app . use ( function ( err , req , res , next ) {
839
+ expect ( err . ramlValidation ) . to . equal ( true )
840
+ err . requestErrors . sort ( )
841
+ expect ( err . requestErrors [ 0 ] ) . to . deep . equal ( {
842
+ type : 'json' ,
843
+ keyword : 'type' ,
844
+ dataPath : '' ,
845
+ message : 'should be string' ,
846
+ data : { zoo : 'bear' } ,
847
+ schema : 'string'
848
+ } )
849
+ expect ( err . requestErrors [ 1 ] ) . to . deep . equal ( {
850
+ type : 'json' ,
851
+ keyword : 'type' ,
852
+ dataPath : '' ,
853
+ message : 'should be number' ,
854
+ data : { zoo : 'bear' } ,
855
+ schema : 'number'
856
+ } )
857
+ expect ( err . requestErrors [ 2 ] . message ) . to . equal (
858
+ 'should match some schema in anyOf' )
859
+ return next ( err )
860
+ } )
861
+
862
+ return makeFetcher ( app ) . fetch ( '/' , {
863
+ method : 'POST' ,
864
+ headers : {
865
+ 'Content-Type' : 'application/json; charset=utf-8'
866
+ } ,
867
+ body : '{"zoo": "bear"}'
868
+ } )
869
+ . then ( function ( res ) {
870
+ expect ( res . status ) . to . equal ( 400 )
871
+ } )
872
+ } )
873
+ } )
874
+
758
875
describe ( 'raml datatype' , function ( ) {
759
876
function makeRamlDt ( ) {
760
877
return new wp . model . domain . NodeShape ( )
0 commit comments