Add tests for invalid numeric formats

This commit is contained in:
yongruilin
2026-01-15 06:43:07 +00:00
parent e4f87f1cd7
commit 4a457d97f4

View File

@@ -670,6 +670,62 @@ func TestValidateCustomResource(t *testing.T) {
},
},
},
{name: "numeric formats invalid",
schema: apiextensions.JSONSchemaProps{
Type: "object",
Properties: map[string]apiextensions.JSONSchemaProps{
"intThirtyTwo": {Type: "integer", Format: "int32"},
"intSixtyFour": {Type: "integer", Format: "int64"},
"floatThreeTwo": {Type: "number", Format: "float"},
"floatSixFour": {Type: "number", Format: "double"},
},
},
failingObjects: []failingObject{
{
object: map[string]interface{}{"intThirtyTwo": int64(math.MaxInt32 + 1)},
expectErrs: []string{
`<nil>: Invalid value: "": Checked value must be of type integer with format int32 in intThirtyTwo`,
},
},
{
object: map[string]interface{}{"intThirtyTwo": int64(math.MinInt32 - 1)},
expectErrs: []string{
`<nil>: Invalid value: "": Checked value must be of type integer with format int32 in intThirtyTwo`,
},
},
// int64 overflow is not possible with int64 input, but we can test it with float64
{
object: map[string]interface{}{"intSixtyFour": float64(math.MaxInt64) * 1.1},
expectErrs: []string{
`intSixtyFour: Invalid value: "float64": intSixtyFour in body must be of type int64: "float64"`,
`<nil>: Invalid value: "": Checked value must be of type integer with format int64 in intSixtyFour`,
},
},
{
object: map[string]interface{}{"intSixtyFour": float64(math.MinInt64) * 1.1},
expectErrs: []string{
`intSixtyFour: Invalid value: "float64": intSixtyFour in body must be of type int64: "float64"`,
`<nil>: Invalid value: "": Checked value must be of type integer with format int64 in intSixtyFour`,
},
},
{
object: map[string]interface{}{"floatThreeTwo": float64(math.MaxFloat32 * 1.1)},
expectErrs: []string{
`<nil>: Invalid value: "": Checked value must be of type number with format float in floatThreeTwo`,
},
},
{
object: map[string]interface{}{"floatThreeTwo": float64(-math.MaxFloat32 * 1.1)},
expectErrs: []string{
`<nil>: Invalid value: "": Checked value must be of type number with format float in floatThreeTwo`,
},
},
// double overflow (float64) is handled by JSON parsing, but we can try to pass a value that might trigger it if not parsed as such.
// However, standard JSON unmarshalling usually caps at float64 limits or errors out.
// The validator itself checks ranges. Since Go's float64 matches double, true overflow is hard to represent without a custom numeric type.
// We will skip explicit double overflow tests here as they often result in +Inf/-Inf which might be handled differently or parse errors.
},
},
}
for _, tt := range tests {
compatVersion := tt.compatVersion