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

feat: new rule to detect enum value that do not respect specified type #913

Merged
merged 2 commits into from
Jan 22, 2020

Conversation

nulltoken
Copy link
Contributor

@nulltoken nulltoken commented Jan 14, 2020

Fixes #570

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

@nulltoken nulltoken changed the title feat: new rule to detect enum value that do not respect specified format [WIP] feat: new rule to detect enum value that do not respect specified format Jan 14, 2020
@nulltoken nulltoken force-pushed the ntk/formated_enum branch 2 times, most recently from effcee9 to c194d2c Compare January 15, 2020 08:06
@nulltoken
Copy link
Contributor Author

nulltoken commented Jan 15, 2020

@rossmcdonald Could you please take a look at this, please? It's not done yet, but this should be usable for a first round of tests.

If you could also run some local tests by checking this branch out and running yarn cli lint... against a couple files that would be amazing!

@P0lip I'd also be a taker for a review pass. FWIW, I foresee to enhance the testing of the function by throwing at it more combinations of formats and values.

Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it!
If you fix typos and add tests, we should be all set.

src/functions/formatedEnum.ts Outdated Show resolved Hide resolved
src/functions/index.ts Outdated Show resolved Hide resolved
src/functions/formatedEnum.ts Outdated Show resolved Hide resolved
src/functions/formatedEnum.ts Outdated Show resolved Hide resolved
@nulltoken nulltoken force-pushed the ntk/formated_enum branch 2 times, most recently from 78e8b1e to 59e547d Compare January 16, 2020 23:27
@nulltoken
Copy link
Contributor Author

Just pushed an update to ease the coverage of detection of invalid format usage

@nulltoken nulltoken requested review from rossmcdonald and removed request for chris-miaskowski January 17, 2020 07:40
Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LMK once you are done

src/functions/formattedEnum.ts Outdated Show resolved Hide resolved
src/functions/formattedEnum.ts Outdated Show resolved Hide resolved
@philsturgeon
Copy link
Contributor

This might actually go above and beyond. The feature request was for type, but this is type and format. Format is notoriously vague and I worry we're opening ourselves up to a maintainability nightmare, because JSON Schema 2019-09 has several new formats, and we're moving away from format being a validation keyword.

Could we stick to type, and if people really want this in the future we can find a way to make it work with JSON Schema 2019-09 (and OAS 3.1)?

@nulltoken
Copy link
Contributor Author

This might actually go above and beyond. The feature request was for type, but this is type and format. Format is notoriously vague and I worry we're opening ourselves up to a maintainability nightmare, because JSON Schema 2019-09 has several new formats, and we're moving away from format being a validation keyword.

So my attempt at properly narrowing the scope in #570 (comment) was eventually ... completely useless 😜

Could we stick to type, and if people really want this in the future we can find a way to make it work with JSON Schema 2019-09 (and OAS 3.1)?

No problem. I'll dumb it down and should have something ready by tomorrow evening.

@rossmcdonald
Copy link
Contributor

Sorry @nulltoken, I read 'format' as 'type' in your comment, so I'm most likely to blame for the extra effort here.

After speaking with @philsturgeon, I also wasn't aware of how much 'format' was a can of worms when it comes to validation, so I agree limiting scope now will probably save us from a 'death by a thousand cuts' later as everyone has their own specific definition of each format.

@nulltoken
Copy link
Contributor Author

@rossmcdonald No problem! I'm actually happy to reschedule all the life-lessons I could learn from 'formats' 😄

@philsturgeon I've pushed a simpler version. Does it fit you better?

BTW, although I've tried to be cautious, it's possible some "format" related things may still hide in the corners. If you spot some, please add a comment so that I can clean that up.

['integer', [-2147483648, 17], 12.3],
['boolean', [true, false], 1],
// array ?
// object ?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@philsturgeon I now realize I've actually never met this use case.

It looks like (from my reading of both oas2 and oas3 specs) that an enum could also typed as an array or an object.

Do you confirm this point?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OAS 2 and 3 don't have any opinions of their own about what is or is not a valid instance for an enum, but, JSON Schema does: https://json-schema.org/draft/2019-09/json-schema-validation.html#enum

It says it can be used for anything, but I have never see anyone try to doenum: [[0, 1], [2,3]] and I don't think we want to waste our time figuring out how to make that happen right now. Let's stick to the cases you've mentioned here, and make sure we test no type (no type = any, so no error)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's stick to the cases you've mentioned here
@phil Done.

make sure we test no type (no type = any, so no error)
Covered by an additional test case


## typedEnum

Models described by both a `type` and an `enum` must only expose enum values that respect this type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Models described by both a `type` and an `enum` must only expose enum values that respect this type.
When both a `type` and `enum` are defined for a property, the enum values must respect the type.

This makes me think this would be better off as a custom function for the oas ruleset. Afterall, this is oas/json schema specific, which means it doesn't really belong in core functions, right?


name | description | required?
-----------------------|--------------------------------------------------------------------|----------
reportingThreshold | the maximum number of invalid values reported in the error message | yes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unique to this rule, whats the idea here?

['integer', [-2147483648, 17], 12.3],
['boolean', [true, false], 1],
// array ?
// object ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OAS 2 and 3 don't have any opinions of their own about what is or is not a valid instance for an enum, but, JSON Schema does: https://json-schema.org/draft/2019-09/json-schema-validation.html#enum

It says it can be used for anything, but I have never see anyone try to doenum: [[0, 1], [2,3]] and I don't think we want to waste our time figuring out how to make that happen right now. Let's stick to the cases you've mentioned here, and make sure we test no type (no type = any, so no error)


expect(runTypedEnum(schema, threshold)).toEqual([
{
message: `Some enum values do not respect the specified type "integer": ${errorMessageTrail}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the error be reported for each invalid enum value instead of all of them? this might cut out the need for reportingThreshold.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phil Fixed. Multiple messages are now returned and reportingThreahold has been shown the door.

@@ -0,0 +1,33 @@
====test====
Identify enum values that do not respect the specified format
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Identify enum values that do not respect the specified format
Identify enum values that do not respect the specified type

});

describe('basic', () => {
test('should return undefined when all enum values respect the format', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('should return undefined when all enum values respect the format', () => {
it('is undefined when all enum values respect the type', () => {

expect(runTypedEnum(schema, defaultReportingThreshold)).toBeUndefined();
});

test('should identify enum values that do not respect the format', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('should identify enum values that do not respect the format', () => {
it('identifies each enum value which does not respect the type', () => {

@philsturgeon philsturgeon changed the title [WIP] feat: new rule to detect enum value that do not respect specified format [WIP] feat: new rule to detect enum value that do not respect specified type Jan 17, 2020
package.json Outdated Show resolved Hide resolved
philsturgeon
philsturgeon previously approved these changes Jan 21, 2020
@nulltoken nulltoken changed the title [WIP] feat: new rule to detect enum value that do not respect specified type feat: new rule to detect enum value that do not respect specified type Jan 21, 2020
P0lip
P0lip previously approved these changes Jan 21, 2020
Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor comments, pre-approving!

src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/rulesets/oas/__tests__/typed-enum.ts Outdated Show resolved Hide resolved
src/functions/typedEnum.ts Outdated Show resolved Hide resolved
src/functions/typedEnum.ts Outdated Show resolved Hide resolved
src/functions/typedEnum.ts Outdated Show resolved Hide resolved
@nulltoken nulltoken dismissed stale reviews from P0lip and philsturgeon via 69e0af2 January 21, 2020 21:39
@nulltoken
Copy link
Contributor Author

@P0lip All required changes have been fixed.

P0lip
P0lip previously approved these changes Jan 21, 2020
@P0lip P0lip added the enhancement New feature or request label Jan 21, 2020
@nulltoken
Copy link
Contributor Author

@philsturgeon It looks like the Check Markdown Links failed because it cannot resolve https://offset.earth/stoplightinc

Sweet irony!

image

And.. indeed, it's down

image


image

@marbemac
Copy link
Contributor

Ha - looks like the offset earth site is back up - just kicked off a re-run of those checks. 🤞

@nulltoken nulltoken merged commit 8d16284 into develop Jan 22, 2020
@nulltoken nulltoken deleted the ntk/formated_enum branch January 22, 2020 16:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create OpenAPI rule for enums that don't match the attribute type
5 participants