@@ -110,10 +110,25 @@ impl Schema {
110
110
}
111
111
112
112
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 ( ) {
114
116
return Err ( ValidateError :: NonRootDefinitions ) ;
115
117
}
116
118
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
+
117
132
match & self . form {
118
133
form:: Form :: Empty | form:: Form :: Type ( _) => { }
119
134
form:: Form :: Enum ( form:: Enum { values, .. } ) => {
@@ -122,7 +137,9 @@ impl Schema {
122
137
}
123
138
}
124
139
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) {
126
143
return Err ( ValidateError :: NoSuchDefinition ( definition. clone ( ) ) ) ;
127
144
}
128
145
}
0 commit comments