This Commented CUE demonstrates how to constrain fields to contain string representations of integer values.
example.cue
package example
import "strconv"
// Constrain s1 and s2 to be the string
// interpolation of the result of converting
// their respective values to integers.
s1?: "\(strconv.Atoi(s1))"
s2?: "\(strconv.Atoi(s2))"
data.yml
# s1 is a valid integer
s1: "137"
# s2 is not a valid integer
s2: "42.7"
TERMINAL
$ cue vet .:example data.yml
s2: invalid interpolation: error in call to strconv.Atoi: strconv.Atoi: parsing "42.7": invalid syntax:
./example.cue:9:6
./example.cue:9:9
The constraint enforces that the concrete values of s1
and s2
are equal to
the string
-to-int
-to-string
conversions of those same values. This process
must start and end with identical values, otherwise validation fails, as in the
case of s2
.
cue
’s error messages can, at times, be rather hard to interpret. This is an
area we are actively looking to improve. Please raise bug
reports with examples
you find hard to understand.Related content
- The
strconv
package in the CUE standard library