diff --git a/internal/validation/resource_test.go b/internal/validation/resource_test.go new file mode 100644 index 0000000..f370731 --- /dev/null +++ b/internal/validation/resource_test.go @@ -0,0 +1,38 @@ +package validation + +import ( + "encoding/json" + "testing" + + "github.com/santhosh-tekuri/jsonschema/v6" +) + +func TestResourceNumericConstraint(t *testing.T) { + value, err := decodeResource([]byte(`{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"integer","maximum":9223372036854775807}`)) + if err != nil { + t.Fatal(err) + } + if got := value.(map[string]any)["maximum"]; got != json.Number("9223372036854775807") { + t.Fatalf("constraint lost precision: %#v", got) + } + compiler := jsonschema.NewCompiler() + compiler.UseLoader(offlineLoader{}) + if err := compiler.AddResource("urn:schemas:test:integer", value); err != nil { + t.Fatal(err) + } + schema, err := compiler.Compile("urn:schemas:test:integer") + if err != nil { + t.Fatal(err) + } + if err := schema.Validate(int64(9223372036854775807)); err != nil { + t.Fatal(err) + } + if err := schema.Validate(json.Number("9223372036854775808")); err == nil { + t.Fatal("above-bound value accepted") + } + for _, data := range []string{`{"maximum":1} {}`, `maximum: 1`} { + if _, err := decodeResource([]byte(data)); err == nil { + t.Fatalf("invalid resource accepted: %s", data) + } + } +} diff --git a/internal/validation/validate.go b/internal/validation/validate.go index ab1e272..9ba4d3c 100644 --- a/internal/validation/validate.go +++ b/internal/validation/validate.go @@ -1,6 +1,7 @@ package validation import ( + "bytes" "encoding/json" "fmt" "io/fs" @@ -41,13 +42,9 @@ func Load(root fs.FS) (*Set, error) { if err != nil { return nil, fmt.Errorf("catalog.json: %w", err) } - // Decode through the shared profile as well, so duplicate JSON keys fail. if !json.Valid(data) { return nil, fmt.Errorf("catalog.json: expected JSON syntax") } - if _, err := Decode(data); err != nil { - return nil, fmt.Errorf("catalog.json: %w", err) - } set := &Set{schemas: make(map[string]*Schema), selections: make(map[string]string)} if err := json.Unmarshal(data, &set.Catalog); err != nil { return nil, fmt.Errorf("catalog.json: %w", err) @@ -98,7 +95,7 @@ func Load(root fs.FS) (*Set, error) { if !json.Valid(data) { return nil, fmt.Errorf("%s: expected JSON syntax", record.Path) } - value, err := Decode(data) + value, err := decodeResource(data) if err != nil { return nil, fmt.Errorf("%s: %w", record.Path, err) } @@ -151,6 +148,19 @@ func Load(root fs.FS) (*Set, error) { return set, nil } +// Schema resources are JSON, not operator configuration. Preserve exact numeric +// constraints independently of the domain loader's numeric binding policy. +func decodeResource(data []byte) (any, error) { + if !json.Valid(data) { + return nil, fmt.Errorf("expected one JSON value") + } + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.UseNumber() + var value any + err := decoder.Decode(&value) + return value, err +} + type offlineLoader struct{} func (offlineLoader) Load(url string) (any, error) {