Skip to content

Commit d27f5e3

Browse files
committed
tests: more test cases
1 parent e1e8c1a commit d27f5e3

4 files changed

+60
-8
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
# Azure Functions Open API v3 specification validation
22

3-
This library contains an Open API v3 validator and an Azure Functions v4 hook that validates http function requests.
3+
This library contains an Open API v3.0 validator and an Azure Functions v4 hook that validates http function requests.
44

55
It identifies the Azure function route and tries to find a matching route in the Open API specification. It then validates query parameters,
66
the request body and response body against the schema.
77

88
## Caveats
99

10-
* Early version: Bugs most likely exist. Bug fixes welcome and more test cases very welcom. :-)
11-
* So far only tested with Open API specifications in v3
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)
1214
* Does not (yet?) validate headers
13-
* Does not (really) validate path params, but supports them
15+
* Does not (really) validate path params, but supports them in the definition and request route
1416
* This library does not validate the Open API specification itself. I suggest you use another tool for this for now.
1517

1618
## Getting started
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module.exports = {
2+
validateArgs: {
3+
requestBody: {
4+
description: 'a test body',
5+
content: {
6+
'application/json; charset=utf-8': {
7+
schema: {
8+
properties: {
9+
test1: {
10+
$ref: '#/components/schemas/Test1',
11+
},
12+
test2: {
13+
$ref: '#/components/schemas/Test2',
14+
},
15+
},
16+
},
17+
},
18+
},
19+
},
20+
schemas:
21+
{
22+
Test1: {
23+
type: 'object',
24+
properties: {
25+
foo: {
26+
type: 'string',
27+
},
28+
},
29+
},
30+
Test2: {
31+
type: 'object',
32+
properties: {
33+
boo: {
34+
type: 'string',
35+
},
36+
},
37+
},
38+
},
39+
},
40+
request: {
41+
body: {}
42+
},
43+
};

test/unit/ajv-validator-api.spec.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,22 @@ interface TestFixture {
5151
}
5252

5353
// read all *.ts files from the fixtures directory
54+
const onlyInclude: string[] = ['accept-empty-body-when-body-is-not-required-and-body-schema-has-no-required-properties.js.txt'] // can be used to only run specific tests. for local testing only!
5455
const fixtureDir = `${__dirname}/../fixtures`
5556
const files = fs.readdirSync(fixtureDir)
5657
const testCases: { [key: string]: TestFixture } = {}
5758
for (const file of files) {
5859
if (file.endsWith('.js.txt')) {
5960
const testName = path.basename(file, '.js.txt').replace(/-/g, ' ')
6061
const fixtureContent = fs.readFileSync(path.resolve(fixtureDir, file), { encoding: 'utf-8' })
61-
const fixture: TestFixture = eval(fixtureContent)
62-
testCases[testName] = fixture
62+
try {
63+
const fixture: TestFixture = eval(fixtureContent)
64+
if (!onlyInclude || onlyInclude.length === 0 || onlyInclude.includes(file)) {
65+
testCases[testName] = fixture
66+
}
67+
} catch (e: any) {
68+
throw new Error(`Error parsing fixture ${file}: ${e.message}`)
69+
}
6370
}
6471
}
6572

@@ -74,7 +81,7 @@ describe('The api validator', () => {
7481
} else {
7582
if (fixture.validateArgs.requestBody || fixture.validateArgs.parameters) {
7683
const operations: { [method in OpenAPIV3.HttpMethods]?: OpenAPIV3.OperationObject<any> } = {}
77-
spec.paths[`/${fixture.request.route}` ?? '/test'] = operations
84+
spec.paths[`/${fixture.request.route ?? 'test'}`] = operations
7885
operations[(fixture.request.method as OpenAPIV3.HttpMethods) ?? 'post'] = {
7986
requestBody: fixture.validateArgs.requestBody,
8087
responses: {},
@@ -86,7 +93,7 @@ describe('The api validator', () => {
8693

8794
if (fixture.validateArgs.requestBody && fixture.request.body) {
8895
const result = validator.validateRequestBody(
89-
`/${fixture.request.route}` ?? '/test',
96+
`/${fixture.request.route ?? 'test'}`,
9097
fixture.request.method ?? 'post',
9198
fixture.request.body
9299
)

0 commit comments

Comments
 (0)