@@ -146,6 +146,42 @@ class SimpleSchema {
146146 } ) ;
147147 }
148148
149+ /**
150+ * @param {String } key One specific or generic key for which to get the schema.
151+ * @returns {[SimpleSchema, String] } Returns a 2-tuple.
152+ *
153+ * First item: The SimpleSchema instance that actually defines the given key.
154+ *
155+ * For example, if you have several nested objects, each their own SimpleSchema
156+ * instance, and you pass in 'outerObj.innerObj.innerestObj.name' as the key, you'll
157+ * get back the SimpleSchema instance for `outerObj.innerObj.innerestObj` key.
158+ *
159+ * But if you pass in 'outerObj.innerObj.innerestObj.name' as the key and that key is
160+ * defined in the main schema without use of subschemas, then you'll get back the main schema.
161+ *
162+ * Second item: The part of the key that is in the found schema.
163+ *
164+ * Always returns a tuple (array) but the values may be `null`.
165+ */
166+ nearestSimpleSchemaInstance ( key ) {
167+ if ( ! key ) return [ null , null ] ;
168+
169+ const genericKey = MongoObject . makeKeyGeneric ( key ) ;
170+ if ( this . _schema [ genericKey ] ) return [ this , genericKey ] ;
171+
172+ // If not defined in this schema, see if it's defined in a subschema
173+ let innerKey ;
174+ let nearestSimpleSchemaInstance ;
175+ this . forEachAncestorSimpleSchema ( key , ( simpleSchema , ancestor , subSchemaKey ) => {
176+ if ( ! nearestSimpleSchemaInstance && simpleSchema . _schema [ subSchemaKey ] ) {
177+ nearestSimpleSchemaInstance = simpleSchema ;
178+ innerKey = subSchemaKey ;
179+ }
180+ } ) ;
181+
182+ return innerKey ? [ nearestSimpleSchemaInstance , innerKey ] : [ null , null ] ;
183+ }
184+
149185 /**
150186 * @param {String } [key] One specific or generic key for which to get the schema.
151187 * @returns {Object } The entire schema object or just the definition for one key.
@@ -702,10 +738,12 @@ class SimpleSchema {
702738 Object . keys ( labels ) . forEach ( ( key ) => {
703739 const label = labels [ key ] ;
704740 if ( typeof label !== 'string' && typeof label !== 'function' ) return ;
705- if ( ! Object . prototype . hasOwnProperty . call ( this . _schema , key ) ) return ;
706741
707- this . _schema [ key ] . label = label ;
708- this . _depsLabels [ key ] && this . _depsLabels [ key ] . changed ( ) ;
742+ const [ schemaInstance , innerKey ] = this . nearestSimpleSchemaInstance ( key ) ;
743+ if ( ! schemaInstance ) return ;
744+
745+ schemaInstance . _schema [ innerKey ] . label = label ;
746+ schemaInstance . _depsLabels [ innerKey ] && schemaInstance . _depsLabels [ innerKey ] . changed ( ) ;
709747 } ) ;
710748 }
711749
0 commit comments