Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Package jsonschema provides json-schema compilation and validation.
- uri, uriref, uri-template(limited validation)
- json-pointer, relative-json-pointer
- regex, format
- int32, int64
- implements following contentEncoding (supports [user-defined](https://pkg.go.dev/github.com/santhosh-tekuri/jsonschema/v5/#example-package-UserDefinedContent))
- base64
- implements following contentMediaType (supports [user-defined](https://pkg.go.dev/github.com/santhosh-tekuri/jsonschema/v5/#example-package-UserDefinedContent))
Expand Down
65 changes: 65 additions & 0 deletions format.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package jsonschema

import (
"encoding/json"
"errors"
"math"
"math/big"
"net"
"net/mail"
"net/url"
Expand Down Expand Up @@ -37,6 +40,8 @@ var Formats = map[string]func(interface{}) bool{
"json-pointer": isJSONPointer,
"relative-json-pointer": isRelativeJSONPointer,
"uuid": isUUID,
"int32": isInt32, // OpenAPI
"int64": isInt64, // OpenAPI
}

// isDateTime tells whether given string is a valid date representation
Expand Down Expand Up @@ -565,3 +570,63 @@ func isUUID(v interface{}) bool {
}
return true
}

// isInt32 tells whether given integer is a valid 32-bit one
// as specified in OpenAPI 3.1.0.
//
// see https://github.com/OAI/OpenAPI-Specification/blob/3.1.0/versions/3.1.0.md#data-types, for details
func isInt32(v interface{}) bool {
var bv *big.Rat
switch v := v.(type) {
case json.Number:
// v.Int64() and big.Int.SetString fail with .0 fractionals, which is inconsistent with basic integer test
var ok bool
if bv, ok = new(big.Rat).SetString(v.String()); !ok {
return false
}
case int:
bv = new(big.Rat).SetInt64(int64(v))
case int32:
return true
case int64:
bv = new(big.Rat).SetInt64(v)
case float64:
if bv = new(big.Rat).SetFloat64(v); bv == nil {
return false
}
default:
return true
}
if !bv.IsInt() {
return false
}
bl := new(big.Rat)
return bv.Cmp(bl.SetInt64(math.MinInt32)) != -1 && bv.Cmp(bl.SetInt64(math.MaxInt32)) != 1
}

// isInt64 tells whether given integer is a valid 64-bit one
// as specified in OpenAPI 3.1.0.
//
// see https://github.com/OAI/OpenAPI-Specification/blob/3.1.0/versions/3.1.0.md#data-types, for details
func isInt64(v interface{}) bool {
var bv *big.Rat
switch v := v.(type) {
case json.Number:
// v.Int64() and big.Int.SetString fail with .0 fractionals, which is inconsistent with basic integer test
var ok bool
if bv, ok = new(big.Rat).SetString(v.String()); !ok {
return false
}
case float64:
if bv = new(big.Rat).SetFloat64(v); bv == nil {
return false
}
default:
return true
}
if !bv.IsInt() {
return false
}
bl := new(big.Rat)
return bv.Cmp(bl.SetInt64(math.MinInt64)) != -1 && bv.Cmp(bl.SetInt64(math.MaxInt64)) != 1
}
59 changes: 59 additions & 0 deletions format_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jsonschema

import (
"encoding/json"
"fmt"
"math"
"strings"
"testing"
)
Expand Down Expand Up @@ -353,3 +356,59 @@ func TestIsUUID(t *testing.T) {
}
}
}

func TestIsInt32(t *testing.T) {
tests := []struct {
val interface{}
valid bool
}{
{1, true},
{1.0, true},
{1.2, false},
{-0, true},
{math.MinInt32, true},
{int32(math.MinInt32), true},
{int64(math.MinInt64), false},
{math.NaN(), false},
{math.Inf(1), false},
{json.Number("1"), true},
{json.Number("1.0"), true},
{json.Number("-1.01"), false},
{json.Number(fmt.Sprint(math.MaxInt32)), true},
{json.Number(fmt.Sprint(math.MaxInt64)), false},
{"string", true},
}
for i, test := range tests {
if test.valid != isInt32(test.val) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.val, test.valid, !test.valid)
}
}
}

func TestIsInt64(t *testing.T) {
tests := []struct {
val interface{}
valid bool
}{
{1, true},
{1.0, true},
{1.2, false},
{0, true},
{int32(math.MinInt32), true},
{int64(math.MinInt64), true},
{math.NaN(), false},
{math.Inf(-1), false},
{json.Number("1"), true},
{json.Number("1.0"), true},
{json.Number("-1.01"), false},
{json.Number(fmt.Sprint(math.MaxInt32)), true},
{json.Number(fmt.Sprint(math.MaxInt64)), true},
{json.Number("9223372036854775808"), false},
{"string", true},
}
for i, test := range tests {
if test.valid != isInt64(test.val) {
t.Errorf("#%d: %q, valid %t, got valid %t", i, test.val, test.valid, !test.valid)
}
}
}