github.com/kaptinlin/jsonschema@v0.4.6/tests/maximum_test.go (about) 1 package tests 2 3 import ( 4 "testing" 5 6 "github.com/goccy/go-json" 7 "github.com/kaptinlin/jsonschema" 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 // TestMaximumForTestSuite executes the maximum validation tests for Schema Test Suite. 13 func TestMaximumForTestSuite(t *testing.T) { 14 testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/maximum.json") 15 } 16 17 func TestSchemaWithMaximum(t *testing.T) { 18 testCases := []struct { 19 name string 20 schemaJSON string 21 expectedSchema jsonschema.Schema 22 }{ 23 { 24 name: "Maximum validation", 25 schemaJSON: `{ 26 "$schema": "https://json-schema.org/draft/2020-12/schema", 27 "maximum": 3.0 28 }`, 29 expectedSchema: jsonschema.Schema{ 30 Schema: "https://json-schema.org/draft/2020-12/schema", 31 Maximum: jsonschema.NewRat(3.0), 32 }, 33 }, 34 { 35 name: "Maximum validation with unsigned integer", 36 schemaJSON: `{ 37 "$schema": "https://json-schema.org/draft/2020-12/schema", 38 "maximum": 300 39 }`, 40 expectedSchema: jsonschema.Schema{ 41 Schema: "https://json-schema.org/draft/2020-12/schema", 42 Maximum: jsonschema.NewRat(300), 43 }, 44 }, 45 } 46 47 for _, tc := range testCases { 48 t.Run(tc.name, func(t *testing.T) { 49 var schema jsonschema.Schema 50 err := json.Unmarshal([]byte(tc.schemaJSON), &schema) 51 require.NoError(t, err, "Unmarshalling failed unexpectedly") 52 assert.Equal(t, tc.expectedSchema.ID, schema.ID) 53 assert.Equal(t, tc.expectedSchema.Schema, schema.Schema) 54 assert.Equal(t, tc.expectedSchema.Type, schema.Type) 55 56 // Now test marshaling back to JSON 57 marshaledJSON, err := json.Marshal(schema) 58 require.NoError(t, err, "Marshalling failed unexpectedly") 59 60 // Unmarshal marshaled JSON to verify it matches the original schema object 61 var reUnmarshaledSchema jsonschema.Schema 62 err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema) 63 require.NoError(t, err, "Unmarshalling the marshaled JSON failed") 64 assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original") 65 66 // Check if the marshaled JSON matches the original JSON input 67 assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON") 68 }) 69 } 70 }