-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Parameter with required and example fails valid-example rule #243
Conversation
Bumps [@stoplight/types](https://github.com/stoplightio/types) from 5.1.2 to 7.0.1. - [Release notes](https://github.com/stoplightio/types/releases) - [Commits](stoplightio/types@v5.1.2...v7.0.1) Signed-off-by: dependabot[bot] <support@dependabot.com>
Bumps [yaml](https://github.com/eemeli/yaml) from 1.5.1 to 1.6.0. - [Release notes](https://github.com/eemeli/yaml/releases) - [Commits](eemeli/yaml@v1.5.1...v1.6.0) Signed-off-by: dependabot[bot] <support@dependabot.com>
* fix: handle ajv resolving issues * feat: include resolver errors
Bumps [tar](https://github.com/npm/node-tar) from 2.2.1 to 2.2.2. **This update includes security fixes.** - [Release notes](https://github.com/npm/node-tar/releases) - [Commits](isaacs/node-tar@v2.2.1...v2.2.2)
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 12.0.2 to 12.0.3. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node)
Bumps [typescript](https://github.com/Microsoft/TypeScript) from 3.4.5 to 3.5.1. - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](https://github.com/Microsoft/TypeScript/commits)
Bumps [@stoplight/types](https://github.com/stoplightio/types) from 7.0.1 to 7.1.0. - [Release notes](https://github.com/stoplightio/types/releases) - [Commits](stoplightio/types@v7.0.1...v7.1.0)
Bumps [@oclif/command](https://github.com/oclif/command) from 1.5.13 to 1.5.14. - [Release notes](https://github.com/oclif/command/releases) - [Changelog](https://github.com/oclif/command/blob/master/CHANGELOG.md) - [Commits](oclif/command@v1.5.13...v1.5.14)
Bumps [@types/lodash](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/lodash) from 4.14.130 to 4.14.133. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/lodash)
src/functions/schema-path.ts
Outdated
}; | ||
|
||
function withoutRequiredIfNotArray(schemaObject: any) { |
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.
Is this required or is this an older change that is left in here?
This should not need to be done. If required: true
is there it means the rule is operating on a parameter object, which with the jsonpath rules you've set up, it shouldnt ever be.
The only way a schema object will have a required: true in it, is because the user has invalid API specifications, and that'll get caught earlier.
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.
hi @phil :)
so oas3-schema
rule lints a spec with:
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
example: "2323"
example: "2323"
as OK. So, I'm assuming this is valid OAS3. Maybe it isn't?
Now:
valid-openapi-example
will matchschema
andschema.example
valid-example
will matchparameters[0]
andparameters[0].example
The problem is with 2
, there is required: true
on the object, that is later passed to
spectral/src/functions/schema.ts
Line 61 in 6590414
if (!ajv.validate(schemaObj, targetVal) && ajv.errors) { |
withoutRequiredIfNotArray
. Maybe oas3-schema
should not lint this case as OK ?
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 is entirely valid OAS3 yeah.
- valid-schema-example should match example against the
schema
object - valid-openapi-example should match example against the current (
$.
) 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.
@phil, done, withoutRequiredIfNotArray
is removed now as well
c28f8bf
to
db3b0d9
Compare
db3b0d9
to
8f45e90
Compare
This is pretty close but OpenAPI v2 and v3 are tricky beasts. Let's do some TDD and I'll write up the tests for you to code into. |
@@ -215,3 +216,95 @@ describe('valid-example', () => { | |||
}, | |||
); | |||
}); | |||
|
|||
describe('valid-schema-example', () => { |
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.
Looks like we've split this one rule into two but have left them both in the old file? The file names match the rules so we should have:
- src/rulesets/oas3/tests/valid-openapi-example.ts
- src/rulesets/oas3/tests/valid-schema-example.ts
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.
If not in src/rulesets/oas3/ruleset.json
, then where I should put these 2 new rules? Thought oas3/ruleset.json
is the place as the two rules belong to oas3 validators, no?
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.
The valid-example.ts is when there was one rule. You are splitting it into two rules. So make two files, the ones above, and tadaaaa you’re done. No new ruleset, that’s a total misuse of ruleset a.
1 rule becomes 2 rules.
@@ -0,0 +1,17 @@ | |||
{ |
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've never seen a rule specific ruleset before. What is in the intention? Is this for testing?
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.
It's to move out this rule from oas3 rules
. My understanding is that spectral
can also validate jsons that are not oas2
/oas3
. We could also move this rule to some other not oas2/oas3
ruleset, but haven't found one (is there one existing?)
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.
functions are generic and can be used anywhere. We have schemaPath
as a generic function, and this is the basis of the valid example rules.
The rules are different for oas2 and oas3 because the specification structures are different.
So we reuse the schemaPath function, but do not reuse the valid example stuff because we cannot.
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.
And that's why I have moved valid-example
out of ruleset.json
for oas3
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.
Please remove this new file.
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, if I remove it, where do I put valid-example
? I'm kinda confused. I still have to test valid-example
rule..
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 can move already existing tests from src/rulesets/oas3/__tests__/valid-example.ts
to src/rulesets/oas2/__tests__/valid-example.ts
, since valid-example
rule didn't change for oas2
. So this way src/rulesets/oas3/__tests__/valid-example.ts
and src/rulesets/oas3/valid-example-ruleset.json
are removed. Would that be OK?
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.
So after seeing the talk about how many different types of param/header/schema example there is, maybe you could use that to create some tests which highlight each type of parameter and property example we looked at, then make sure this change works with each of those?
The ruleset you added is not the way to go about DRYing things, although I do understand the urge. Mixing up OAS2 and OAS3 is also not the way to go, as the two specs are different.
Seeing as you have split the valid-example
rule in two, and the describe tests split in two, I was expecting you to just split the valid-example
file in two.
instead of having describe('valid-schema-example', () => {
inside src/rulesets/oas3/__tests__/valid-example.ts
the convention is to match the test file with whatever you are describing, so if you are gonna have two different rules have two different test files.
Is that all starting to make sense?
- fix the example of importing rules - add repl.it examples - move programmatic api docs to separate file - clean up the readme a bit and update outputs
Bumps [@types/node](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/node) from 12.0.3 to 12.0.7. - [Release notes](https://github.com/DefinitelyTyped/DefinitelyTyped/releases) - [Commits](https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/node) Signed-off-by: dependabot-preview[bot] <support@dependabot.com>
Hi @phil. This is WIP, but I wanted to show you what's been done so far. Please mind that external examples ( |
@@ -0,0 +1,37 @@ | |||
import * as AJV from 'ajv'; |
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 don’t understand what this file is, could you give it a quick explanation?
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.
Sure. @philsturgeon, this file is used by valid-type-example
rule:
spectral/src/rulesets/oas3/ruleset.json
Line 2432 in 06fcb9e
"function": "exampleType" |
This rule takes care of the cases where we have:
type: some_type
example: some example
So where the shape of an example is defined by .type
, not by schema
.
…es (#251) * fix(pattern.ts): enable use of regex flags for pattern in ruleset files #242 * Update src/functions/pattern.ts Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com> * Update src/functions/pattern.ts Co-Authored-By: Jakub Rożek <P0lip@users.noreply.github.com> * test(pattern.test.ts): testing exception on invalid flag in string regex #242 * test(pattern.ts): use a different assertion
Work is now being done in #472 |
Please check if the PR fulfills these requirements
What kind of change does this PR introduce?
Bug fix.
What is the current behavior?
#223
Does this PR introduce a breaking change?
No.