@@ -289,6 +289,33 @@ class SimpleSchema {
289289 return keySchema
290290 }
291291
292+ /**
293+ * @param key One specific or generic key for which to get all possible schemas.
294+ * @returns An potentially empty array of possible definitions for one key
295+ *
296+ * Note that this returns the raw, unevaluated definition object. Use `getDefinition`
297+ * if you want the evaluated definition, where any properties that are functions
298+ * have been run to produce a result.
299+ */
300+ schemas ( key : string ) : StandardSchemaKeyDefinition [ ] {
301+ const schemas : StandardSchemaKeyDefinition [ ] = [ ]
302+
303+ const genericKey = MongoObject . makeKeyGeneric ( key )
304+ const keySchema = genericKey == null ? null : this . _schema [ genericKey ]
305+ if ( keySchema != null ) schemas . push ( keySchema )
306+
307+ // See if it's defined in any subschema
308+ this . forEachAncestorSimpleSchema (
309+ key ,
310+ ( simpleSchema , ancestor , subSchemaKey ) => {
311+ const keyDef = simpleSchema . schema ( subSchemaKey )
312+ if ( keyDef != null ) schemas . push ( keyDef )
313+ }
314+ )
315+
316+ return schemas
317+ }
318+
292319 /**
293320 * @returns {Object } The entire schema object with subschemas merged. This is the
294321 * equivalent of what schema() returned in SimpleSchema < 2.0
@@ -329,9 +356,45 @@ class SimpleSchema {
329356 propList ?: string [ ] | null ,
330357 functionContext : Record < string , unknown > = { }
331358 ) : StandardSchemaKeyDefinitionWithSimpleTypes | undefined {
332- const defs = this . schema ( key )
333- if ( defs == null ) return
359+ const schemaKeyDefinition = this . schema ( key )
360+ if ( schemaKeyDefinition == null ) return
361+ return this . resolveDefinitionForSchema ( key , schemaKeyDefinition , propList , functionContext )
362+ }
363+
364+ /**
365+ * Returns the evaluated definition for one key in the schema
366+ *
367+ * @param key Generic or specific schema key
368+ * @param [propList] Array of schema properties you need; performance optimization
369+ * @param [functionContext] The context to use when evaluating schema options that are functions
370+ * @returns The schema definition for the requested key
371+ */
372+ getDefinitions (
373+ key : string ,
374+ propList ?: string [ ] | null ,
375+ functionContext : Record < string , unknown > = { }
376+ ) : StandardSchemaKeyDefinitionWithSimpleTypes [ ] {
377+ const schemaKeyDefinitions = this . schemas ( key )
378+ return schemaKeyDefinitions . map ( ( def ) => {
379+ return this . resolveDefinitionForSchema ( key , def , propList , functionContext )
380+ } )
381+ }
334382
383+ /**
384+ * Resolves the definition for one key in the schema
385+ *
386+ * @param key Generic or specific schema key
387+ * @param schemaKeyDefinition Unresolved definition as returned from simpleSchema.schema()
388+ * @param [propList] Array of schema properties you need; performance optimization
389+ * @param [functionContext] The context to use when evaluating schema options that are functions
390+ * @returns The schema definition for the requested key
391+ */
392+ resolveDefinitionForSchema (
393+ key : string ,
394+ schemaKeyDefinition : StandardSchemaKeyDefinition ,
395+ propList ?: string [ ] | null ,
396+ functionContext : Record < string , unknown > = { }
397+ ) : StandardSchemaKeyDefinitionWithSimpleTypes {
335398 const getPropIterator = ( obj : Record < string , any > , newObj : Record < string , any > ) => {
336399 return ( prop : string ) : void => {
337400 if ( Array . isArray ( propList ) && ! propList . includes ( prop ) ) return
@@ -362,11 +425,11 @@ class SimpleSchema {
362425 type : [ ]
363426 }
364427
365- Object . keys ( defs ) . forEach ( getPropIterator ( defs , result ) )
428+ Object . keys ( schemaKeyDefinition ) . forEach ( getPropIterator ( schemaKeyDefinition , result ) )
366429
367430 // Resolve all the types and convert to a normal array to make it easier to use.
368- if ( Array . isArray ( defs . type ?. definitions ) ) {
369- result . type = defs . type . definitions . map ( ( typeDef ) => {
431+ if ( Array . isArray ( schemaKeyDefinition . type ?. definitions ) ) {
432+ result . type = schemaKeyDefinition . type . definitions . map ( ( typeDef ) => {
370433 const newTypeDef : SchemaKeyDefinitionWithOneType = {
371434 type : String // will be overwritten
372435 }
0 commit comments