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

Adds a model-level validation capability to our validation library #49543

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

bl-nero
Copy link
Contributor

@bl-nero bl-nero commented Nov 28, 2024

This PR adds a new mechanism for validating the form model at the level of a component that owns the form, as opposed to every form control separately. This is required for the further role editor work, which requires errors to be visible on multiple levels of the hierarchy.

For more context (particularly understanding the use of runRules), see the upcoming PR #49546, which makes use of these utilities.

Requires https://github.com/gravitational/teleport.e/pull/5591
Followed up by #49544
Followed up by https://github.com/gravitational/teleport.e/pull/5592 (cleanup)
Contributes to #46612

Adds a model-level validation capability to our validation library.
@bl-nero bl-nero added the no-changelog Indicates that a PR does not require a changelog entry label Nov 28, 2024
@bl-nero bl-nero marked this pull request as ready for review November 28, 2024 15:15
@github-actions github-actions bot requested review from avatus and gzdunek November 28, 2024 15:16
* fewer items in this list than the labels, the corresponding items will be
* treated as valid.
*/
items?: ValidationResult[];
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if i received this field in some result i would have no idea what it meant. Based on the comment, should this be validItems or ... unchecked items? (based on label number? im confused)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@avatus How about itemResults?

Copy link
Contributor

Choose a reason for hiding this comment

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

what is the context of the result? valid or not valid? checked vs unchecked? thats the part I'm getting lost with (forgive my brain)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I see what you mean. The results are "valid or not valid". In other words, these are per-item validation results that enable us to show a validity state and message for each item of the string list separately.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ahhhh, that makes sense. with that context, items probably makes most sense. I think maybe even results might be better but, I'll leave it up to you if you want to make any changes

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for results.

Comment on lines 121 to 123
rule={precomputed(
validationResult.items?.[i] ?? { valid: true }
)}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you help me understand how this is interacting with default rule? The default rule just returns valid: true, why would we need to do extra validation here based on item length? could we just return whatever the default rule returns here instead of valid: true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This { valid: true } object is a per-item object, as opposed to the one returned from the default rule, which applies to all items.

Comment on lines +52 to +56
/**
* @deprecated For temporary Enterprise compatibility only. Use {@link state}
* instead.
*/
valid = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

is this something to just keep the builds from breaking? I'd add a TODO here to ensure it gets updated after, otherwise this could get messy. I wonder if it'd be better to use a getter here to always return the value of state instead of a separate property. like this pseudo-ish code

  get valid() {
    return this.state.valid;
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The cleanup PR #49550 removes it, but I'll follow your advice anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah you're right. I think we can ignore this comment then

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I wanted to suggest using a getter as well. Accessing state.valid in the callsites look a bit off, like accessing an internal property of Validator. I believe having getters for valid and validating would provide a nicer API.

But I will leave it up to you.

* fewer items in this list than the labels, the corresponding items will be
* treated as valid.
*/
items?: ValidationResult[];
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 for results.

web/packages/shared/components/Validation/Validation.jsx Outdated Show resolved Hide resolved
Comment on lines +52 to +56
/**
* @deprecated For temporary Enterprise compatibility only. Use {@link state}
* instead.
*/
valid = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I wanted to suggest using a getter as well. Accessing state.valid in the callsites look a bit off, like accessing an internal property of Validator. I believe having getters for valid and validating would provide a nicer API.

But I will leave it up to you.

web/packages/shared/components/Validation/Validation.tsx Outdated Show resolved Hide resolved
web/packages/shared/components/Validation/Validation.tsx Outdated Show resolved Hide resolved
@bl-nero bl-nero requested a review from gzdunek December 3, 2024 15:28
@bl-nero bl-nero enabled auto-merge December 4, 2024 14:42
@bl-nero bl-nero added this pull request to the merge queue Dec 4, 2024
Merged via the queue into master with commit 3b1f2b5 Dec 4, 2024
40 checks passed
@bl-nero bl-nero deleted the bl-nero/validation branch December 4, 2024 15:07
@public-teleport-github-review-bot

@bl-nero See the table below for backport results.

Branch Result
branch/v15 Failed
branch/v16 Failed
branch/v17 Create PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants