Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(schema): handle non-string enum values #1377

Merged
merged 2 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"test": "jest --silent"
},
"dependencies": {
"@stoplight/better-ajv-errors": "0.0.0",
"@stoplight/better-ajv-errors": "0.0.3",
"@stoplight/json": "3.9.0",
"@stoplight/json-ref-readers": "1.2.1",
"@stoplight/json-ref-resolver": "3.1.0",
Expand Down
29 changes: 27 additions & 2 deletions src/functions/__tests__/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('schema', () => {
it('reports pretty enum errors for a string', () => {
expect(runSchema('baz', testSchema)).toEqual([
{
message: `String should be equal to one of the allowed values: foo, bar. Did you mean bar?`,
message: 'String should be equal to one of the allowed values: `foo`, `bar`. Did you mean `bar`?',
path: [],
},
]);
Expand Down Expand Up @@ -310,6 +310,31 @@ describe('schema', () => {
]);
});
});

describe('and an enum contains a null', () => {
const testSchema: JSONSchema6 = {
$schema: `http://json-schema.org/draft-06/schema#`,
enum: [1, null],
};

it('reports pretty enum errors for a string', () => {
expect(runSchema('baz', testSchema)).toEqual([
{
message: `String should be equal to one of the allowed values: 1, null`,
path: [],
},
]);
});

it('reports pretty enum errors for a number', () => {
expect(runSchema(2, testSchema)).toEqual([
{
message: `Number should be equal to one of the allowed values: 1, null`,
path: [],
},
]);
});
});
});

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

expect(runSchema('three', testSchema)).toEqual([
{
message: `String should be equal to one of the allowed values: foo, bar`,
message: 'String should be equal to one of the allowed values: `foo`, `bar`',
path: [],
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe(`Rule '${ruleName}'`, () => {
expect.objectContaining({
code: ruleName,
message:
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: object. Did you mean object?).',
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: `object`. Did you mean `object`?).',
path: ['components', 'messages', 'aMessage', 'headers', 'type'],
severity: rule.severity,
}),
Expand Down Expand Up @@ -98,7 +98,7 @@ describe(`Rule '${ruleName}'`, () => {
expect.objectContaining({
code: ruleName,
message:
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: object. Did you mean object?).',
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: `object`. Did you mean `object`?).',
path: ['components', 'messageTraits', 'aTrait', 'headers', 'type'],
severity: rule.severity,
}),
Expand Down Expand Up @@ -155,7 +155,7 @@ describe(`Rule '${ruleName}'`, () => {
expect.objectContaining({
code: ruleName,
message:
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: object. Did you mean object?).',
'Headers schema type should be `object` (`type` property should be equal to one of the allowed values: `object`. Did you mean `object`?).',
path: ['channels', 'users/{userId}/signedUp', property, 'message', 'headers', 'type'],
severity: rule.severity,
}),
Expand Down
2 changes: 1 addition & 1 deletion test-harness/scenarios/examples.oas2.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ OpenAPI 2.0 (Swagger) detected
95:26 error oas2-valid-schema-example `example` property should match format `date-time`
99:26 error oas2-valid-schema-example `example` property should match format `url`
106:11 warning operation-tag-defined Operation tags should be defined in global tags.
120:22 error oas2-valid-schema-example `x-example` property should be equal to one of the allowed values: foo, bar
120:22 error oas2-valid-schema-example `x-example` property should be equal to one of the allowed values: `foo`, `bar`
177:30 error oas2-valid-media-example `application/json` property should have required property `name`
194:30 error oas2-valid-media-example `application/json` property should have required property `user`

Expand Down
4 changes: 2 additions & 2 deletions test-harness/scenarios/examples.oas3.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ OpenAPI 3.x detected
138:30 error oas3-valid-schema-example `example` property type should be boolean
142:30 error oas3-valid-schema-example `example` property should match format `date-time`
146:30 error oas3-valid-schema-example `example` property should match format `url`
169:20 error oas3-valid-media-example `example` property should be equal to one of the allowed values: foo, bar
185:22 error oas3-valid-schema-example `example` property should be equal to one of the allowed values: foo, bar
169:20 error oas3-valid-media-example `example` property should be equal to one of the allowed values: `foo`, `bar`
185:22 error oas3-valid-schema-example `example` property should be equal to one of the allowed values: `foo`, `bar`
197:22 error oas3-valid-media-example `value` property type should be string
199:21 error oas3-valid-media-example `value` property type should be string
262:25 error oas3-valid-media-example `value` property should have required property `id`
Expand Down
4 changes: 2 additions & 2 deletions test-harness/scenarios/external-schemas-ruleset.scenario
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ info:
OpenAPI 3.x detected

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

✖ 2 problems (2 errors, 0 warnings, 0 infos, 0 hints)
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,10 @@
dependencies:
type-detect "4.0.8"

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