github.com/kaptinlin/jsonschema@v0.4.6/tests/properties_test.go (about) 1 package tests 2 3 import ( 4 "testing" 5 ) 6 7 // func TestSchemaProperties(t *testing.T) { 8 // testCases := []struct { 9 // name string 10 // schemaJSON string 11 // expectedSchema jsonschema.Schema 12 // }{ 13 // { 14 // name: "Properties with basic types", 15 // schemaJSON: `{ 16 // "$schema": "https://json-schema.org/draft/2020-12/schema", 17 // "properties": { 18 // "foo": {"type": "integer"}, 19 // "bar": {"type": "string"} 20 // } 21 // }`, 22 // expectedSchema: jsonschema.Schema{ 23 // Schema: "https://json-schema.org/draft/2020-12/schema", 24 // Properties: &jsonschema.SchemaMap{ 25 // "foo": &jsonschema.Schema{Types: jsonschema.SchemaTypes{"integer"}}, 26 // "bar": &jsonschema.Schema{Types: jsonschema.SchemaTypes{"string"}}, 27 // }, 28 // }, 29 // }, 30 // // { 31 // // name: "Properties, patternProperties, additionalProperties interaction", 32 // // schemaJSON: `{ 33 // // "$schema": "https://json-schema.org/draft/2020-12/schema", 34 // // "properties": { 35 // // "foo": {"type": "array", "maxItems": 3}, 36 // // "bar": {"type": "array"} 37 // // }, 38 // // "patternProperties": {"f.o": {"minItems": 2}}, 39 // // "additionalProperties": {"type": "integer"} 40 // // }`, 41 // // expectedSchema: jsonschema.Schema{ 42 // // Schema: "https://json-schema.org/draft/2020-12/schema", 43 // // Properties: &jsonschema.Properties{ 44 // // "foo": &jsonschema.Schema{ 45 // // Types: jsonschema.SchemaTypes{"array"}, 46 // // MaxItems: ptrUint64(3), 47 // // }, 48 // // "bar": &jsonschema.Schema{ 49 // // Types: jsonschema.SchemaTypes{"array"}, 50 // // }, 51 // // }, 52 // // PatternProperties: &jsonschema.PatternProperties{ 53 // // "f.o": &jsonschema.Schema{ 54 // // MinItems: ptrUint64(2), 55 // // }, 56 // // }, 57 // // AdditionalProperties: &jsonschema.Schema{ 58 // // Types: jsonschema.SchemaTypes{"integer"}, 59 // // }, 60 // // }, 61 // // }, 62 // } 63 64 // for _, tc := range testCases { 65 // t.Run(tc.name, func(t *testing.T) { 66 // var schema jsonschema.Schema 67 // err := json.Unmarshal([]byte(tc.schemaJSON), &schema) 68 // require.NoError(t, err, "Unmarshalling failed unexpectedly") 69 70 // // Assert the schema properties 71 // assert.Equal(t, tc.expectedSchema.Properties, schema.Properties) 72 73 // // Now test marshaling back to JSON 74 // marshaledJSON, err := json.Marshal(schema) 75 // require.NoError(t, err, "Marshalling failed unexpectedly") 76 77 // // Unmarshal marshaled JSON to verify it matches the original schema object 78 // var reUnmarshaledSchema jsonschema.Schema 79 // err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema) 80 // require.NoError(t, err, "Unmarshalling the marshaled JSON failed") 81 // assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original") 82 83 // // Check if the marshaled JSON matches the original JSON input 84 // assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON") 85 // }) 86 // } 87 // } 88 89 // TestPropertiesForTestSuite executes the properties validation tests for Schema Test Suite. 90 func TestPropertiesForTestSuite(t *testing.T) { 91 testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/properties.json") 92 }