Skip to content

Commit d969493

Browse files
committed
Add more conventions around validation
There's a LOT more to be written, but it's a start.
1 parent e4ac06b commit d969493

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

contributors/devel/sig-architecture/api-conventions.md

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -579,11 +579,13 @@ selectors, annotations, data), as opposed to sets of subobjects.
579579
580580
#### Primitive types
581581
582+
Also read the section on validation, below.
583+
584+
When selecting fields, consider the following:
582585
* Look at similar fields in the API (e.g. ports, durations) and follow the
583586
conventions of existing fields.
584-
* Do not use enums. Use aliases for string instead (e.g. `NodeConditionType`).
585-
* All numeric fields should be bounds-checked, both for too-small or negative
586-
and for too-large.
587+
* Do not use numeric enums. Use aliases for string instead (e.g.
588+
`NodeConditionType`).
587589
* All public integer fields MUST use the Go `int32` or Go `int64` types, not
588590
`int` (which is ambiguously sized, depending on target platform). Internal
589591
types may use `int`.
@@ -1954,12 +1956,61 @@ the future, an HTTP/2 implementation will be exposed that deprecates SPDY.
19541956
## Validation
19551957

19561958
API objects are validated upon receipt by the apiserver.
1957-
Validation can be implemented in two ways: declaratively, using tags on the Go type definitions, or manually, by writing validation functions.
1958-
For all new APIs, declarative validation is the preferred approach for the validation rules it supports. For more information see the [declarative validation documentation](api_changes.md#declarative-validation). Validation errors are
1959-
flagged and returned to the caller in a `Failure` status with `reason` set to
1960-
`Invalid`. In order to facilitate consistent error messages, we ask that
1961-
validation logic adheres to the following guidelines whenever possible (though
1962-
exceptional cases will exist).
1959+
Validation can be implemented in two ways: declaratively, using tags on the Go
1960+
type definitions, or manually, by writing validation functions. For all new
1961+
APIs, declarative validation is the preferred approach for the validation rules
1962+
it supports. For more information see the
1963+
[declarative validation documentation](api_changes.md#declarative-validation).
1964+
Validation errors are flagged and returned to the caller in a `Failure` status,
1965+
usually with `reason` set to `Invalid`.
1966+
1967+
### Requiredness
1968+
1969+
All fields should be declared optional or required, and validation should
1970+
check the requiredness.
1971+
1972+
### Type-specific validation guidelines
1973+
1974+
#### String fields
1975+
1976+
Almost all string fields should be checked for format. Some format checks
1977+
include checking for length (e.g. "short-name", aka, "DNS Label" must be less
1978+
than 64 bytes).
1979+
1980+
All string fields should be checked for maximum length. If the field has a
1981+
minimum length, that should also be checked.
1982+
1983+
Never do case-insensitive comparisons in validation. This is a source of bugs
1984+
and failed uniqueness assumptions. If validation treats "abc" and "ABC" as
1985+
the same, then ALL consumers of that field must do the same, and it is very
1986+
easy to miss a case.
1987+
1988+
#### Numeric fields
1989+
1990+
All numeric fields should be bounds-checked, both for too-small values
1991+
(including negative) and for too-large values.
1992+
1993+
#### List fields
1994+
1995+
All list fields should be checked for maximum size. If the list has a minimum
1996+
size, that should also be checked.
1997+
1998+
All list fields should have their "listType" tag(s) set. Lists with set or map
1999+
semantics should be checked for uniqueness (declarative validation will do this
2000+
authomatically).
2001+
2002+
#### Map fields
2003+
2004+
All map fields should be checked for maximum size. If the map has a minimum
2005+
size, that should also be checked.
2006+
2007+
Map fields should have their keys validated, in addition to their values.
2008+
2009+
### Error messages
2010+
2011+
In order to facilitate consistent error messages, we ask that validation logic
2012+
adheres to the following guidelines whenever possible (though exceptional cases
2013+
will exist).
19632014

19642015
* Be as precise as possible.
19652016
* Telling users what they CAN do is more useful than telling them what they

0 commit comments

Comments
 (0)