Skip to content

Commit ecf2a44

Browse files
authored
chore: decrease severity of valid schema/content example validation (#228)
1 parent 3dfec27 commit ecf2a44

File tree

5 files changed

+38
-34
lines changed

5 files changed

+38
-34
lines changed

src/.defaultsForValidator.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,14 @@ const defaults = {
120120
'oas2-api-host': "warning",
121121
'oas2-api-schemes': "warning",
122122
'oas2-host-trailing-slash': "warning",
123-
'oas2-valid-example': "warning",
124-
'oas2-valid-definition-example': "error",
123+
'oas2-valid-definition-example': "warning",
125124
'oas2-anyOf': "warning",
126125
'oas2-oneOf': "warning",
127126
'oas3-api-servers': "warning",
128127
'oas3-examples-value-or-externalValue': "warning",
129128
'oas3-server-trailing-slash': "warning",
130-
'oas3-valid-example': "error",
131-
'oas3-valid-schema-example': "error"
129+
'oas3-valid-oas-content-example': "warning",
130+
'oas3-valid-schema-example': "warning"
132131
}
133132
}
134133
};

src/spectral/rulesets/.defaultsForSpectral.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ rules:
5454
oas2-operation-security-defined: off
5555
# Turn off
5656
oas2-valid-parameter-example: off
57-
# Enable with same severity as Spectral
58-
oas2-valid-definition-example: true
57+
# Enable with warn severity
58+
oas2-valid-definition-example: warn
5959
# Turn off
6060
oas2-valid-response-example: off
6161
# Turn off
@@ -80,14 +80,14 @@ rules:
8080
oas3-valid-oas-parameter-example: off
8181
# Turn off
8282
oas3-valid-oas-header-example: off
83-
# Turn off
84-
oas3-valid-oas-content-example: off
83+
# Enable with warn severity
84+
oas3-valid-oas-content-example: warn
8585
# Turn off
8686
oas3-valid-parameter-schema-example: off
8787
# Turn off
8888
oas3-valid-header-schema-example: off
89-
# Enable with same severity as Spectral
90-
oas3-valid-schema-example: true
89+
# Enable with warn severity
90+
oas3-valid-schema-example: warn
9191
# Turn off
9292
oas3-schema: off
9393
# Turn off - duplicates non-configurable validation in base validator

test/spectral/mockFiles/oas3/enabled-rules.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ paths:
5858
application/json:
5959
schema:
6060
$ref: '#/components/schemas/TheBadModel'
61+
example:
62+
number_of_connectors: 3
6163
responses:
6264
204:
6365
description: "Success"

test/spectral/tests/enabled-rules.test.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ describe('spectral - test enabled rules - Swagger 2', function() {
3030
foundOtherValidator = true;
3131
}
3232
});
33-
// Since some spectral validations will result in errors, the exit code should equal 1
34-
expect(exitCode).toEqual(1);
33+
34+
expect(exitCode).toEqual(0);
3535
expect(foundOtherValidator).toBe(false);
3636

3737
consoleSpy.mockRestore();
@@ -124,14 +124,12 @@ describe('spectral - test enabled rules - Swagger 2 In Memory', function() {
124124
defaultMode
125125
);
126126

127-
// should produce an object with `errors` and `warnings` keys that should
128-
// both be non-empty
129-
expect(validationResults.errors.length).toBeGreaterThan(0);
127+
expect(validationResults.errors.length).toEqual(0);
130128
expect(validationResults.warnings.length).toBeGreaterThan(0);
131129

132130
errors = validationResults.errors.map(error => error.message);
133131
warnings = validationResults.warnings.map(warn => warn.message);
134-
expect(errors.length).toBeGreaterThan(0);
132+
expect(errors.length).toEqual(0);
135133
expect(warnings.length).toBeGreaterThan(0);
136134
});
137135

@@ -219,11 +217,11 @@ describe('spectral - test enabled rules - Swagger 2 In Memory', function() {
219217
);
220218
});
221219

222-
it('test oas2-valid-example rule using mockFiles/swagger/enabled-rules-in-memory', function() {
223-
expect(errors).toContain(
220+
it('test oas2-valid-definition-example rule using mockFiles/swagger/enabled-rules-in-memory', function() {
221+
expect(errors).not.toContain(
224222
'`number_of_coins` property type should be integer'
225223
);
226-
expect(warnings).not.toContain(
224+
expect(warnings).toContain(
227225
'`number_of_coins` property type should be integer'
228226
);
229227
});
@@ -273,8 +271,8 @@ describe('spectral - test enabled rules - OAS3', function() {
273271
foundOtherValidator = true;
274272
}
275273
});
276-
// Since some spectral validations will result in errors, the exit code should equal 1
277-
expect(exitCode).toEqual(1);
274+
275+
expect(exitCode).toEqual(0);
278276
expect(foundOtherValidator).toBe(false);
279277

280278
consoleSpy.mockRestore();
@@ -336,11 +334,17 @@ describe('spectral - test enabled rules - OAS3', function() {
336334
);
337335
});
338336

339-
it('test oas3-valid-example rule using mockFiles/oas3/enabled-rules.yml', function() {
337+
it('test oas3-valid-schema-example rule using mockFiles/oas3/enabled-rules.yml', function() {
340338
expect(allOutput).toContain(
341339
'`number_of_coins` property type should be integer'
342340
);
343341
});
342+
343+
it('test oas3-valid-oas-content-example rule using mockFiles/oas3/enabled-rules.yml', function() {
344+
expect(allOutput).toContain(
345+
'`number_of_connectors` property should be equal to one of the allowed values: 1, 2, a_string, 8'
346+
);
347+
});
344348
});
345349

346350
describe('spectral - test enabled rules - OAS3 In Memory', function() {
@@ -352,14 +356,13 @@ describe('spectral - test enabled rules - OAS3 In Memory', function() {
352356
const defaultMode = true;
353357
const validationResults = await inCodeValidator(oas3InMemory, defaultMode);
354358

355-
// should produce an object with `errors` and `warnings` keys that should
356-
// both be non-empty
357-
expect(validationResults.errors.length).toBeGreaterThan(0);
359+
// should produce an object with an empty `errors` key and a non-empty `warnings` key
360+
expect(validationResults.errors.length).toEqual(0);
358361
expect(validationResults.warnings.length).toBeGreaterThan(0);
359362

360363
errors = validationResults.errors.map(error => error.message);
361364
warnings = validationResults.warnings.map(warn => warn.message);
362-
expect(errors.length).toBeGreaterThan(0);
365+
expect(errors.length).toEqual(0);
363366
expect(warnings.length).toBeGreaterThan(0);
364367
});
365368

@@ -448,10 +451,10 @@ describe('spectral - test enabled rules - OAS3 In Memory', function() {
448451
});
449452

450453
it('test oas3-valid-example rule using mockFiles/oas3/enabled-rules-in-memory', function() {
451-
expect(errors).toContain(
454+
expect(errors).not.toContain(
452455
'`number_of_coins` property type should be integer'
453456
);
454-
expect(warnings).not.toContain(
457+
expect(warnings).toContain(
455458
'`number_of_coins` property type should be integer'
456459
);
457460
});

test/spectral/tests/spectral-config.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ describe('Spectral - test custom configuration', function() {
9494
);
9595

9696
// Verify warnings
97-
expect(jsonOutput['warnings']['spectral'].length).toBe(17);
97+
expect(jsonOutput['warnings']['spectral'].length).toBe(18);
9898
const warnings = jsonOutput['warnings']['spectral'].map(w => w['message']);
9999
// This warning should be turned off
100100
expect(warnings).not.toContain(
@@ -135,18 +135,18 @@ describe('Spectral - test custom configuration', function() {
135135

136136
consoleSpy.mockRestore();
137137

138-
// Verify errors
139-
expect(jsonOutput['errors']['spectral'].length).toBe(2);
138+
// Verify there are no errors
139+
expect(jsonOutput['errors']['spectral']).toBeUndefined();
140140

141141
// Verify warnings
142-
expect(jsonOutput['warnings']['spectral'].length).toBe(21);
142+
expect(jsonOutput['warnings']['spectral'].length).toBe(23);
143143
const warnings = jsonOutput['warnings']['spectral'].map(w => w['message']);
144-
// This is the new warning -- there should be four occurrences
144+
// This is the new warning -- there should be three occurrences
145145
const warning = 'All request bodies should have an example.';
146146
const occurrences = warnings.reduce(
147147
(a, v) => (v === warning ? a + 1 : a),
148148
0
149149
);
150-
expect(occurrences).toBe(4);
150+
expect(occurrences).toBe(3);
151151
});
152152
});

0 commit comments

Comments
 (0)