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

[ML] Transforms: Refactor validators and add unit tests. #173736

Merged
merged 14 commits into from
Jan 2, 2024

Conversation

walterra
Copy link
Contributor

@walterra walterra commented Dec 20, 2023

Summary

Part of #173301.

Splits up form validators and adds more unit tests.

Checklist

@walterra walterra self-assigned this Dec 20, 2023
@walterra walterra force-pushed the ml-transform-refactor-validators branch from 9d549df to 83d4d13 Compare December 20, 2023 15:16
@walterra walterra added :ml Feature:Transforms ML transforms v8.13.0 release_note:skip Skip the PR/issue when compiling release notes labels Dec 21, 2023
@walterra walterra marked this pull request as ready for review December 21, 2023 09:01
@walterra walterra requested a review from a team as a code owner December 21, 2023 09:01
@elasticmachine
Copy link
Contributor

Pinging @elastic/ml-ui (:ml)

);

export const transformSettingsNumberOfRetriesValidator: Validator = (value) =>
!(value + '').includes('.') &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be a part of a generic validator?

* @param value User input value.
*/
export const transformSettingsPageSearchSizeValidator: Validator = (value) =>
!(value + '').includes('.') &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be a part of a generic validator?


import type { Validator } from './types';

export const stringValidator: Validator = (value, isOptional = true) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

we already have a generic requiredValidator

Comment on lines 11 to 12
const transformRetentionPolicyMaxAgeValidator = (arg: string) =>
retentionPolicyMaxAgeValidator(arg).length === 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

I reckon we should not write unit tests for functions created in the test suites itself

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 0193145.

import type { ParsedDuration } from './types';

// only valid if value is up to 1 hour
export function isValidFrequency(arg: unknown): arg is ParsedDuration {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be useful to have a generic duration validator, e.g.

const getDurationValidator = ({  minDurationInMs: number; maxDurationInMs: number; allowedUnits: stiring[] })

* Doesn't allow floating intervals.
* @param value User input value.
*/
export function isContinuousModeDelay(value: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

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

we have a generic patternValidator

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like patternValidator lives in the ml plugin, not transform. We could at one point create a package for validators and share that among plugins, but I'm afraid it's not something I'm going to pick up in this PR 😅 .

Comment on lines 20 to 24
export const integerRangeMinus1To100Validator: Validator = (value) =>
!(value + '').includes('.') &&
numberValidator({ min: -1, max: 100, integerOnly: true })(+value) === null
? []
: [numberRangeMinus1To100NotValidErrorMessage];
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use a generic validator I mentioned before?
another important moment to consider, depending on the locale, a comma might be used as a decimal separator

);

export const integerAboveZeroValidator: Validator = (value) =>
!(value + '').includes('.') && numberValidator({ min: 1, integerOnly: true })(+value) === null
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use a generic validator and avoid creating a function for each specific case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I fully get the suggestion. This is making use of the generic validator numberValidator, it then sets this up as a wrapper for a specific use case required by the edit transform flyout to satisfy the spec of the request object to update a transform. Also note this isn't new code, all the validators have just been moved to individual files and a corresponding jest test file, the intent of this PR wasn't to change any logic but just to spread out the existing validators into separate files. Can you share more details about how you envision that generic validator?

Copy link
Contributor

@darnautov darnautov Jan 2, 2024

Choose a reason for hiding this comment

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

Why do you need the !(value + '').includes('.') part? The generic validator already checks for integers with integerOnly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have a test assertion in there that would fail if remove that part: expect(integerAboveZeroValidator('1.')).toEqual(['Value needs to be an integer above zero.']);. I guess it's because 1. technically passes Number.isInteger but we didn't want users to be able to enter 1..

}
);

export const integerRangeMinus1To100Validator: Validator = (value) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use a generic validator and avoid creating a function for each specific case?

@walterra
Copy link
Contributor Author

walterra commented Jan 2, 2024

@darnautov I addressed your comments and adjusted the test assertions. As mentioned in one of the comments, I'd like to avoid a larger refactor in this PR, the aim was just to split up larger files into files with 1 validator and corresponding test file. Please have another look.

@walterra
Copy link
Contributor Author

walterra commented Jan 2, 2024

@darnautov As discussed, I removed the checks for !(value + '').includes('.') in 49f1284

@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
transform 392 405 +13

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
transform 403.4KB 402.8KB -632.0B
Unknown metric groups

ESLint disabled line counts

id before after diff
transform 33 32 -1

Total ESLint disabled count

id before after diff
transform 37 36 -1

History

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

cc @walterra

Copy link
Contributor

@alvarezmelissa87 alvarezmelissa87 left a comment

Choose a reason for hiding this comment

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

LGTM ⚡

@walterra walterra merged commit 34935a2 into elastic:main Jan 2, 2024
19 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Jan 2, 2024
@walterra walterra deleted the ml-transform-refactor-validators branch January 2, 2024 18:18
jloleysens added a commit that referenced this pull request Jan 4, 2024
* main: (4129 commits)
  [Logs Explorer] Change the default link for "Discover" in the serverless nav (#173420)
  [Fleet] fix unhandled error in agent details when components are missing (#174152)
  [Obs UX] Unskip transaction duration alerts test (#174069)
  [Fleet] Fix keep policies up to date after package install (#174093)
  [Profiling] Stack traces embeddable (#173905)
  [main] Sync bundled packages with Package Storage (#174115)
  [SLO Form] Refactor to use kibana data view component (#173513)
  [Obs UX] Unskip APM Service Inventory Journey (#173510)
  [Obs UX] Unskip preview_chart_error_count test (#173624)
  [api-docs] 2024-01-03 Daily api_docs build (#174142)
  Update babel runtime helpers (#171745)
  Handle content stream errors in report pre-deletion (#173792)
  [Cloud Posture] [Quick wins] Enable edit DataView on the Misconfigurations Findings Table (#173870)
  [ftr] abort retry on invalid webdriver session (#174092)
  Upgrade openai to 4.24.1 (#173934)
  chore(NA): bump node into v20 (#173461)
  [Security Solution][Endpoint] Fix index name pattern in SentinelOne dev. script (#174105)
  fix versions.json
  [Obs AI Assistant] Add guardrails (#174060)
  [ML] Transforms: Refactor validators and add unit tests. (#173736)
  ...
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 Feature:Transforms ML transforms :ml release_note:skip Skip the PR/issue when compiling release notes v8.13.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants