/
path.spec.ts
113 lines (103 loc) · 3.76 KB
/
path.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { HttpParamStyles, IHttpPathParam } from '@stoplight/types';
import { path as registry } from '../../deserializers';
import { HttpPathValidator } from '../path';
import * as validateAgainstSchemaModule from '../utils';
import { assertLeft, assertRight } from '@stoplight/prism-core/src/__tests__/utils';
import * as O from 'fp-ts/lib/Option';
describe('HttpPathValidator', () => {
const httpPathValidator = new HttpPathValidator(registry, 'path');
beforeEach(() => {
jest.clearAllMocks();
jest.spyOn(registry, 'get');
jest.spyOn(validateAgainstSchemaModule, 'validateAgainstSchema');
});
describe('validate()', () => {
describe('spec is present', () => {
describe('path param is not present', () => {
describe('spec defines it as required', () => {
it('returns validation error', () => {
assertLeft(
httpPathValidator.validate({}, [{ name: 'aParam', style: HttpParamStyles.Simple, required: true }]),
error =>
expect(error).toEqual([
{
code: 'required',
message: "should have required property 'aparam'",
path: ['path'],
severity: 0,
},
])
);
});
});
});
describe('path param is present', () => {
describe('schema is present', () => {
describe('deserializer not available', () => {
it('omits schema validation', () => {
jest.spyOn(registry, 'get').mockReturnValueOnce(undefined);
const param: IHttpPathParam = {
name: 'param',
style: HttpParamStyles.Simple,
schema: { type: 'number' },
};
assertRight(httpPathValidator.validate({ param: 'abc' }, [param]));
expect(validateAgainstSchemaModule.validateAgainstSchema).toReturnWith(O.none);
});
});
describe('deserializer is available', () => {
describe('path param is valid', () => {
it('validates positively against schema', () => {
assertRight(
httpPathValidator.validate({ param: 'abc' }, [
{
name: 'param',
style: HttpParamStyles.Simple,
schema: { type: 'string' },
},
])
);
expect(validateAgainstSchemaModule.validateAgainstSchema).toReturnWith(O.none);
});
});
});
});
describe('schema was not provided', () => {
it('omits schema validation', () => {
assertRight(
httpPathValidator.validate({ param: 'abc' }, [
{
name: 'param',
style: HttpParamStyles.Simple,
},
])
);
expect(validateAgainstSchemaModule.validateAgainstSchema).toReturnWith(O.none);
});
});
describe('deprecated flag is set', () => {
it('returns deprecation warning', () => {
assertLeft(
httpPathValidator.validate({ param: 'abc' }, [
{
name: 'param',
deprecated: true,
style: HttpParamStyles.Simple,
},
]),
error =>
expect(error).toEqual([
{
code: 'deprecated',
message: 'Path param param is deprecated',
path: ['path', 'param'],
severity: 1,
},
])
);
});
});
});
});
});
});