Skip to content
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

[Versioned HTTP] Add response runtime and type-level validation #153011

Merged

Conversation

jloleysens
Copy link
Contributor

@jloleysens jloleysens commented Mar 9, 2023

Summary

This PR restructures the validation object on the .addVersion method by:

  1. Adding an in object for the body, params and query validations
  2. Adding out so that we can have both runtime and TS type checking our responses

To reviewers: easiest way to interpret these changes is to read the example.ts file.

@@ -27,14 +27,18 @@ export interface KibanaSuccessResponseFactory {
* Status code: `200`.
* @param options - {@link HttpResponseOptions} configures HTTP response body & headers.
*/
ok(options?: HttpResponseOptions): IKibanaResponse;
ok<T extends HttpResponsePayload = HttpResponsePayload>(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for properly type checking responses. Assuming we only want this for ok. But I can easily see this also being checkable for non-OK responses like 400.

Comment on lines 121 to 126
interface FullValidationConfig<P, Q, B, R> {
/** Validation to run against route inputs: params, query and body */
in?: InValidation<P, Q, B>;
/** Validation to run against route output */
out?: Type<R>;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding something like this gives us a few new capabilities:

  1. TS checking enforcing shape of return type
  2. Runtime checks against the shape of return type
  3. A clearer path to true HTTP API introspection since we have the types for the return value of the endpoint...

@jloleysens jloleysens changed the title [Versioned routes] Add response runtime and type-level validation [Versioned HTTP] Add response runtime and type-level validation Mar 9, 2023
Copy link
Contributor

@TinaHeiligers TinaHeiligers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where we're heading here. I left a few nits as naming conventions.

@jloleysens jloleysens marked this pull request as ready for review March 10, 2023 12:44
@jloleysens jloleysens requested review from a team as code owners March 10, 2023 12:44
@jloleysens jloleysens added Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc release_note:skip Skip the PR/issue when compiling release notes v8.8.0 labels Mar 10, 2023
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-core (Team:Core)

@jloleysens jloleysens enabled auto-merge (squash) March 10, 2023 15:13
Copy link
Contributor

@ymao1 ymao1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Response Ops changes LGTM

Copy link
Contributor

@TinaHeiligers TinaHeiligers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

opts: AddVersionOpts<P, Q, B>,
handler: RequestHandler<P, Q, B, Ctx>
addVersion<P, Q, B, R>(
options: AddVersionOpts<P, Q, B, R>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

* interface Foo { bar?: string }
* const foo: WithRequiredProperty<Foo, 'bar'> = { bar: 'baz' }
*/
export type WithRequiredProperty<Type, Key extends keyof Type> = Omit<Type, Key> & {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to add a test if the type starts getting used a lot.

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] Rules, Alerts and Exceptions ResponseOps Cypress Tests on Security Solution / Rules selection should correctly update the selection label when rules are bulk selected and then unselected via the table select all checkbox

Metrics [docs]

Unknown metric groups

API count

id before after diff
@kbn/utility-types 35 36 +1

ESLint disabled line counts

id before after diff
securitySolution 434 437 +3

Total ESLint disabled count

id before after diff
securitySolution 514 517 +3

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @rudolf

Copy link
Contributor

@davismcphee davismcphee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

response_formatter.ts explicit return type change LGTM!

@jloleysens jloleysens merged commit 3c7bf58 into elastic:main Mar 13, 2023
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Mar 13, 2023
@jloleysens jloleysens deleted the add-response-validation-to-versioned-router branch March 23, 2023 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes Team:Core Core services & architecture: plugins, logging, config, saved objects, http, ES client, i18n, etc v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants