@@ -17,11 +17,16 @@ namingConventions = {
17
17
//"Train-Case": "[a-z0-9\-]*
18
18
}
19
19
20
+ function ValidationError ( type , field ) {
21
+ this . type = type ;
22
+ this . field = field ;
23
+ }
24
+
20
25
//Promise.onPossiblyUnhandledRejection(function(error){
21
26
// throw error;
22
27
//});
23
28
24
- function getSpec ( file ) {
29
+ function getSpecPromise ( file ) {
25
30
return parser . parseAsync ( file )
26
31
. spread ( function ( api , metadata ) {
27
32
return api ;
@@ -31,7 +36,7 @@ function getSpec(file) {
31
36
} ) ;
32
37
}
33
38
34
- function getCheckStyle ( file ) {
39
+ function getCheckStylePromise ( file ) {
35
40
return fs . readFileAsync ( file )
36
41
. then ( yaml . safeLoad )
37
42
. catch ( function ( e ) {
@@ -40,22 +45,6 @@ function getCheckStyle(file) {
40
45
} ) ;
41
46
}
42
47
43
- function ValidationErrors ( ) {
44
- this . errors = [ ]
45
- this . add = add ;
46
-
47
- function add ( error ) {
48
- if ( error != null ) {
49
- this . errors . push ( error ) ;
50
- }
51
- }
52
- }
53
-
54
- function ValidationError ( type , field ) {
55
- this . type = type ;
56
- this . field = field ;
57
- }
58
-
59
48
function validatePath ( path , pathNamingConvention ) {
60
49
matchPath = path . replace ( pathNamingConvention , "" ) === path
61
50
if ( ! matchPath ) {
@@ -75,28 +64,28 @@ function validateOperation(opId, opNamingConvention) {
75
64
}
76
65
77
66
function validateConventions ( spec , pathNamingConvention , opNamingConvention ) {
78
- validationErrors = new ValidationErrors ( ) ;
67
+ var errors = new Array ( ) ;
79
68
80
69
result = mask ( spec , "paths/*/*/operationId" ) ;
81
70
paths = result . paths
82
71
_ . each ( Object . keys ( paths ) , function ( path ) {
83
72
84
73
pathError = validatePath ( path , pathNamingConvention ) ;
85
- validationErrors . add ( pathError ) ;
74
+ if ( pathError != null ) errors . push ( pathError ) ;
86
75
87
76
pathValue = result . paths [ path ]
88
77
_ . each ( Object . keys ( pathValue ) , function ( verb ) {
89
78
90
79
opId = pathValue [ verb ] . operationId ;
91
80
opError = validateOperation ( opId , opNamingConvention ) ;
92
- validationErrors . add ( opError ) ;
81
+ if ( opError != null ) errors . push ( opError ) ;
93
82
94
83
} ) ;
95
84
} ) ;
96
- return validationErrors ;
85
+ return errors ;
97
86
}
98
87
99
- function getSchema ( spec , checkStyle ) {
88
+ function getSchema ( checkStyle ) {
100
89
schema = Joi . object ( ) . keys ( {
101
90
swagger : Joi . any ( ) . valid ( checkStyle . swagger ) ,
102
91
host : joiRegex ( checkStyle . host ) ,
@@ -111,25 +100,32 @@ function getSchema(spec, checkStyle) {
111
100
return schema ;
112
101
}
113
102
103
+ function validate ( checkStyleFile , specFile ) {
104
+ specPromise = getSpecPromise ( specFile ) ;
105
+ stylePromise = getCheckStylePromise ( checkStyleFile ) ;
106
+
107
+ Promise . join ( specPromise , stylePromise , function ( spec , checkStyle ) {
108
+ pathConvention = namingConventions [ checkStyle . paths . namingConvention ] ;
109
+ opIdConvention = namingConventions [ checkStyle . paths . operationId . namingConvention ] ;
110
+ errors = validateConventions ( spec , pathConvention , opIdConvention ) ;
111
+ console . log ( errors ) ;
112
+
113
+ return [ spec ,
114
+ getSchema ( spec , checkStyle ) ,
115
+ { allowUnknown : true } ] ;
116
+ } ) . spread ( function ( spec , schema , options ) {
117
+ Joi . validateAsync ( spec , schema , options )
118
+ . then ( function ( result ) {
119
+ //console.log(result);
120
+ } ) . catch ( function ( err ) {
121
+ console . log ( err ) ;
122
+ } ) . error ( function ( err ) {
123
+ console . log ( err ) ;
124
+ } ) ;
125
+ } ) ;
126
+ }
114
127
115
- checkStyleFile = './examples/uber/swagger-checkstyle.yaml' ;
116
- specFile = './examples/uber/swagger.yaml' ;
117
-
118
- specPromise = getSpec ( specFile ) ;
119
- stylePromise = getCheckStyle ( checkStyleFile ) ;
120
-
121
- Promise . join ( specPromise , stylePromise , function ( spec , checkStyle ) {
122
- pathConvention = namingConventions [ checkStyle . paths . namingConvention ] ;
123
- opIdConvention = namingConventions [ checkStyle . paths . operationId . namingConvention ] ;
124
- errors = validateConventions ( spec , pathConvention , opIdConvention ) ;
125
- console . log ( errors ) ;
128
+ checkStyle = './examples/uber/swagger-checkstyle.yaml' ;
129
+ spec = './examples/uber/swagger.yaml' ;
126
130
127
- return [ spec ,
128
- getSchema ( spec , checkStyle ) ,
129
- { allowUnknown : true } ] ;
130
- } ) . spread ( function ( spec , schema , options ) {
131
- Joi . validateAsync ( spec , schema , options )
132
- . catch ( function ( err ) {
133
- console . log ( err . details ) ;
134
- } ) ;
135
- } ) ;
131
+ validate ( checkStyle , spec ) ;
0 commit comments