Skip to content

Commit c5528ac

Browse files
committed
fix: refactor to support exceptions and standard ipa functions
1 parent e6ff859 commit c5528ac

File tree

5 files changed

+175
-149
lines changed

5 files changed

+175
-149
lines changed

tools/spectral/ipa/__tests__/IPA125OneOfMustHaveDiscriminator.test.js

Lines changed: 89 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,32 @@ import testRule from './__helpers__/testRule';
22
import { DiagnosticSeverity } from '@stoplight/types';
33

44
const componentSchemas = {
5-
schemas: {
6-
Dog: {
7-
type: 'object',
8-
properties: {
9-
breed: { type: 'string' },
10-
age: { type: 'integer' },
11-
},
5+
Dog: {
6+
type: 'object',
7+
properties: {
8+
breed: { type: 'string' },
9+
age: { type: 'integer' },
1210
},
13-
Cat: {
14-
type: 'object',
15-
properties: {
16-
color: { type: 'string' },
17-
livesLeft: { type: 'integer' },
18-
},
11+
},
12+
Cat: {
13+
type: 'object',
14+
properties: {
15+
color: { type: 'string' },
16+
livesLeft: { type: 'integer' },
1917
},
20-
Bird: {
21-
type: 'object',
22-
properties: {
23-
species: { type: 'string' },
24-
wingspan: { type: 'number' },
25-
},
18+
},
19+
Bird: {
20+
type: 'object',
21+
properties: {
22+
species: { type: 'string' },
23+
wingspan: { type: 'number' },
2624
},
27-
Fish: {
28-
type: 'object',
29-
properties: {
30-
species: { type: 'string' },
31-
waterType: { type: 'string' },
32-
},
25+
},
26+
Fish: {
27+
type: 'object',
28+
properties: {
29+
species: { type: 'string' },
30+
waterType: { type: 'string' },
3331
},
3432
},
3533
};
@@ -38,15 +36,17 @@ testRule('xgen-IPA-125-oneOf-must-have-discriminator', [
3836
{
3937
name: 'valid oneOf with discriminator and matching mapping',
4038
document: {
41-
components: componentSchemas,
42-
schemas: {
43-
Animal: {
44-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
45-
discriminator: {
46-
propertyName: 'type',
47-
mapping: {
48-
dog: '#/components/schemas/Dog',
49-
cat: '#/components/schemas/Cat',
39+
components: {
40+
schemas: {
41+
...componentSchemas,
42+
Animal: {
43+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
44+
discriminator: {
45+
propertyName: 'type',
46+
mapping: {
47+
dog: '#/components/schemas/Dog',
48+
cat: '#/components/schemas/Cat',
49+
},
5050
},
5151
},
5252
},
@@ -57,15 +57,17 @@ testRule('xgen-IPA-125-oneOf-must-have-discriminator', [
5757
{
5858
name: 'invalid oneOf with discriminator but mismatched mapping',
5959
document: {
60-
components: componentSchemas,
61-
schemas: {
62-
Animal: {
63-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
64-
discriminator: {
65-
propertyName: 'type',
66-
mapping: {
67-
dog: '#/components/schemas/Dog',
68-
bird: '#/components/schemas/Bird',
60+
components: {
61+
schemas: {
62+
...componentSchemas,
63+
Animal: {
64+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
65+
discriminator: {
66+
propertyName: 'type',
67+
mapping: {
68+
dog: '#/components/schemas/Dog',
69+
bird: '#/components/schemas/Bird',
70+
},
6971
},
7072
},
7173
},
@@ -76,61 +78,67 @@ testRule('xgen-IPA-125-oneOf-must-have-discriminator', [
7678
code: 'xgen-IPA-125-oneOf-must-have-discriminator',
7779
message:
7880
'The discriminator mapping must match the oneOf references. Unmatched Discriminator mappings with oneOf references: #/components/schemas/Bird',
79-
path: ['schemas', 'Animal'],
81+
path: ['components', 'schemas', 'Animal'],
8082
severity: DiagnosticSeverity.Error,
8183
},
8284
],
8385
},
8486
{
8587
name: 'invalid oneOf without discriminator',
8688
document: {
87-
components: componentSchemas,
88-
schemas: {
89-
Animal: {
90-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
89+
components: {
90+
schemas: {
91+
...componentSchemas,
92+
Animal: {
93+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
94+
},
9195
},
9296
},
9397
},
9498
errors: [
9599
{
96100
code: 'xgen-IPA-125-oneOf-must-have-discriminator',
97101
message: 'The schema has oneOf but no discriminator property.',
98-
path: ['schemas', 'Animal'],
102+
path: ['components', 'schemas', 'Animal'],
99103
severity: DiagnosticSeverity.Error,
100104
},
101105
],
102106
},
103107
{
104108
name: 'invalid oneOf with non-object discriminator',
105109
document: {
106-
components: componentSchemas,
107-
schemas: {
108-
Animal: {
109-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
110-
discriminator: "I'm a string, not an object!",
110+
components: {
111+
schemas: {
112+
...componentSchemas,
113+
Animal: {
114+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
115+
discriminator: "I'm a string, not an object!",
116+
},
111117
},
112118
},
113119
},
114120
errors: [
115121
{
116122
code: 'xgen-IPA-125-oneOf-must-have-discriminator',
117123
message: 'Discriminator property is not an object.',
118-
path: ['schemas', 'Animal'],
124+
path: ['components', 'schemas', 'Animal'],
119125
severity: DiagnosticSeverity.Error,
120126
},
121127
],
122128
},
123129
{
124130
name: 'invalid oneOf with discriminator but no propertyName',
125131
document: {
126-
components: componentSchemas,
127-
schemas: {
128-
Animal: {
129-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
130-
discriminator: {
131-
mapping: {
132-
dog: '#/components/schemas/Dog',
133-
cat: '#/components/schemas/Cat',
132+
components: {
133+
schemas: {
134+
...componentSchemas,
135+
Animal: {
136+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
137+
discriminator: {
138+
mapping: {
139+
dog: '#/components/schemas/Dog',
140+
cat: '#/components/schemas/Cat',
141+
},
134142
},
135143
},
136144
},
@@ -140,20 +148,22 @@ testRule('xgen-IPA-125-oneOf-must-have-discriminator', [
140148
{
141149
code: 'xgen-IPA-125-oneOf-must-have-discriminator',
142150
message: 'Discriminator has no propertyName defined.',
143-
path: ['schemas', 'Animal'],
151+
path: ['components', 'schemas', 'Animal'],
144152
severity: DiagnosticSeverity.Error,
145153
},
146154
],
147155
},
148156
{
149157
name: 'invalid oneOf with discriminator but no mapping',
150158
document: {
151-
components: componentSchemas,
152-
schemas: {
153-
Animal: {
154-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
155-
discriminator: {
156-
propertyName: 'type',
159+
components: {
160+
schemas: {
161+
...componentSchemas,
162+
Animal: {
163+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
164+
discriminator: {
165+
propertyName: 'type',
166+
},
157167
},
158168
},
159169
},
@@ -162,20 +172,22 @@ testRule('xgen-IPA-125-oneOf-must-have-discriminator', [
162172
{
163173
code: 'xgen-IPA-125-oneOf-must-have-discriminator',
164174
message: 'Discriminator must have a mapping object.',
165-
path: ['schemas', 'Animal'],
175+
path: ['components', 'schemas', 'Animal'],
166176
severity: DiagnosticSeverity.Error,
167177
},
168178
],
169179
},
170180
{
171181
name: 'oneOf with discriminator exemption',
172182
document: {
173-
components: componentSchemas,
174-
schemas: {
175-
Animal: {
176-
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
177-
'x-xgen-IPA-exception': {
178-
'xgen-IPA-125-oneOf-must-have-discriminator': 'reason for exemption',
183+
components: {
184+
schemas: {
185+
...componentSchemas,
186+
Animal: {
187+
oneOf: [{ $ref: '#/components/schemas/Dog' }, { $ref: '#/components/schemas/Cat' }],
188+
'x-xgen-IPA-exception': {
189+
'xgen-IPA-125-oneOf-must-have-discriminator': 'reason for exemption',
190+
},
179191
},
180192
},
181193
},

0 commit comments

Comments
 (0)