Skip to content

Commit 814e92a

Browse files
committed
test: more test cases
1 parent d27f5e3 commit 814e92a

10 files changed

+235
-3
lines changed

src/ajv-opts.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ export const AJV_FORMATS = {
4747
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4848
return !isNaN(Date.parse(dateTimeString as any)) // any test that returns true/false
4949
},
50+
uuid: (uuid: string) => {
51+
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i
52+
return uuidRegex.test(uuid)
53+
},
5054
} as const
5155

5256
export const DEFAULT_AJV_SETTINGS: Options = {
File renamed without changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'foo',
10+
schema: {
11+
type: 'string',
12+
format: 'uuid',
13+
},
14+
},
15+
],
16+
}
17+
}
18+
},
19+
},
20+
request: {
21+
route: 'foo',
22+
query: {
23+
foo: '0210a341-d7f9-4d5f-80e5-db5e8143ea12'
24+
}
25+
},
26+
};
27+
28+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
required: ['test1', 'test2'],
17+
},
18+
},
19+
},
20+
},
21+
schemas: {
22+
Test1: {
23+
properties: {
24+
foo: {
25+
type: 'string',
26+
},
27+
},
28+
required: ['foo'],
29+
},
30+
Test2: {
31+
properties: {
32+
boo: {
33+
type: 'string',
34+
},
35+
},
36+
required: ['boo'],
37+
},
38+
},
39+
},
40+
request: {
41+
body: {
42+
test1: {
43+
foo: 'asdf',
44+
},
45+
test2: {
46+
boo: 'ccccc',
47+
},
48+
},
49+
},
50+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module.exports = {
2+
validateArgs: {
3+
requestBody: {
4+
description: 'a test body',
5+
content: {
6+
'application/json': {
7+
schema: {
8+
$ref: '#/components/schemas/Test1',
9+
},
10+
},
11+
},
12+
},
13+
schemas: {
14+
Test1: {
15+
properties: {
16+
foo: {
17+
type: 'string',
18+
},
19+
},
20+
required: ['foo'],
21+
},
22+
},
23+
},
24+
request: {
25+
body: {
26+
foo: 'asdf',
27+
},
28+
},
29+
};
Lines changed: 43 additions & 0 deletions
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+
required: true,
6+
content: {
7+
'application/json': {
8+
schema: {
9+
properties: {
10+
test1: {
11+
$ref: '#/components/schemas/Test1',
12+
},
13+
},
14+
required: ['test1'],
15+
}
16+
},
17+
},
18+
},
19+
schemas: {
20+
Test1: {
21+
properties: {
22+
foo: {
23+
type: 'string',
24+
},
25+
recursive: {
26+
$ref: '#/components/schemas/Test1',
27+
},
28+
},
29+
required: ['foo'],
30+
},
31+
},
32+
},
33+
request: {
34+
body: {
35+
test1: {
36+
foo: 'asdf',
37+
recursive: {
38+
foo: 'boo',
39+
},
40+
},
41+
},
42+
},
43+
};
File renamed without changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = {
2+
validateArgs: {
3+
paths: {
4+
'/foo': {
5+
get: {
6+
parameters: [
7+
{
8+
in: 'query',
9+
name: 'foo',
10+
schema: {
11+
type: 'string',
12+
format: 'uuid',
13+
},
14+
},
15+
],
16+
}
17+
}
18+
},
19+
},
20+
request: {
21+
route: 'foo',
22+
query: {
23+
foo: 'hello'
24+
}
25+
},
26+
expectedErrors: [
27+
{
28+
code: 'Validation-format',
29+
source: {
30+
pointer: '#/paths/foo/get/parameters/foo/format'
31+
},
32+
status: 400,
33+
"title": 'must match format "uuid"'
34+
}
35+
]
36+
};
37+
38+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
validateArgs: {
3+
parameters: [],
4+
requestBody: {
5+
description: 'a multipart',
6+
content: {
7+
'multipart/form-data': {
8+
schema: {
9+
type: 'object',
10+
properties: {
11+
files: {
12+
type: 'array',
13+
items: { type: 'string', format: 'binary' },
14+
},
15+
},
16+
},
17+
},
18+
},
19+
},
20+
},
21+
request: {
22+
body: {
23+
files: [],
24+
},
25+
headers: {
26+
'content-type':
27+
'multipart/form-data; boundary=----WebKitFormBoundaryWyK9kAU7d35AKf26',
28+
},
29+
},
30+
requestOpts: {
31+
strictRequestBodyValidation: false
32+
}
33+
};

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ interface TestFixture {
4545

4646
requestOpts?: {
4747
strictQueryParamValidation?: boolean
48+
strictRequestBodyValidation?: boolean
4849
}
4950

5051
expectedErrors?: ErrorObj[]
5152
}
5253

5354
// 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!
55+
const onlyInclude: string[] = [] // can be used to only run specific tests. for local testing only!
5556
const fixtureDir = `${__dirname}/../fixtures`
5657
const files = fs.readdirSync(fixtureDir)
5758
const testCases: { [key: string]: TestFixture } = {}
@@ -95,7 +96,8 @@ describe('The api validator', () => {
9596
const result = validator.validateRequestBody(
9697
`/${fixture.request.route ?? 'test'}`,
9798
fixture.request.method ?? 'post',
98-
fixture.request.body
99+
fixture.request.body,
100+
fixture.requestOpts?.strictRequestBodyValidation ?? true
99101
)
100102
if (fixture.expectedErrors) {
101103
expect(result).toEqual(fixture.expectedErrors)
@@ -125,7 +127,12 @@ describe('The api validator', () => {
125127
}
126128
}
127129
if (operation.requestBody && fixture.request.body) {
128-
const result = validator.validateRequestBody(path, methodName, fixture.request.body)
130+
const result = validator.validateRequestBody(
131+
path,
132+
methodName,
133+
fixture.request.body,
134+
fixture.requestOpts?.strictRequestBodyValidation ?? true
135+
)
129136
if (fixture.expectedErrors) {
130137
expect(result).toEqual(fixture.expectedErrors)
131138
} else {

0 commit comments

Comments
 (0)