github.com/kaptinlin/jsonschema@v0.4.6/tests/enum_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // TestEnumForTestSuite executes the enum validation tests for Schema Test Suite.
     8  func TestEnumForTestSuite(t *testing.T) {
     9  	testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/enum.json")
    10  }
    11  
    12  // func TestSchemaWithEnum(t *testing.T) {
    13  // 	testCases := []struct {
    14  // 		name           string
    15  // 		schemaJSON     string
    16  // 		expectedSchema jsonschema.Schema
    17  // 	}{
    18  // 		{
    19  // 			name: "simple enum validation",
    20  // 			schemaJSON: `{
    21  //                 "$schema": "https://json-schema.org/draft/2020-12/schema",
    22  //                 "enum": [1, 2, 3]
    23  //             }`,
    24  // 			expectedSchema: jsonschema.Schema{
    25  // 				Schema: "https://json-schema.org/draft/2020-12/schema",
    26  // 				Enum:   []interface{}{1, 2, 3},
    27  // 			},
    28  // 		},
    29  // 		{
    30  // 			name: "heterogeneous enum validation",
    31  // 			schemaJSON: `{
    32  //                 "$schema": "https://json-schema.org/draft/2020-12/schema",
    33  //                 "enum": [6, "foo", [], true, {"foo":12}]
    34  //             }`,
    35  // 			expectedSchema: jsonschema.Schema{
    36  // 				Schema: "https://json-schema.org/draft/2020-12/schema",
    37  // 				Enum:   []interface{}{6, "foo", []interface{}{}, true, map[string]interface{}{"foo": 12}},
    38  // 			},
    39  // 		},
    40  // 		{
    41  // 			name: "heterogeneous enum-with-null validation",
    42  // 			schemaJSON: `{
    43  //                 "$schema": "https://json-schema.org/draft/2020-12/schema",
    44  //                 "enum": [6, null]
    45  //             }`,
    46  // 			expectedSchema: jsonschema.Schema{
    47  // 				Schema: "https://json-schema.org/draft/2020-12/schema",
    48  // 				Enum:   []interface{}{6, nil},
    49  // 			},
    50  // 		},
    51  // 		{
    52  // 			name: "enums in properties",
    53  // 			schemaJSON: `{
    54  //                 "$schema": "https://json-schema.org/draft/2020-12/schema",
    55  //                 "type": "object",
    56  //                 "properties": {
    57  //                     "foo": {"enum": ["foo"]},
    58  //                     "bar": {"enum": ["bar"]}
    59  //                 },
    60  //                 "required": ["bar"]
    61  //             }`,
    62  // 			expectedSchema: jsonschema.Schema{
    63  // 				Schema: "https://json-schema.org/draft/2020-12/schema",
    64  // 				Types:  jsonschema.SchemaTypes{"object"},
    65  // 				Properties: &jsonschema.SchemaMap{
    66  // 					"foo": &jsonschema.Schema{Enum: []interface{}{"foo"}},
    67  // 					"bar": &jsonschema.Schema{Enum: []interface{}{"bar"}},
    68  // 				},
    69  // 				Required: []string{"bar"},
    70  // 			},
    71  // 		},
    72  // 	}
    73  
    74  // 	for _, tc := range testCases {
    75  // 		t.Run(tc.name, func(t *testing.T) {
    76  // 			var schema jsonschema.Schema
    77  // 			err := json.Unmarshal([]byte(tc.schemaJSON), &schema)
    78  // 			require.NoError(t, err, "Unmarshalling failed unexpectedly")
    79  // 			assert.Equal(t, tc.expectedSchema.ID, schema.ID)
    80  // 			assert.Equal(t, tc.expectedSchema.Schema, schema.Schema)
    81  // 			assert.Equal(t, tc.expectedSchema.Type, schema.Type)
    82  
    83  // 			// Now test marshaling back to JSON
    84  // 			marshaledJSON, err := json.Marshal(schema)
    85  // 			require.NoError(t, err, "Marshalling failed unexpectedly")
    86  
    87  // 			// Unmarshal marshaled JSON to verify it matches the original schema object
    88  // 			var reUnmarshaledSchema jsonschema.Schema
    89  // 			err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema)
    90  // 			require.NoError(t, err, "Unmarshalling the marshaled JSON failed")
    91  // 			assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original")
    92  
    93  // 			// Check if the marshaled JSON matches the original JSON input
    94  // 			assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON")
    95  // 		})
    96  // 	}
    97  // }