-
-
Notifications
You must be signed in to change notification settings - Fork 247
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: custom functions #430
Conversation
2dfe157
to
27e844f
Compare
Part of the plan with Spectral v4.1 was to add loadRulesets and custom functions, but something I didn't emphasize enough was that this was meant to be used throughout Spectral itself. We have some rather confusing code for our internal rulesets, and loadRulesets, formats, etc is meant to make it a bit more simple. People should be able to include We should be able to have the current oas2 ruleset look like this:
Then oas2 and oas3 specific stuff can stay where it is. They can use the same functionDir because all the functions are the same for 2 and 3, but this hopefully means we can get rid of
This will involve a bunch of change in our tests. It would be ok to split this work into another issue and follow up PR, but it would still need to be done for the 4.1 launch, otherwise we are suggesting features we dont use, and carrying around this extra baggage instead of using our nice new functionality. :) |
Other cleanup, lets get rid of this:
They're the same thing anyway! We don't need to check the object to detect format, formats do that, and now functions are gonna be loaded through the ruleset right? |
One last thing, we are getting deprecation warnings in the CLI because we use this old stuff:
:D |
@philsturgeon I'm aware of deprecation warnings, but we don't have any alternative solutions just yet 🤔 We'd really need to get that |
482c37e
to
3e5ef49
Compare
The code should be pretty much ready. I will just add a few extra test cases and write docs, so hold on with the reviews yet. |
Docs added 😅 |
@XVincentX https://dev.azure.com/vncz/vncz/_build/results?buildId=1117 any clue why azure jobs get stuck at |
So begin the conflicts! |
4386e97
to
52779fc
Compare
a8ad526
to
9e2f56d
Compare
711c79d
to
35935b5
Compare
@@ -0,0 +1,39 @@ | |||
# Development |
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 feel like we should move the relevant bits of this over to contributing, and delete this file.
docs/guides/custom-functions.md
Outdated
function: "abc" | ||
``` | ||
|
||
Optionally, if you'd like to validate the data that it passed to abc function before the function gets executed, you can provide a JSON Schema. |
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.
Again, I'm not sure that this is what it should be doing?
We have a json schema rule that should be used to validate target data.
This JSON schema should describe the functionOptions
available in this function. Eventually we'll use it to render documentation for the ruleset and custom functions, and I guess we could also use it to validate the functionOptions
the end user passes in?
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 JSON schema should describe the functionOptions available in this function. Eventually we'll use it to render documentation for the ruleset and custom functions, and I guess we could also use it to validate the functionOptions the end user passes in?
Yeah, we validate the function options that are passed in.
src/spectral.ts
Outdated
} | ||
} | ||
} | ||
public async loadRuleset(...uris: string[]) { |
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 a very constrictive API - we'll never be able to change it or add any options without introducing a breaking change. I suggest something like this instead:
public async loadRulesets(uris: string[]) {
With this, we can easily add an optional opts
or other argument after uris in the future, as needed. I also pluralized the method, since it supports multiple.
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.
Yeah, you are right. I'll pull over the code I wrote herehttps://github.com//pull/460 it has a very similar APIs. I had to change it because of the reasons you mentioned.
|
||
## Inheritance | ||
|
||
Core functions can be overridden with custom rulesets, so if you'd like to make your own truthy go ahead. Custom functions are only available in the ruleset which defines them, so loading a foo in one ruleset will not clobber a foo in another ruleset. |
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 confusing. The truthy
function is defined further up, but I can still use it in my ruleset. Can I not use any of the functions defined in the oas2 ruleset in my own ruleset 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.
Can I not use any of the functions defined in the oas2 ruleset in my own ruleset file?
You can use it.
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'm not really sure how to explain it any better 🤔
As you could have all observed already, writing docs is not my biggest asset 😆
It's up to you all. |
I feel like this is ready for another pass. |
@@ -169,13 +168,12 @@ rules: | |||
|
|||
The example above will run the single rule that we enabled, since we passed `off` to disable all rules by default when extending the `spectral:oas2` ruleset. | |||
|
|||
### Disable Rules | |||
## Disabling rules |
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.
Note that this breaks the readme
We’re gonna look at docs in another PR
The docs you’ve written are excellent but need a little polish. I’ll take care of this in another PR on Monday, using the comments here to start and just giving it a general once over. |
Ah, I was just polishing a few bits up - will open a separate PR :). Awesome to get this merged, great job @P0lip ! |
* feat: implement module exports evaluator * feat: add functions and functionsDir to ruleset.schema.json * feat: resolve and load custom functions (wip) * chore: move mergeRules tests * feat: migrate formats merging code * feat: inject fn.name and polish paths resolving * feat: tweak ruleset validation * feat: merge fns * feat: migrate oas functions * build: add Rollup to the mix * feat: deprecate addFunctions * feat(cli): load rulesets * feat: default exports + extended ruleset functions merging * test: proxy fs calls * feat: add skipRule back * test: typings * fix(reader): proper custom functions path * fix(cli): restore previous rulesets errors * test(reader): relax then propety value check * fix: do not overuse mergeFunctions * chore: run merging code in jest only * build: generate karma fixtures * test: use karma fixtures * test(linter): redirect fs calls * test(linter): split to karma and linter * test: consolidate tests * feat: improve export evaluator + moar tests * feat: consume schema & add tests * docs: functions and basic development guide * docs: remove hassle-free phrase Co-Authored-By: Phil Sturgeon <phil@stoplight.io> * docs: improve docs * test(formats): dot in json path expression * docs: link IFunction * feat: allow function reading to fail * docs: vncz always needs to leave a commentCo-Authored-By: Vincenzo Chianese <vincenz.chianese@icloud.com> * style: separate all the commands * test: do not use deprecated methods in tests * docs: rulesets * chore: deprecate oas functions and rules * refactor: ruleset validation throws ValidationError * test: do not use deprecate methods #2 * docs: move custom functions to a separate doc * fix: update docs a bit * refactor: loadRuleset * docs: clarify function options
Closes #311
Checklist
Does this PR introduce a breaking change?
Here it is.
The PR is likely to introduce a breaking change, as I shuffled some methods around and perhaps removed a few.
There is still cleanup left to be done, mostly around the usage of deprecated methods in our code as well as tests.
I'm not really sure whether we are good with making breaking changes, as we are not planning to release 5.0 anytime soon, and the code is meant to be shipped as a part of 4.1, so I suspect I might need to revert a few changes and be more careful with the cleanup.
The code is ready to be reviewed, I covered all the cases that I could think of.
I'll write a word on the updates I had to make to tests as well as the rulesets functions.
A note on functions.
This is the final syntax I implemented:
There are a couple of fixtures and tests covering these, so you can take a look to get a grasp of that in action.
The syntax follows the other JS-based tooling such as Babel etc., therefore we should be aligned with others.
The object pattern felt very awkward in practice.
@philsturgeon
I believe the custom functions deserve their own document, thoughts?
Moreover, I'm planning to start a developer guide or something similar, as everything gets more complicated and you need to be wary of certain patterns.
I'm planning to do that in the scope of this PR, but beforehand, if you could at least take a look at the code and the functions concept I eventually went with, that would be great.