Skip to content

Commit 6c992c2

Browse files
test(schema): add test for existing allOf with if/then/else constructs
Verify backward compatibility - schemas with pre-existing standard JSON Schema conditional validation (allOf with if/then/else) continue to work correctly after the options.dependencies transformation changes. Co-Authored-By: Martin Vasko <Matovidlo2@gmail.com>
1 parent 01dcb7c commit 6c992c2

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

internal/pkg/encoding/json/schema/schema_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,70 @@ func TestNormalizeSchema_OptionsDependencies_ArrayValidation(t *testing.T) {
898898
require.NoError(t, err, "Should pass validation when protocol=FTP and passive_mode is provided")
899899
}
900900

901+
func TestNormalizeSchema_ExistingAllOfIfThen(t *testing.T) {
902+
t.Parallel()
903+
904+
// Test that schemas with existing allOf containing if/then/else constructs
905+
// continue to work correctly (backward compatibility)
906+
schema := []byte(`
907+
{
908+
"type": "object",
909+
"required": ["append_date"],
910+
"properties": {
911+
"append_date": {
912+
"type": "integer",
913+
"default": 0
914+
},
915+
"append_date_format": {
916+
"type": "string"
917+
}
918+
},
919+
"allOf": [{
920+
"if": { "properties": { "append_date": { "const": 1 } } },
921+
"then": { "required": ["append_date_format"] }
922+
}]
923+
}
924+
`)
925+
926+
// Test 1: When append_date = 0, append_date_format should NOT be required
927+
content1 := orderedmap.FromPairs([]orderedmap.Pair{
928+
{
929+
Key: "parameters",
930+
Value: orderedmap.FromPairs([]orderedmap.Pair{
931+
{Key: "append_date", Value: 0},
932+
}),
933+
},
934+
})
935+
err := ValidateContent(schema, content1)
936+
require.NoError(t, err, "Should pass validation when append_date=0 and append_date_format is missing")
937+
938+
// Test 2: When append_date = 1, append_date_format SHOULD be required
939+
content2 := orderedmap.FromPairs([]orderedmap.Pair{
940+
{
941+
Key: "parameters",
942+
Value: orderedmap.FromPairs([]orderedmap.Pair{
943+
{Key: "append_date", Value: 1},
944+
}),
945+
},
946+
})
947+
err = ValidateContent(schema, content2)
948+
require.Error(t, err, "Should fail validation when append_date=1 and append_date_format is missing")
949+
assert.Contains(t, err.Error(), "append_date_format")
950+
951+
// Test 3: When append_date = 1 and append_date_format is provided, should pass
952+
content3 := orderedmap.FromPairs([]orderedmap.Pair{
953+
{
954+
Key: "parameters",
955+
Value: orderedmap.FromPairs([]orderedmap.Pair{
956+
{Key: "append_date", Value: 1},
957+
{Key: "append_date_format", Value: "Y-m-d"},
958+
}),
959+
},
960+
})
961+
err = ValidateContent(schema, content3)
962+
require.NoError(t, err, "Should pass validation when append_date=1 and append_date_format is provided")
963+
}
964+
901965
func getTestSchema() []byte {
902966
return []byte(`
903967
{

0 commit comments

Comments
 (0)