Skip to content

Conversation

aaron-prindle
Copy link
Contributor

PR adds documentation around when to use declarative vs hand-Written validation and when to create a new declarative validation tag.

Related to KEP 5073:

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. area/developer-guide Issues or PRs related to the developer guide sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. labels Sep 22, 2025
@k8s-ci-robot k8s-ci-robot added sig/contributor-experience Categorizes an issue or PR as relevant to SIG Contributor Experience. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 22, 2025
…ion and when to create a new declarative validation tag
@dims
Copy link
Member

dims commented Sep 22, 2025

/assign @liggitt @soltysh

Copy link
Contributor

@soltysh soltysh left a comment

Choose a reason for hiding this comment

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

this lgtm, leaving tags to Jordan

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: aaron-prindle, soltysh

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 23, 2025
#### When to Use Declarative Validation vs Hand-Written Validation

**Use Declarative Validation when:**
- The validation rule has an existing stable declarative validation tag which can be used in a straightforward way. See the catalog of stable tags [here](https://kubernetes.io/docs/reference/using-api/declarative-validation/).
Copy link
Member

@liggitt liggitt Sep 23, 2025

Choose a reason for hiding this comment

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

looking at https://kubernetes.io/docs/reference/using-api/declarative-validation/#catalog, am I missing how to tell for each tag what its stability level is?

**Use Declarative Validation when:**
- The validation rule has an existing stable declarative validation tag which can be used in a straightforward way. See the catalog of stable tags [here](https://kubernetes.io/docs/reference/using-api/declarative-validation/).
- You are working on a new API or adding new fields to an existing API that already uses declarative validation.
- The validation rule is simple and linear (e.g., min/max values, string length, format checks).
Copy link
Member

Choose a reason for hiding this comment

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

what does "linear" mean here?

- You are working on a new API or adding new fields to an existing API that already uses declarative validation.
- The validation rule is simple and linear (e.g., min/max values, string length, format checks).
- The validation can be expressed as a property of the data schema.
- The validation pattern is reusable across multiple APIs.
Copy link
Member

Choose a reason for hiding this comment

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

What does this bit mean in the context of a single field in a single API? this seems more related to deciding whether or not to add a new tag (the section below).

The "The validation rule has an existing stable declarative validation tag" item above makes it seem like DV would only be considered for things already deemed common enough to have tags added

- The validation requires complex logic or conditional branching based on multiple fields.
- The validation needs to consider external state beyond the object itself.
- Custom, detailed error messages are required for different failure conditions.
- The validation logic is unique to a specific API and unlikely to be reused.
Copy link
Member

Choose a reason for hiding this comment

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

same question here... isn't this bit about whether or not to add a new tag?

4) Any tag we add should be something we are OK with people using in new APIs. (We may eventually add tags that capture legacy semantics which break this rule).
5) With rare and clear exceptions, tags should not be interdependent.
6) Tags should usually represent properties of the data (schema) and the API operation (e.g. update), rather than the act of validation.
7) Tags are written from the POV of an API client.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
7) Tags are written from the POV of an API client.
7) Tags are written from the point of view of an API client.

5) With rare and clear exceptions, tags should not be interdependent.
6) Tags should usually represent properties of the data (schema) and the API operation (e.g. update), rather than the act of validation.
7) Tags are written from the POV of an API client.
8) Tags should be as orthogonal as possible.
Copy link
Member

Choose a reason for hiding this comment

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

nit: could probably combine this with the tags should not be interdependent item


#### When to Use Declarative Validation vs Hand-Written Validation

**Use Declarative Validation when:**
Copy link
Member

Choose a reason for hiding this comment

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

is this an "all of these must be true" list or "any of these may be true"?

6) Tags should usually represent properties of the data (schema) and the API operation (e.g. update), rather than the act of validation.
7) Tags are written from the POV of an API client.
8) Tags should be as orthogonal as possible.
9) Tags should be linear and (with rare exceptions) not represent complex logic.
Copy link
Member

Choose a reason for hiding this comment

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

same question about what linear means


1) Any tag added should be as simple and intuitive as possible to read.
2) Any tag added should plausibly be applicable to many APIs.
3) Any tag added should represent our current understanding of the best way to express an idea.
Copy link
Member

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 understand what this is getting at ... is this saying we won't create new tags to capture validation patterns we no longer consider a good idea? Is that the same as "Any tag we add should be something we are OK with people using in new APIs"?

7) Tags are written from the POV of an API client.
8) Tags should be as orthogonal as possible.
9) Tags should be linear and (with rare exceptions) not represent complex logic.
10) Tags should not need to consider anything other than the object(s) in question and configured options (e.g. feature gates).
Copy link
Member

Choose a reason for hiding this comment

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

possible suggestion: using the same language above about "external state" might be clearer

Tags should not need to consider external state beyond the new object being validated, the existing object (for update operations), the operation / resource / subresource, and configured options (e.g. feature gates).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. area/developer-guide Issues or PRs related to the developer guide cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. sig/contributor-experience Categorizes an issue or PR as relevant to SIG Contributor Experience. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants