1010
1111const { walk } = require ( '../../../utils' ) ;
1212const MessageCarrier = require ( '../../../utils/messageCarrier' ) ;
13+ const at = require ( 'lodash/at' ) ;
1314
14- module . exports . validate = function ( { jsSpec } ) {
15+ const reduceObj = function ( jsSpec , obj ) {
16+ if ( obj [ '$ref' ] ) {
17+ const objPath = obj [ '$ref' ] . split ( '/' ) ;
18+ objPath . shift ( ) ;
19+ return reduceObj ( jsSpec , at ( jsSpec , [ objPath ] ) [ 0 ] ) ;
20+ }
21+ return obj ;
22+ } ;
23+
24+ const checkReqProp = function ( jsSpec , obj , requiredProp ) {
25+ obj = reduceObj ( jsSpec , obj ) ;
26+ if ( obj . properties && obj . properties [ requiredProp ] ) {
27+ return true ;
28+ } else if ( Array . isArray ( obj . anyOf ) || Array . isArray ( obj . oneOf ) ) {
29+ const childList = obj . anyOf || obj . oneOf ;
30+ let reqPropDefined = true ;
31+ childList . forEach ( ( childObj ) => {
32+ if ( ! checkReqProp ( jsSpec , childObj , requiredProp ) ) {
33+ reqPropDefined = false ;
34+ }
35+ } ) ;
36+ return reqPropDefined ;
37+ } else if ( Array . isArray ( obj . allOf ) ) {
38+ let reqPropDefined = false ;
39+ obj . allOf . forEach ( ( childObj ) => {
40+ if ( checkReqProp ( jsSpec , childObj , requiredProp ) ) {
41+ reqPropDefined = true ;
42+ }
43+ } ) ;
44+ return reqPropDefined ;
45+ }
46+ return false ;
47+ }
48+
49+ module . exports . validate = function ( { jsSpec } , config ) {
1550 const messages = new MessageCarrier ( ) ;
1651
1752 walk ( jsSpec , [ ] , function ( obj , path ) {
@@ -36,13 +71,14 @@ module.exports.validate = function({ jsSpec }) {
3671 }
3772
3873 // Assertation 2
74+ const undefinedRequiredProperties = config . schemas . undefined_required_properties ;
3975 if ( Array . isArray ( obj . required ) ) {
4076 obj . required . forEach ( ( requiredProp , i ) => {
41- if ( ! obj . properties || ! obj . properties [ requiredProp ] ) {
77+ if ( ! checkReqProp ( jsSpec , obj , requiredProp ) ) {
4278 messages . addMessage (
4379 path . concat ( [ `required[${ i } ]` ] ) . join ( '.' ) ,
44- "Schema properties specified as 'required' must be defined" ,
45- 'error'
80+ "Schema properties specified as 'required' should be defined" ,
81+ undefinedRequiredProperties
4682 ) ;
4783 }
4884 } ) ;
0 commit comments