github.com/kaptinlin/jsonschema@v0.4.6/tests/pattern_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  // TestPatternForTestSuite executes the pattern validation tests for Schema Test Suite.
    13  func TestPatternForTestSuite(t *testing.T) {
    14  	testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/pattern.json")
    15  }
    16  
    17  // TestNonBmpRegexForTestSuite executes the non-bmp-regex validation tests for Schema Test Suite.
    18  func TestNonBmpRegexForTestSuite(t *testing.T) {
    19  	testJSONSchemaTestSuiteWithFilePath(t, "../testdata/JSON-Schema-Test-Suite/tests/draft2020-12/optional/non-bmp-regex.json")
    20  }
    21  
    22  func TestSchemaWithPattern(t *testing.T) {
    23  	testCases := []struct {
    24  		name           string
    25  		schemaJSON     string
    26  		expectedSchema jsonschema.Schema
    27  	}{
    28  		{
    29  			name: "Pattern validation",
    30  			schemaJSON: `{
    31  				"$schema": "https://json-schema.org/draft/2020-12/schema",
    32  				"type": "string",
    33  				"pattern": "^a*$"
    34  			}`,
    35  			expectedSchema: jsonschema.Schema{
    36  				Schema:  "https://json-schema.org/draft/2020-12/schema",
    37  				Type:    jsonschema.SchemaType{"string"},
    38  				Pattern: ptrString("^a*$"),
    39  			},
    40  		},
    41  		{
    42  			name: "Pattern with special characters",
    43  			schemaJSON: `{
    44  				"$schema": "https://json-schema.org/draft/2020-12/schema",
    45  				"type": "string",
    46  				"pattern": "^[a-zA-Z0-9]+$"
    47  			}`,
    48  			expectedSchema: jsonschema.Schema{
    49  				Schema:  "https://json-schema.org/draft/2020-12/schema",
    50  				Type:    jsonschema.SchemaType{"string"},
    51  				Pattern: ptrString("^[a-zA-Z0-9]+$"),
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, tc := range testCases {
    57  		t.Run(tc.name, func(t *testing.T) {
    58  			var schema jsonschema.Schema
    59  			err := json.Unmarshal([]byte(tc.schemaJSON), &schema)
    60  			require.NoError(t, err, "Unmarshalling failed unexpectedly")
    61  			assert.Equal(t, tc.expectedSchema.Schema, schema.Schema)
    62  			assert.Equal(t, tc.expectedSchema.Type, schema.Type)
    63  			assert.Equal(t, *tc.expectedSchema.Pattern, *schema.Pattern)
    64  
    65  			// Now test marshaling back to JSON
    66  			marshaledJSON, err := json.Marshal(schema)
    67  			require.NoError(t, err, "Marshalling failed unexpectedly")
    68  
    69  			// Unmarshal marshaled JSON to verify it matches the original schema object
    70  			var reUnmarshaledSchema jsonschema.Schema
    71  			err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema)
    72  			require.NoError(t, err, "Unmarshalling the marshaled JSON failed")
    73  			assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original")
    74  
    75  			// Check if the marshaled JSON matches the original JSON input
    76  			assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON")
    77  		})
    78  	}
    79  }