|
| 1 | +# IPA Validation |
| 2 | + |
| 3 | +The IPA validation uses [Spectral](https://docs.stoplight.io/docs/spectral/9ffa04e052cc1-spectral-cli) to validate the [MongoDB Atlas Admin API OpenAPI Specification](https://github.com/mongodb/openapi/tree/main/openapi). The rules cover MongoDB best-practices for API design. |
| 4 | + |
| 5 | +**Note:** For MongoDB engineers, please review https://go/ipa-validation-internal-wiki for additional information. |
| 6 | + |
| 7 | +## Running Locally |
| 8 | + |
| 9 | +### Prerequisites |
| 10 | + |
| 11 | +- Node.js >= v20 |
| 12 | +- npm |
| 13 | + |
| 14 | +### Run Validation |
| 15 | + |
| 16 | +To run the IPA validation locally, install necessary dependencies with `npm install` if you haven't already. Then, simply run: |
| 17 | + |
| 18 | +``` |
| 19 | +npm run ipa-validation |
| 20 | +``` |
| 21 | + |
| 22 | +This command will run Spectral CLI for the ruleset [ipa-spectral.yaml](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/ipa-spectral.yaml) on the [v2.yaml](https://github.com/mongodb/openapi/blob/main/openapi/v2.json) OpenAPI spec. |
| 23 | + |
| 24 | +The Spectral CLI can also be used to run the validation on any valid OpenAPI file (`json` or `yaml`). |
| 25 | + |
| 26 | +``` |
| 27 | +spectral lint {path/to/oas/file} --ruleset=./tools/spectral/ipa/ipa-spectral.yaml |
| 28 | +``` |
| 29 | + |
| 30 | +## Rule Implementation |
| 31 | + |
| 32 | +The rule validations are custom JS functions (see [/rulesets/functions](https://github.com/mongodb/openapi/tree/main/tools/spectral/ipa/rulesets/functions)). To learn more about custom functions, refer to the [Spectral Documentation](https://docs.stoplight.io/docs/spectral/a781e290eb9f9-custom-functions). |
| 33 | + |
| 34 | +The custom rule implementation allows for: |
| 35 | +- Advanced validations not available using the standard Spectral rules |
| 36 | +- Custom exception handling |
| 37 | +- Metrics collection |
| 38 | + |
| 39 | +### Exceptions |
| 40 | + |
| 41 | +Instead of using the [Spectral overrides approach](https://docs.stoplight.io/docs/spectral/293426e270fac-overrides), we use [custom OAS extensions](https://swagger.io/docs/specification/v3_0/openapi-extensions/) to handle exceptions to IPA validation rules. Exception extensions are added to the component which should be exempted, with the Spectral rule name and a reason. |
| 42 | + |
| 43 | +``` |
| 44 | +"x-xgen-IPA-exception": { |
| 45 | + "xgen-IPA-104-resource-has-GET": "Legacy API, not used by infrastructure-as-code tooling", |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Testing |
| 50 | + |
| 51 | +- IPA Validation related code is tested using [Jest](https://jestjs.io/) |
| 52 | +- Each custom validation function has tests, located in [/\__tests\__](https://github.com/mongodb/openapi/tree/main/tools/spectral/ipa/__tests__). They use the test hook [testRule.js](https://github.com/mongodb/openapi/blob/main/tools/spectral/ipa/__tests__/__helpers__/testRule.js) as a common approach for Spectral rule testing |
| 53 | +- Helper/util functions are tested as well, see [/\__tests\__/utils](https://github.com/mongodb/openapi/tree/main/tools/spectral/ipa/__tests__/utils) |
| 54 | + |
| 55 | +Install necessary dependencies with `npm install` if you haven't already. All Jest tests can be run with: |
| 56 | + |
| 57 | +``` |
| 58 | +npm run test |
| 59 | +``` |
| 60 | + |
| 61 | +To run a single test, in this case `singletonHasNoId.test.js`: |
| 62 | + |
| 63 | +``` |
| 64 | +npm run test -- singletonHasNoId |
| 65 | +``` |
0 commit comments