I’ve currently got an attribute definition like this:
"description": schema.StringAttribute{
Optional: true,
Validators: []validator.String{stringvalidator.LengthAtLeast(1)},
},
It’s Optional
because neither the API, nor the accompanying web UI require the user to submit a description.
Though the API permits an omitted description attribute in POST
it will return ""
(rather than null
) for this attribute in response to GET
. There is no distinction between null
and ""
in the API.
The “no empty strings allowed” validator exists to eliminate ambiguity about what we should commit to the state during Read()
: The user could not have set an empty string, so types.StringNull()
is the best choice when the API returns ""
.
It’s just occurred to me that a custom string type which considers null
and ""
to be semantically equal would give my users a little more flexibility: It would allow them to either omit the description
attribute, or to set an empty string.
Am I on the right track here? Any concerns about using a custom type in this way?
There are other attributes with similar behavior:
- integer attributes where
0
is a special “I don’t care” value returned by the API when the attribute was omitted - I currently forbid0
from the config via validation. - set attributes like
aliases
(set of strings) - I currently disallow empty sets via validation because the API doesn’t distinguish between[]
andnull
.