|
1 | | -# Azure Functions Open API v3 specification validation |
| 1 | +# Node.js API tools |
2 | 2 |
|
3 | | -This library contains an Open API v3.0 validator and an Azure Functions v4 hook that validates http function requests. |
| 3 | +This mono repository contains libraries for implementing APIs. It contains the following sub-packages: |
4 | 4 |
|
5 | | -It identifies the Azure function route and tries to find a matching route in the Open API specification. It then validates query parameters, |
6 | | -the request body and response body against the schema. |
7 | | - |
8 | | -## Caveats |
9 | | - |
10 | | -* Early version: Bugs most likely exist. Bug fixes welcome and more test cases very welcome, too. :-) |
11 | | -* Only supports v3.0 of the Open API specifications (v3.1 has not been tested, contributions welcome) |
12 | | -* Only supports content of type `application/json` (and not `multipart/form-data` for example) |
13 | | -* Does not support external schemas (inline schemas are supported) |
14 | | -* Does not (yet?) validate headers |
15 | | -* Does not (really) validate path params, but supports them in the definition and request route |
16 | | -* Does not support references to properties (e.g. `$ref: '#/components/schemas/Test1/properties/bar/allOf/0/properties/baz'`) |
17 | | -* Does not support `readOnly` or `writeOnly`. |
18 | | -* This library does not validate the Open API specification itself. I suggest you use another tool for this for now. |
19 | | - |
20 | | -To check out what is supported, take a look at the [test fixtures](/test/fixtures/) |
21 | | - |
22 | | -## Getting started |
23 | | - |
24 | | -Because the Open API specification can come in different flavors and from different sources, loading the specification file is not in scope |
25 | | -of this library. To load a YAML based spec, you can for example use `js-yaml` as follows: |
26 | | - |
27 | | -```typescript |
28 | | -const openApiContent = fs.readFileSync('openapi.yaml', 'utf8') |
29 | | -const openApiSpec = yaml.load(openApiContent) |
30 | | -``` |
31 | | - |
32 | | -Once you've loaded the specification, use the `setupValidation` function to register the hook. |
33 | | - |
34 | | -```typescript |
35 | | -setupValidation(openApiSpec) |
36 | | -``` |
37 | | - |
38 | | -You can also take a look at [the example function app](example/src/functions/test.ts). |
39 | | - |
40 | | -## Configuration |
41 | | - |
42 | | -The `setupValidation` function takes in a number of configuration parameters that allow you to modify the behavior of this libary as well as |
43 | | -[AJV](https://www.npmjs.com/package/ajv), that is used to perform the schema validation. |
44 | | - |
45 | | -By default, the hook returns a 400 error response with details about the validation error for request parameter and request body validation. |
46 | | -Validation errors in the response body are only logged. |
47 | | - |
48 | | -Here's an example that disables query parameter validaton completely, logs out request body validation errors (but does not return an error |
49 | | -response) and returns a 500 error response for response body validation errors: |
50 | | - |
51 | | -```typescript |
52 | | -setupValidation(openApiSpec, { |
53 | | - hook: { |
54 | | - queryParameterValidationMode: false, |
55 | | - requestBodyValidationMode: { |
56 | | - returnErrorResponse: false, |
57 | | - logLevel: 'error', |
58 | | - strict: true, |
59 | | - }, |
60 | | - responseBodyValidationMode: { |
61 | | - returnErrorResponse: true, |
62 | | - logLevel: 'warn', |
63 | | - strict: true, |
64 | | - }, |
65 | | - } |
66 | | -}) |
67 | | -``` |
68 | | - |
69 | | -## Development Setup |
70 | | - |
71 | | -To run the example function app locally from VSCode, make sure to install the Azure Functions and Azurite extensions. |
72 | | -Then start Azurite via the `Azurite: Start` VSCode task. |
73 | | - |
74 | | -Build the library `npm run build` or use `npm run watch` to hotdeploy changes. (Warning: This didn't always work for me in practice.) |
75 | | - |
76 | | -Start the function app by running the VScode launch configuration `Debug Functions app`. |
77 | | - |
78 | | -Then send some requests to the API, for example: |
79 | | -`curl -X POST -H "Content-Type: application/json" -d '{"name":"hi"}' http://localhost:7071/api/users` |
80 | | - |
81 | | -## License and Attribution |
82 | | - |
83 | | -The scripts and documentation in this project are released under the [MIT License](LICENSE) |
84 | | - |
85 | | -Some of the validation test cases are based on the tests from [openapi-request-validator](`https://github.com/kogosoftwarellc/open-api/tree/main/packages/openapi-request-validator`) by Kogo Software LLC released under MIT. |
| 5 | +* [ajv-openapi-request-response-validator](./packages/ajv-openapi-request-response-validator/README.md): Validation of http request and responses against an OpenAPI schema. |
| 6 | +* [azure-functions-openapi-validator](./packages/azure-functions-openapi-validator/): Azure functions v4 middleware that uses the openapi validator |
| 7 | +* [azure-functions-openapi-validator-example](./packages/azure-functions-openapi-validator-example/README.md): Example Azure functions v4 app that uses the middleware |
0 commit comments