Skip to content

Commit 38c612f

Browse files
committed
Fixes issue #86
Now you can use the API more effectively.
1 parent d93c54b commit 38c612f

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

validator.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ type Validator interface {
6161

6262
// GetResponseBodyValidator will return a parameters.ResponseBodyValidator instance used to validate response bodies
6363
GetResponseBodyValidator() responses.ResponseBodyValidator
64+
65+
// SetDocument will set the OpenAPI 3+ document to be validated
66+
SetDocument(document libopenapi.Document)
6467
}
6568

6669
// NewValidator will create a new Validator from an OpenAPI 3+ document
@@ -92,6 +95,10 @@ func NewValidatorFromV3Model(m *v3.Document, opts ...config.Option) Validator {
9295
return v
9396
}
9497

98+
func (v *validator) SetDocument(document libopenapi.Document) {
99+
v.document = document
100+
}
101+
95102
func (v *validator) GetParameterValidator() parameters.ParameterValidator {
96103
return v.paramValidator
97104
}
@@ -105,6 +112,17 @@ func (v *validator) GetResponseBodyValidator() responses.ResponseBodyValidator {
105112
}
106113

107114
func (v *validator) ValidateDocument() (bool, []*errors.ValidationError) {
115+
if v.document == nil {
116+
return false, []*errors.ValidationError{{
117+
ValidationType: "document",
118+
ValidationSubType: "missing",
119+
Message: "Document is not set",
120+
Reason: "The document cannot be validated as it is not set",
121+
SpecLine: 1,
122+
SpecCol: 1,
123+
HowToFix: "Set the document via `SetDocument` before validating",
124+
}}
125+
}
108126
var validationOpts []config.Option
109127
if v.options != nil {
110128
validationOpts = append(validationOpts, config.WithRegexEngine(v.options.RegexEngine))

validator_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,3 +1696,59 @@ components:
16961696

16971697
}
16981698
}
1699+
1700+
// https://github.com/pb33f/libopenapi-validator/issues/86
1701+
func TestNewValidator_HaveYourModelAndEatIt(t *testing.T) {
1702+
spec := `openapi: 3.1.0
1703+
info:
1704+
title: Panic at response validation
1705+
version: 1.0.0
1706+
paths:
1707+
/operations:
1708+
delete:
1709+
description: Delete operations
1710+
responses:
1711+
default:
1712+
description: Any response
1713+
content:
1714+
application/json:
1715+
schema:
1716+
$ref: '#/components/schemas/Error'
1717+
1718+
components:
1719+
schemas:
1720+
Error:
1721+
type: object
1722+
properties:
1723+
code:
1724+
type: string
1725+
details:
1726+
type: array
1727+
items:
1728+
$ref: '#/components/schemas/Error'`
1729+
1730+
document, err := libopenapi.NewDocument([]byte(spec))
1731+
1732+
if err != nil {
1733+
panic(fmt.Sprintf("failed to create new document: %v\n", err))
1734+
}
1735+
1736+
model, errs := document.BuildV3Model()
1737+
1738+
if errs != nil {
1739+
panic(fmt.Sprintf("failed to create v3 model from document: %d errors reported", len(errs)))
1740+
}
1741+
1742+
v := NewValidatorFromV3Model(&model.Model)
1743+
1744+
valid, vErrs := v.ValidateDocument()
1745+
assert.False(t, valid)
1746+
assert.Len(t, vErrs, 1)
1747+
assert.Equal(t, "The document cannot be validated as it is not set", vErrs[0].Reason)
1748+
1749+
v.SetDocument(document)
1750+
1751+
valid, vErrs = v.ValidateDocument()
1752+
assert.True(t, valid)
1753+
assert.Len(t, vErrs, 0)
1754+
}

0 commit comments

Comments
 (0)