From 989e7bb88955331f4e03a369278f3a3f7cc6e631 Mon Sep 17 00:00:00 2001 From: Christian Boitel Date: Mon, 1 Aug 2022 17:16:04 +0200 Subject: [PATCH] feat #482: add support for int32 and int64 integer formats --- openapi3/schema.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/openapi3/schema.go b/openapi3/schema.go index 4c8df1cba..bc64931de 100644 --- a/openapi3/schema.go +++ b/openapi3/schema.go @@ -24,6 +24,14 @@ const ( TypeNumber = "number" TypeObject = "object" TypeString = "string" + + // constants for integer formats + formatMinInt = float64(math.MinInt) + formatMaxInt = float64(math.MaxInt) + formatMinInt32 = float64(math.MinInt32) + formatMaxInt32 = float64(math.MaxInt32) + formatMinInt64 = float64(math.MinInt64) + formatMaxInt64 = float64(math.MaxInt64) ) var ( @@ -1039,6 +1047,35 @@ func (schema *Schema) visitJSONNumber(settings *schemaValidationSettings, value return schema.expectedType(settings, "number, integer") } + // formats + if schema.Format != "" { + formatMin := formatMinInt + formatMax := formatMaxInt + switch schema.Format { + case "int32": + formatMin = formatMinInt32 + formatMax = formatMaxInt32 + case "int64": + formatMin = formatMinInt64 + formatMax = formatMaxInt64 + } + if !(value >= formatMin) || !(value <= formatMax) { + if settings.failfast { + return errSchema + } + err := &SchemaError{ + Value: value, + Schema: schema, + SchemaField: "format", + Reason: fmt.Sprintf("number must be a %s", schema.Format), + } + if !settings.multiError { + return err + } + me = append(me, err) + } + } + // "exclusiveMinimum" if v := schema.ExclusiveMin; v && !(*schema.Min < value) { if settings.failfast {