Skip to content

Commit

Permalink
feat: warn when unsupported options are provided
Browse files Browse the repository at this point in the history
To help debug subtle errors, we print a console warning when
a constructor option or clean option isn't recognized.
Packages that enable support for additional options
can extend the list of supported option names.
  • Loading branch information
aldeed committed Nov 25, 2022
1 parent 55fea9a commit bb513b5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/SimpleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,37 @@ const propsThatCanBeFunction = [
class SimpleSchema {
public static debug?: boolean
public static defaultLabel?: string

/**
* Packages that want to allow and check additional options
* should add the option names to this set.
*/
public static supportedConstructorOptions = new Set([
'clean',
'getErrorMessage',
'humanizeAutoLabels',
'keepRawDefinition',
'requiredByDefault'
])

/**
* Packages that want to allow and check additional options
* should add the option names to this set.
*/
public static supportedCleanOptions = new Set([
'autoConvert',
'extendAutoValueContext',
'filter',
'getAutoValues',
'isModifier',
'isUpsert',
'mongoObject',
'mutate',
'removeEmptyStrings',
'removeNullsFromArrays',
'trimStrings'
])

public static validationErrorTransform?: (error: ClientError<ValidationError[]>) => Error
public static version = 2
public version: number
Expand Down Expand Up @@ -115,6 +146,12 @@ class SimpleSchema {
}
delete this._constructorOptions.clean // stored separately below

Object.getOwnPropertyNames(this._constructorOptions).forEach((opt) => {
if (!SimpleSchema.supportedConstructorOptions.has(opt)) {
console.warn(`Unsupported "${opt}" option passed to SimpleSchema constructor`)
}
})

// Schema-level defaults for cleaning
this._cleanOptions = {
...SimpleSchema._constructorOptionDefaults.clean,
Expand Down
6 changes: 6 additions & 0 deletions src/clean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ function clean (
...options
}

Object.getOwnPropertyNames(cleanOptions).forEach((opt) => {
if (!SimpleSchema.supportedCleanOptions.has(opt)) {
console.warn(`Unsupported "${opt}" option passed to SimpleSchema clean`)
}
})

// Clone so we do not mutate
const cleanDoc = cleanOptions.mutate === true ? doc : clone(doc)

Expand Down

0 comments on commit bb513b5

Please sign in to comment.