Skip to content

Commit a540ccc

Browse files
authored
fix(schema): handle non-string enum values (#1377)
* fix(schema): handle non-string enum values * test: update
1 parent 66e241b commit a540ccc

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"test": "jest --silent"
6363
},
6464
"dependencies": {
65-
"@stoplight/better-ajv-errors": "0.0.0",
65+
"@stoplight/better-ajv-errors": "0.0.3",
6666
"@stoplight/json": "3.9.0",
6767
"@stoplight/json-ref-readers": "1.2.1",
6868
"@stoplight/json-ref-resolver": "3.1.0",

src/functions/__tests__/schema.test.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ describe('schema', () => {
269269
it('reports pretty enum errors for a string', () => {
270270
expect(runSchema('baz', testSchema)).toEqual([
271271
{
272-
message: `String should be equal to one of the allowed values: foo, bar. Did you mean bar?`,
272+
message: 'String should be equal to one of the allowed values: `foo`, `bar`. Did you mean `bar`?',
273273
path: [],
274274
},
275275
]);
@@ -310,6 +310,31 @@ describe('schema', () => {
310310
]);
311311
});
312312
});
313+
314+
describe('and an enum contains a null', () => {
315+
const testSchema: JSONSchema6 = {
316+
$schema: `http://json-schema.org/draft-06/schema#`,
317+
enum: [1, null],
318+
};
319+
320+
it('reports pretty enum errors for a string', () => {
321+
expect(runSchema('baz', testSchema)).toEqual([
322+
{
323+
message: `String should be equal to one of the allowed values: 1, null`,
324+
path: [],
325+
},
326+
]);
327+
});
328+
329+
it('reports pretty enum errors for a number', () => {
330+
expect(runSchema(2, testSchema)).toEqual([
331+
{
332+
message: `Number should be equal to one of the allowed values: 1, null`,
333+
path: [],
334+
},
335+
]);
336+
});
337+
});
313338
});
314339

315340
test('reports slightly less pretty enum errors for primitive values that are not similar to any values in enum', () => {
@@ -321,7 +346,7 @@ describe('schema', () => {
321346

322347
expect(runSchema('three', testSchema)).toEqual([
323348
{
324-
message: `String should be equal to one of the allowed values: foo, bar`,
349+
message: 'String should be equal to one of the allowed values: `foo`, `bar`',
325350
path: [],
326351
},
327352
]);

src/rulesets/asyncapi/__tests__/asyncapi-headers-schema-type-object.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ describe(`Rule '${ruleName}'`, () => {
6363
expect.objectContaining({
6464
code: ruleName,
6565
message:
66-
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: object. Did you mean object?).',
66+
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: `object`. Did you mean `object`?).',
6767
path: ['components', 'messages', 'aMessage', 'headers', 'type'],
6868
severity: rule.severity,
6969
}),
@@ -98,7 +98,7 @@ describe(`Rule '${ruleName}'`, () => {
9898
expect.objectContaining({
9999
code: ruleName,
100100
message:
101-
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: object. Did you mean object?).',
101+
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: `object`. Did you mean `object`?).',
102102
path: ['components', 'messageTraits', 'aTrait', 'headers', 'type'],
103103
severity: rule.severity,
104104
}),
@@ -155,7 +155,7 @@ describe(`Rule '${ruleName}'`, () => {
155155
expect.objectContaining({
156156
code: ruleName,
157157
message:
158-
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: object. Did you mean object?).',
158+
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: `object`. Did you mean `object`?).',
159159
path: ['channels', 'users/{userId}/signedUp', property, 'message', 'headers', 'type'],
160160
severity: rule.severity,
161161
}),

test-harness/scenarios/examples.oas2.scenario

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ OpenAPI 2.0 (Swagger) detected
268268
95:26 error oas2-valid-schema-example `example` property should match format `date-time`
269269
99:26 error oas2-valid-schema-example `example` property should match format `url`
270270
106:11 warning operation-tag-defined Operation tags should be defined in global tags.
271-
120:22 error oas2-valid-schema-example `x-example` property should be equal to one of the allowed values: foo, bar
271+
120:22 error oas2-valid-schema-example `x-example` property should be equal to one of the allowed values: `foo`, `bar`
272272
177:30 error oas2-valid-media-example `application/json` property should have required property `name`
273273
194:30 error oas2-valid-media-example `application/json` property should have required property `user`
274274

test-harness/scenarios/examples.oas3.scenario

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,8 @@ OpenAPI 3.x detected
356356
138:30 error oas3-valid-schema-example `example` property type should be boolean
357357
142:30 error oas3-valid-schema-example `example` property should match format `date-time`
358358
146:30 error oas3-valid-schema-example `example` property should match format `url`
359-
169:20 error oas3-valid-media-example `example` property should be equal to one of the allowed values: foo, bar
360-
185:22 error oas3-valid-schema-example `example` property should be equal to one of the allowed values: foo, bar
359+
169:20 error oas3-valid-media-example `example` property should be equal to one of the allowed values: `foo`, `bar`
360+
185:22 error oas3-valid-schema-example `example` property should be equal to one of the allowed values: `foo`, `bar`
361361
197:22 error oas3-valid-media-example `value` property type should be string
362362
199:21 error oas3-valid-media-example `value` property type should be string
363363
262:25 error oas3-valid-media-example `value` property should have required property `id`

test-harness/scenarios/external-schemas-ruleset.scenario

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ info:
1111
OpenAPI 3.x detected
1212

1313
{document}
14-
3:10 error info-title `title` property should be equal to one of the allowed values: Stoplight, Stoplight.io, StoplightIO. Did you mean Stoplight?
15-
4:16 error info-description `description` property should be equal to one of the allowed values: foo, foo-bar, bar-foo
14+
3:10 error info-title `title` property should be equal to one of the allowed values: `Stoplight`, `Stoplight.io`, `StoplightIO`. Did you mean `Stoplight`?
15+
4:16 error info-description `description` property should be equal to one of the allowed values: `foo`, `foo-bar`, `bar-foo`
1616

1717
✖ 2 problems (2 errors, 0 warnings, 0 infos, 0 hints)

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,10 @@
593593
dependencies:
594594
type-detect "4.0.8"
595595

596-
"@stoplight/[email protected].0":
597-
version "0.0.0"
598-
resolved "https://registry.yarnpkg.com/@stoplight/better-ajv-errors/-/better-ajv-errors-0.0.0.tgz#18bf69c2380a00b72e4bd5fb7247c5adf615795a"
599-
integrity sha512-jA1i4J5HmCXMmZdZigdeEHpisFCGVg5QfK++ouxP58pxshATJ5G40U9uGec2vYnuR2MQnzWHAXYf0f8jvJhBwg==
596+
"@stoplight/[email protected].3":
597+
version "0.0.3"
598+
resolved "https://registry.yarnpkg.com/@stoplight/better-ajv-errors/-/better-ajv-errors-0.0.3.tgz#8d47a511aca89e0027cf748785910fbb25d7c54f"
599+
integrity sha512-q3PoPiYZdzfJjQ+q6ifuLVFx3dBKRxW1OBGxnLAoDlamYb+GJUNiaSLB32L6OB4fi6h/TsY21lZB7y7aYFM7tQ==
600600
dependencies:
601601
jsonpointer "^4.0.1"
602602
leven "^3.1.0"

0 commit comments

Comments
 (0)