Skip to content

v2.3.0: Example validation tests + auto-detect discriminator from const/enum

Choose a tag to compare

@timofriedlberlin timofriedlberlin released this 22 Apr 22:47
· 30 commits to main since this release

Example validation tests

When --examples is used, the generated test file now includes TestCompschema_ExamplesValidate — a table-driven test that validates every generated example against its JSON Schema:

func TestCompschema_ExamplesValidate(t *testing.T) {
    tests := []struct {
        name    string
        example string
    }{
        {"Circle", `{"radius":1,"type":"circle"}`},
        {"Order", `{"id":"example","items":[{"qty":1,"sku":"ABC-123"}],...}`},
    }
    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            sch := compschemaValidator(tt.name)
            sch.Validate(v)  // proves example is schema-compliant
        })
    }
}

Auto-detect discriminator from const AND enum fields

Discriminator detection now works for both patterns:

const (JSON Schema):

{"properties": {"type": {"const": "circle"}}}

Single-value enum (common in OpenAPI):

{"properties": {"type": {"type": "string", "enum": ["circle"]}}}

When all oneOf variants share a field with distinct const or single-value enum values, that field is automatically used as the discriminator. Works in both:

  • Go → IR (analyzer): any field with jsonschema:"const=..."
  • JSON Schema → IR (import/diff): const constraints and single-value enum types

Previously only json:"type" fields with const constraints were detected.

Bug fix: field-level constraints in examples

Field-level constraints (pattern, format, minimum) now properly flow through to the example generator. Previously sku with pattern=^[A-Z]{3}-[0-9]+$ would generate "example" — now generates "ABC-123".