@@ -488,8 +488,11 @@ class SimpleSchema {
488
488
schemaObj = expandShorthand ( schema ) ;
489
489
}
490
490
491
+ const schemaKeys = Object . keys ( schemaObj ) ;
492
+ const combinedKeys = new Set ( [ ...Object . keys ( this . _schema ) , ...schemaKeys ] ) ;
493
+
491
494
// Update all of the information cached on the instance
492
- Object . keys ( schemaObj ) . forEach ( ( fieldName ) => {
495
+ schemaKeys . forEach ( ( fieldName ) => {
493
496
const definition = standardizeDefinition ( schemaObj [ fieldName ] ) ;
494
497
495
498
// Merge/extend with any existing definition
@@ -511,7 +514,7 @@ class SimpleSchema {
511
514
this . _schema [ fieldName ] = definition ;
512
515
}
513
516
514
- checkAndScrubDefinition ( fieldName , this . _schema [ fieldName ] , this . _constructorOptions , schemaObj ) ;
517
+ checkAndScrubDefinition ( fieldName , this . _schema [ fieldName ] , this . _constructorOptions , combinedKeys ) ;
515
518
} ) ;
516
519
517
520
checkSchemaOverlap ( this . _schema ) ;
@@ -1005,8 +1008,16 @@ function standardizeDefinition(def) {
1005
1008
return standardizedDef ;
1006
1009
}
1007
1010
1008
- // Checks and mutates definition. Clone it first.
1009
- function checkAndScrubDefinition ( fieldName , definition , options , fullSchemaObj ) {
1011
+ /**
1012
+ * @summary Checks and mutates definition. Clone it first.
1013
+ * Throws errors if any problems are found.
1014
+ * @param {String } fieldName Name of field / key
1015
+ * @param {Object } definition Field definition
1016
+ * @param {Object } options Options
1017
+ * @param {Set } allKeys Set of all field names / keys in entire schema
1018
+ * @return {undefined } Void
1019
+ */
1020
+ function checkAndScrubDefinition ( fieldName , definition , options , allKeys ) {
1010
1021
if ( ! definition . type ) throw new Error ( `${ fieldName } key is missing "type"` ) ;
1011
1022
1012
1023
// Validate the field definition
@@ -1034,7 +1045,7 @@ function checkAndScrubDefinition(fieldName, definition, options, fullSchemaObj)
1034
1045
if ( SimpleSchema . isSimpleSchema ( type ) ) {
1035
1046
Object . keys ( type . _schema ) . forEach ( ( subKey ) => {
1036
1047
const newKey = `${ fieldName } .${ subKey } ` ;
1037
- if ( Object . prototype . hasOwnProperty . call ( fullSchemaObj , newKey ) ) {
1048
+ if ( allKeys . has ( newKey ) ) {
1038
1049
throw new Error ( `The type for "${ fieldName } " is set to a SimpleSchema instance that defines "${ newKey } ", but the parent SimpleSchema instance also tries to define "${ newKey } "` ) ;
1039
1050
}
1040
1051
} ) ;
@@ -1043,7 +1054,7 @@ function checkAndScrubDefinition(fieldName, definition, options, fullSchemaObj)
1043
1054
1044
1055
// If at least one of the possible types is Array, then make sure we have a
1045
1056
// definition for the array items, too.
1046
- if ( couldBeArray && ! Object . prototype . hasOwnProperty . call ( fullSchemaObj , `${ fieldName } .$` ) ) {
1057
+ if ( couldBeArray && ! allKeys . has ( `${ fieldName } .$` ) ) {
1047
1058
throw new Error ( `"${ fieldName } " is Array type but the schema does not include a "${ fieldName } .$" definition for the array items"` ) ;
1048
1059
}
1049
1060
0 commit comments