-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Improve validate() function for plugin options
#1323
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
Conversation
validate() function for plugin optionsvalidate() function for plugin options
validate() function for plugin optionsvalidate() function for plugin options
antocuni
left a comment
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.
+1 for moving the validation logic to plugin.ts and have a robust function which all the plugins can use.
-0 for making it overly complicated w.r.t. types.
If I understand correctly, a good chunk of the logic is needed to make the typescript compiler happy. My question is: is this extra complexity worth it?
Also, I can see how this works if you have a fixed set of options: you just pass all the possible values. But what if I want to specify a generic string?
Or a number?
An integer?
An integer between -4 and 102?
Note that I'm not saying that you should implement all the logic in this PR, but it's probably a good idea to plan ahead and think what are the functionalities that we want to support, because it might change the way we design the validation logic.
It also reduces complexity in unit tests - once the type system guarantees that checkedConfigOption({ possible_values: [true, false, 'auto'], default: 'BANANAS' });other than the integration tests that involve those individual attributes, which may or may not consistently handle 'invalid' options themselves.
This is a great thought, and not something I'd considered... let me muse on it and see if there's a straightforward line to something that would allow that. |
|
I've got a promising start an a change where a user could pass their own validation function. Needs some more API thoughts but the bones are there. Reverting this to draft for now until it's ready... probably will want a re-review when that's complete. |
|
@antocuni this is ready for a re-review, when you have a chance.
One can imagine adding more helper methods down the road - ~~ |
29f7ac2 to
bed4d8f
Compare
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
bed4d8f to
3fa082d
Compare
| * if not, throw an error explaining the bad value. | ||
| * This is the most generic validation function; other validation functions for common situations follow | ||
| * @param options.config - The (extended) AppConfig object from py-config | ||
| * @param {string} options.name - The name of the key in py-config to be checked |
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 @param {string} options.name the correct way of specifying params in the docs?
I ask since -- in other places, for example: @param filePath is written i.e. without the type. And name of the variable is written first. The above example I gave is present in fetchJSPlugin function in main.ts
Not sure what the best standards are, but wanted to highlight regardless...
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.
Thanks for calling it out - I think the syntax here is correct? I'm not a JSDoc expert - I'm following the @param documentation, specifically the Documenting a parameter's properties section about adding types and a parameter's properties.
pyscriptjs/src/plugin.ts
Outdated
| } | ||
|
|
||
| // Members of py-config in plug that we want to validate must be one of these types | ||
| type BaseConfigObject = string | boolean | number; |
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.
Do you want to allow undefined here perhaps?
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 think we want to - an undefined value coming from py-config is how we signal that the plugin should supply a default value:
So if a plugin wants to identify whether the value was left undefined in the config, they should set a sentinel value as the default and look for that at runtime.
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 only ask since you typecast it explicitly here: https://github.com/JeffersGlass/pyscript/blob/3fa082df26f1a1d702779e1ac93a875426e71313/pyscriptjs/src/plugin.ts#L298
and check for undefined in the very next line.
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.
Ahhh I see now - sorry was writing the merge as you posted this.
madhur-tandon
left a comment
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.
Tests are Great! Thanks @JeffersGlass
This improves the previous
validate()function in pyterminal.ts to be more flexible in how it handles potential values and default values. Previously, configuration options could only be booleans or strings, and the default had to be a string.Now, the options can be any of the non-array types (string, boolean, number or Object) that could be present in the TOML/JSON from py-config, any of them can be the default,
and the type system will catch whether the listed default value is indeed part of the array.I've moved
validate()- now renamed tovalidateConfigParameter()- from pyterminal.ts to plugin.ts, but only used it to improve pyterminal.ts for now to make it easier to see what's changed. We can use it to validate parameters for the other plugins in follow-up PRs. I've also addedvalidateConfigParameterFromArray(), which handles the (currently) common situation where the valid values are drawn from a small list known ahead of time.This could probably now use a test or two, hence the WIP.Tests written; this is ready for review.Following Antonio's thoughts about broadening what this could do, I'm reverting this to draft for now.This is ready for review again.I also want to use this functionality in #1317 (xterm.js), so I'm prioritizing this at this moment.