Skip to content

Commit 14405ae

Browse files
committed
fix: request coercer
1 parent 79b9563 commit 14405ae

File tree

13 files changed

+845
-257
lines changed

13 files changed

+845
-257
lines changed

README.md

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,7 @@
1-
# Azure Functions Open API v3 specification validation
1+
# Node.js API tools
22

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:
44

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

packages/ajv-openapi-request-response-validator/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Azure Functions Open API v3 specification validation
1+
# AJV Open API v3 specification validation
22

33
This library contains an Open API v3.0 validator that validates http requests and responses.
44

@@ -25,20 +25,25 @@ Because the Open API specification can come in different flavors and from differ
2525
of this library. To load a YAML based spec, you can for example use `js-yaml` as follows:
2626

2727
```typescript
28+
import * as fs from 'fs'
29+
import * as yaml from 'js-yaml'
30+
2831
const openApiContent = fs.readFileSync('openapi.yaml', 'utf8')
2932
const openApiSpec = yaml.load(openApiContent)
3033
```
3134

3235
Once you've loaded the specification, create an instance of AJV (for example by using the factory) and the validator.
3336

3437
```typescript
38+
import { AjvOpenApiValidator, createAjvInstance } from '@restfulhead/ajv-openapi-request-response-validator'
39+
3540
const ajv = createAjvInstance()
3641
const validator = new AjvOpenApiValidator(spec, ajv)
3742
```
3843

39-
You can then use the different validation functions such as `validateQueryParams`, `validateRequestBody` and `validateResponseBody`
44+
You can then use the different validation functions such as `validateQueryParams`, `validateRequestBody` and `validateResponseBody`.
4045

41-
For examples, see refer to the unit tests.
46+
For examples, refer to the unit tests.
4247

4348
## License and Attribution
4449

0 commit comments

Comments
 (0)