Skip to content

Commit

Permalink
feat(try-it-out): display validation error messages (#8212)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredden authored Oct 14, 2022
1 parent 0a7b3d6 commit 130a1be
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
10 changes: 10 additions & 0 deletions src/core/components/operation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export default class Operation extends PureComponent {

let onChangeKey = [ path, method ] // Used to add values to _this_ operation ( indexed by path and method )

const validationErrors = specSelectors.validationErrors([path, method])

return (
<div className={deprecated ? "opblock opblock-deprecated" : isShown ? `opblock opblock-${method} is-open` : `opblock opblock-${method}`} id={escapeDeepLinkPath(isShownKey.join("-"))} >
<OperationSummary operationProps={operationProps} isShown={isShown} toggleShown={toggleShown} getComponent={getComponent} authActions={authActions} authSelectors={authSelectors} specPath={specPath} />
Expand Down Expand Up @@ -189,6 +191,14 @@ export default class Operation extends PureComponent {
</div> : null
}

{ !tryItOutEnabled || !allowTryItOut || validationErrors.length <= 0 ? null : <div className="validation-errors errors-wrapper">
Please correct the following validation errors and try again.
<ul>
{ validationErrors.map((error, index) => <li key={index}> { error } </li>) }
</ul>
</div>
}

<div className={(!tryItOutEnabled || !response || !allowTryItOut) ? "execute-wrapper" : "btn-group"}>
{ !tryItOutEnabled || !allowTryItOut ? null :

Expand Down
12 changes: 8 additions & 4 deletions src/core/plugins/spec/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,23 @@ export const canExecuteScheme = ( state, path, method ) => {
return ["http", "https"].indexOf(operationScheme(state, path, method)) > -1
}

export const validateBeforeExecute = ( state, pathMethod ) => {
export const validationErrors = (state, pathMethod) => {
pathMethod = pathMethod || []
let paramValues = state.getIn(["meta", "paths", ...pathMethod, "parameters"], fromJS([]))
let isValid = true
const result = []

paramValues.forEach( (p) => {
let errors = p.get("errors")
if ( errors && errors.count() ) {
isValid = false
errors.forEach( e => result.push(e))
}
})

return isValid
return result
}

export const validateBeforeExecute = (state, pathMethod) => {
return validationErrors(state, pathMethod).length === 0
}

export const getOAS3RequiredRequestBodyContentType = (state, pathMethod) => {
Expand Down
18 changes: 18 additions & 0 deletions test/e2e-cypress/tests/features/multiple-examples-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,24 @@ describe("OpenAPI 3.0 Multiple Examples - core features", () => {
summary: "A wonderful kitten's info",
},
})
it("should display an error message when input validation fails", () => {
cy.visit("/?url=/documents/features/multiple-examples-core.openapi.yaml")
// Expand the operation
.get("#operations-default-post_Object")
.click()
// Switch to Try-It-Out
.get(".try-out__btn")
.click()
// Set an invalid value
.get(".parameters-container > div > table > tbody > tr > td.parameters-col_description > div:nth-child(2) > textarea")
.type("{{{{ [[[[ <<<< invalid JSON here.")
// Execute the operation
.get(".execute")
.click()
// Verify that an error is shown
.get(".validation-errors")
.contains("Parameter string value must be valid JSON")
})
})
describe("in a Response", () => {
ResponsePrimitiveTestCases({
Expand Down

0 comments on commit 130a1be

Please sign in to comment.