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: Validate inputs that cannot be used together #160
Conversation
Could you please explain how this code exactly works? Frankly I'm not really a JS expert and reading this gives me head scratching. |
I've provided an explanation below, let me know if there's still any confusion. I'll also add a different version that may be easier to understand, which I can use instead: Edit: Fixed uniqueInputSets.forEach((inputSet) => {
/**
* Given example object of:
* const example = {
* "key1": "value1",
* "key2": "value2"
* }
*
* Object.values(example) returns ["value1", "value2"]
* Object.keys(example) returns ["key1", "key2"]
*
* (For each uniqueInputSet)
* Get the values
* Guaranteed to be either the user input or '' (https://github.com/actions/toolkit/blob/c4ae214c261c0d684c6bcfe6632b773b82482bad/packages/core/src/core.ts#L130)
*
* These values are passed to the reduce method which takes a reducer(callback) and a starting value
* *Official Reduce Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
* The reducer is called for each item in the array with the below inputs
* previousValue(Named occurrences in this case)
* This value is the value that was returned by the callback the last time it was called
* If it's the first item in the array, the then starting value is used(In this case 0)
* currentValue(Named val in this case)
* This is the value of the current element in the array
*
* The reducer below checks if the user provided input by checking if the value is not ''
* If the input was provided by a user, then we increment and return the occurrences variable
* which will then be passed into the callback for the next item in the array
*
*/
const occurrences = Object.values(inputSet).reduce((occurrences, val) => {
if (val !== '') {
occurrences++
}
return occurrences
},0)
if(occurrences > 1) {
throw new Error(`The following inputs cannot be used together: ${Object.keys(inputSet).join(", ")}`)
}
}) Alternate version which is probably the better option: uniqueInputSets.forEach((inputSet) => {
const inputs = Object.values(inputSet)
const providedInputs = inputs.filter(input => input !== '')
if (providedInputs.length > 1) {
throw new Error(`The following inputs cannot be used together: ${Object.keys(inputSet).join(", ")}`)
}
}) |
Much nicer, thanks. Yet I'm not sure about the second object. Workflow conclusion input has a default value set in action metadata yaml and it does not really collide with run ID, it is just not evaluated when run ID is provided. |
Ah I see, I'll take a look in the AM at cleaning that up |
a1d704e
to
8a84e08
Compare
I've removed the second block |
Thanks |
* feat: Validate inputs that cannot be used together * Fix reduce callback * Simplify inputSet validation
No description provided.