NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.43k stars 297 forks source link

Make optional options optional in typings and add type tests #622

Closed jdforsythe closed 7 months ago

jdforsythe commented 8 months ago

Purpose / Goal

See #612

This changes the type definitions to set all options with defaults to optional in the typings.

It adds documentation to all types so they show up in Intellisense when using the types.

It adds a type test - defining objects as the various options types (parse, validate, build) with none of the optional properties. Simply run npm test-types to attempt to compile the TS file and it will emit any type errors.

Example with no errors:

$ npm run test-types

> fast-xml-parser@4.3.2 test-types
> tsc --noEmit spec/typings/typings-test.ts

$ echo $?
0
## exit code 0 - no error

Example with an error - add someNewProperty to the type definition as non-optional:

fxp.d.ts:

type validationOptions = {
  /**
   * Whether to allow attributes without value
   * 
   * Defaults to `false`
   */
  allowBooleanAttributes?: boolean;

  /**
   * List of tags without closing tags
   * 
   * Defaults to `[]`
   */
  unpairedTags?: string[];

  // ADD A NEW PROPERTY - FORGET TO MAKE IT OPTIONAL
  /**
   * Some new property
   *
   * Defaults to `false`
   */
  someNewProperty: boolean;
};

The type test will now fail since the typings-test.ts file does not include this property in the validation options and it's listed as required:

$ npm run test-types

> fast-xml-parser@4.3.2 test-types
> tsc --noEmit spec/typings/typings-test.ts

spec/typings/typings-test.ts:41:7 - error TS2741: Property 'someNewProperty' is missing in type '{}' but required in type 'validationOptions'.

41 const validateOpts: validationOptions = {};
         ~~~~~~~~~~~~

  src/fxp.d.ts:234:2
    234   someNewProperty: boolean;
          ~~~~~~~~~~~~~~~~~~~~~~
    'someNewProperty' is declared here.

Found 1 error in spec/typings/typings-test.ts:41

$ echo $?
2
## exit code 2 - error

This will ensure the test fails if a property is added to the type as non-optional but not added to the typings-test.ts file as a required property.

Type

Please mention the type of PR

Note : Please ensure that you've read contribution guidelines before raising this PR. If your PR is in progress, please prepend [WIP] in PR title. Your PR will be reviewed when [WIP] will be removed from the PR title.

Bookmark this repository for further updates.

guardrails[bot] commented 8 months ago

:warning: We detected 8 security issues in this pull request:

Vulnerable Libraries (8)
Severity | Details :-: | :-- Medium | [pkg:npm/eslint@8.6.0](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 8.6.0* Medium | [pkg:npm/@babel/core@7.16.7](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 7.16.7* High | [pkg:npm/webpack@5.65.0](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *5.76.0* Medium | [pkg:npm/@babel/preset-env@7.16.7](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 7.16.7* High | [pkg:npm/babel-loader@8.2.3](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 8.2.3* High | [pkg:npm/cytorus@0.2.13](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 0.2.13* Medium | [pkg:npm/@babel/plugin-transform-runtime@7.16.7](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 7.16.7* Medium | [pkg:npm/@babel/register@7.16.7](https://github.com/NaturalIntelligence/fast-xml-parser/blob/79fb50aee1b6ddb1000e552fca39bf4e22921dc5/package.json) (t) upgrade to: *> 7.16.7* More info on how to fix Vulnerable Libraries in [JavaScript](https://docs.guardrails.io/docs/en/vulnerabilities/javascript/using_vulnerable_libraries.html?utm_source=ghpr).

👉 Go to the dashboard for detailed results.

📥 Happy? Share your feedback with us.

amitguptagwl commented 8 months ago

Hi @jdforsythe Yarn.lock or package-lock both files can't be considered in a PR due to security reasons so they need to be removed from the PR.

jdforsythe commented 7 months ago

@amitguptagwl this has been fixed

coveralls commented 7 months ago

Coverage Status

coverage: 98.248%. remained the same when pulling 451e89556af3ac2d01a19d60bac8c04f492043b3 on jdforsythe:type-tests into fa5a7339a5ae2ca4aea8a256179b82464dbf510e on NaturalIntelligence:master.

amitguptagwl commented 7 months ago

Thanks for this change. I'll confirm and publish soon.