Skip to content

Commit 4418be5

Browse files
restfulheadPatrick Ruhkopf
authored andcommitted
doc: add usage examples
1 parent 046d595 commit 4418be5

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
# Azure Functions Open API specification validation
22

3+
This library contains an Open API validator and an Azure Functions v4 hook that validates http function requests.
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+
(Note: This library does not validate the Open API specification itself. I suggest you use another tool for this for now.)
9+
10+
## Getting started
11+
12+
Because the Open API specification can come in different flavors and from different sources, loading the specification file is not in scope
13+
of this library. To load a YAML based spec, you can for example use `js-yaml` as follows:
14+
15+
```typescript
16+
const openApiContent = fs.readFileSync('openapi.yaml', 'utf8')
17+
const openApiSpec = yaml.load(openApiContent)
18+
```
19+
20+
Once you've loaded the specification, use the `setupValidation` function to register the hook.
21+
22+
```typescript
23+
setupValidation(openApiSpec)
24+
```
25+
26+
You can also take a look at [the example function app](example/src/functions/test.ts).
27+
28+
## Configuration
29+
30+
The `setupValidation` function takes in a number of configuration parameters that allow you to modify the behavior of this libary as well as
31+
[AJV](), that is used to perform the schema validation.
32+
33+
By default, the hook returns a 400 error response with details about the validation error for request parameter and request body validation.
34+
Validation errors in the response body are only logged.
35+
36+
Here's an example that disables query parameter validaton completely, logs out request body validation errors (but does not return an error
37+
response) and returns a 500 error response for response body validation errors:
38+
39+
```typescript
40+
setupValidation(openApiSpec, {
41+
hook: {
42+
queryParameterValidationMode: false,
43+
requestBodyValidationMode: {
44+
returnErrorResponse: false,
45+
logLevel: 'error',
46+
strict: true,
47+
},
48+
responseBodyValidationMode: {
49+
returnErrorResponse: true,
50+
logLevel: 'warn',
51+
strict: true,
52+
},
53+
}
54+
})
55+
```
56+
357
## Development Setup
458

559
To run the example function app locally from VSCode, make sure to install the Azure Functions and Azurite extensions.
@@ -8,6 +62,3 @@ Then start Azurite via the `Azurite: Start` VSCode task.
862
Build the library `npm run build` or use `npm run watch` to hotdeploy changes.
963

1064
Start the function app by running the VScode launch configuration "Attach to Node Functions".
11-
12-
13-
TODO: allow excluding paths

example/src/functions/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ export function getUser(request: HttpRequest, context: InvocationContext): Promi
3636
return Promise.resolve({ body: JSON.stringify({ name: 'jane doe', id: '456' }) })
3737
}
3838

39+
// TODO add many more test cases
40+
3941
app.post('post-users', {
4042
route: 'users',
4143
authLevel: 'anonymous',

0 commit comments

Comments
 (0)