-
-
Notifications
You must be signed in to change notification settings - Fork 242
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
Conversation
effcee9
to
c194d2c
Compare
@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 @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. |
There was a problem hiding this 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.
78e8b1e
to
59e547d
Compare
Just pushed an update to ease the coverage of detection of invalid format usage |
f8f05d0
to
c0d2d7e
Compare
There was a problem hiding this 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
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)? |
So my attempt at properly narrowing the scope in #570 (comment) was eventually ... completely useless 😜
No problem. I'll dumb it down and should have something ready by tomorrow evening. |
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. |
@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 ? |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
91dc358
to
f452d6e
Compare
docs/reference/functions.md
Outdated
|
||
## typedEnum | ||
|
||
Models described by both a `type` and an `enum` must only expose enum values that respect this type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
docs/reference/functions.md
Outdated
|
||
name | description | required? | ||
-----------------------|--------------------------------------------------------------------|---------- | ||
reportingThreshold | the maximum number of invalid values reported in the error message | yes |
There was a problem hiding this comment.
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 ? |
There was a problem hiding this comment.
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}`, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test('should identify enum values that do not respect the format', () => { | |
it('identifies each enum value which does not respect the type', () => { |
c9d55e2
to
1844f2b
Compare
ad9ef43
to
4c197b5
Compare
There was a problem hiding this 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!
69e0af2
to
fe655ec
Compare
@P0lip All required changes have been fixed. |
fe655ec
to
dd02a4e
Compare
@philsturgeon It looks like the Check Markdown Links failed because it cannot resolve https://offset.earth/stoplightinc Sweet irony! And.. indeed, it's down |
Ha - looks like the offset earth site is back up - just kicked off a re-run of those checks. 🤞 |
Fixes #570
Checklist
Does this PR introduce a breaking change?