-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[ML] Transforms: Refactor validators and add unit tests. #173736
Conversation
9d549df
to
83d4d13
Compare
Pinging @elastic/ml-ui (:ml) |
x-pack/plugins/transform/public/app/common/validators/frequency_validator.test.ts
Outdated
Show resolved
Hide resolved
x-pack/plugins/transform/public/app/common/validators/integer_range_10_to_10000_validator.ts
Outdated
Show resolved
Hide resolved
); | ||
|
||
export const transformSettingsNumberOfRetriesValidator: Validator = (value) => | ||
!(value + '').includes('.') && |
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.
Can this be a part of a generic validator?
* @param value User input value. | ||
*/ | ||
export const transformSettingsPageSearchSizeValidator: Validator = (value) => | ||
!(value + '').includes('.') && |
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.
Can this be a part of a generic validator?
|
||
import type { Validator } from './types'; | ||
|
||
export const stringValidator: Validator = (value, isOptional = true) => { |
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.
we already have a generic requiredValidator
const transformRetentionPolicyMaxAgeValidator = (arg: string) => | ||
retentionPolicyMaxAgeValidator(arg).length === 0; |
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 reckon we should not write unit tests for functions created in the test suites itself
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.
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 { |
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.
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 { |
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.
we have a generic patternValidator
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.
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 😅 .
export const integerRangeMinus1To100Validator: Validator = (value) => | ||
!(value + '').includes('.') && | ||
numberValidator({ min: -1, max: 100, integerOnly: true })(+value) === null | ||
? [] | ||
: [numberRangeMinus1To100NotValidErrorMessage]; |
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.
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 |
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.
Can we use a generic validator and avoid creating a function for each specific case?
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'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?
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.
Why do you need the !(value + '').includes('.')
part? The generic validator already checks for integers with integerOnly
.
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.
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) => |
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.
Can we use a generic validator and avoid creating a function for each specific case?
@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. |
…a/kibana into ml-transform-refactor-validators
@darnautov As discussed, I removed the checks for |
💚 Build Succeeded
Metrics [docs]Module Count
Async chunks
Unknown metric groupsESLint disabled line counts
Total ESLint disabled count
History
To update your PR or re-run it, just comment with: cc @walterra |
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.
LGTM ⚡
* 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) ...
Summary
Part of #173301.
Splits up form validators and adds more unit tests.
Checklist