xmlbuilder2 icon indicating copy to clipboard operation
xmlbuilder2 copied to clipboard

Passing an incorrect builder option in `create` breaks things

Open vjpr opened this issue 4 years ago • 0 comments

The function param overloading is confusing, and broken.

If you make a typo in the argument to builderOptions, then it will use defaults.

Validation should be added and overloading should be removed.

export function convert(p1: XMLBuilderCreateOptions | string | ExpandObject,
  p2?: string | ExpandObject | WriterOptions, p3?: WriterOptions): XMLSerializedValue {

  let builderOptions: XMLBuilderCreateOptions
  let contents: string | ExpandObject
  let convertOptions: WriterOptions | undefined
  if (isXMLBuilderCreateOptions(p1) && p2 !== undefined) {
    builderOptions = p1
    contents = p2
    convertOptions = p3
  } else {
    builderOptions = DefaultBuilderOptions
    contents = p1
    convertOptions = p2 as WriterOptions || undefined
  }

  return create(builderOptions, contents).end(convertOptions as any)
}
function isXMLBuilderCreateOptions(obj: any): obj is XMLBuilderCreateOptions {
  if (!isPlainObject(obj)) return false

  for (const key in obj) {
    /* istanbul ignore else */
    if (obj.hasOwnProperty(key)) {
      if (!XMLBuilderOptionKeys.has(key)) return false
    }
  }

  return true
}

vjpr avatar Sep 08 '21 17:09 vjpr