You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/book/src/faq.md
+30Lines changed: 30 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -127,6 +127,36 @@ Your CRDs are generated using [controller-gen][controller-gen]. By using the opt
127
127
128
128
You can review the design of your APIs and see if it has not more specs than should be by hurting single responsibility principle for example. So that you might to re-design them.
129
129
130
+
## How can I validate and parse fields in CRDs effectively?
131
+
132
+
To enhance user experience, it is recommended to use [OpenAPI v3 schema](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.0.md#schemaObject) validation when writing your CRDs. However, this approach can sometimes require an additional parsing step.
133
+
For example, consider this code
134
+
```go
135
+
type StructName struct {
136
+
// +kubebuilder:validation:Format=date-time
137
+
TimeField string`json:"timeField,omitempty"`
138
+
}
139
+
```
140
+
141
+
### What happens in this scenario?
142
+
143
+
- Users will receive an error notification from the Kubernetes API if they attempt to create a CRD with an invalid timeField value.
144
+
- On the developer side, the string value needs to be parsed manually before use.
145
+
146
+
### Is there a better approach?
147
+
148
+
To provide both a better user experience and a streamlined developer experience, it is advisable to use predefined types like [`metav1.Time`](https://pkg.go.dev/k8s.io/[email protected]/pkg/apis/meta/v1#Time)
149
+
For example, consider this code
150
+
```go
151
+
type StructName struct {
152
+
TimeField metav1.Time`json:"timeField,omitempty"`
153
+
}
154
+
```
155
+
156
+
### What happens in this scenario?
157
+
158
+
- Users still receive error notifications from the Kubernetes API for invalid `timeField` values.
159
+
- Developers can directly use the parsed TimeField in their code without additional parsing, reducing errors and improving efficiency.
0 commit comments