@@ -110,10 +110,25 @@ impl Schema {
110110 }
111111
112112 fn validate_with_root ( & self , root : Option < & Self > ) -> Result < ( ) , ValidateError > {
113- if root. is_none ( ) && !self . definitions . is_empty ( ) {
113+ // If root is non-None, then self is not the root schema. We should
114+ // therefore not tolerate definitions being placed on this schema.
115+ if root. is_some ( ) && !self . definitions . is_empty ( ) {
114116 return Err ( ValidateError :: NonRootDefinitions ) ;
115117 }
116118
119+ // This root variable is the one we will use for recursive calls to
120+ // validate_with_root.
121+ //
122+ // If we are at the top-level call of validate_with_root (invoked by the
123+ // public validate method) wherein root is None, then root will be
124+ // Some(self) for the recursive calls, since we ourselves are the root.
125+ let root = root. or ( Some ( self ) ) ;
126+
127+ // Validate each definition, if any.
128+ for sub_schema in self . definitions . values ( ) {
129+ sub_schema. validate_with_root ( root) ?;
130+ }
131+
117132 match & self . form {
118133 form:: Form :: Empty | form:: Form :: Type ( _) => { }
119134 form:: Form :: Enum ( form:: Enum { values, .. } ) => {
@@ -122,7 +137,9 @@ impl Schema {
122137 }
123138 }
124139 form:: Form :: Ref ( form:: Ref { definition, .. } ) => {
125- if !root. unwrap_or ( & self ) . definitions . contains_key ( definition) {
140+ // This unwrap is safe because the assignment to root above
141+ // guarantees root will be non-None.
142+ if !root. unwrap ( ) . definitions . contains_key ( definition) {
126143 return Err ( ValidateError :: NoSuchDefinition ( definition. clone ( ) ) ) ;
127144 }
128145 }
0 commit comments