Skip to content

Commit

Permalink
parse objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SVilgelm committed Dec 28, 2024
1 parent 0706e46 commit bd1e279
Show file tree
Hide file tree
Showing 8 changed files with 1,063 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ The implementation of OpenAPI v3.1 Specification for Go using generics.
* `Validator.ValidateData()` method validates the data.
* `Validator.ValidateDataAsJSON()` method validates the data by converting it into `map[string]any` type first using `json.Marshal` and `json.Unmarshal`.
**WARNING**: the function is slow due to double conversion.
* Added `ParseObject` function to create `SchemaBuilder` by parsing an object.
The function supports `json`, `yaml` and `openapi` field tags for the structs.
* Use OpenAPI `v3.1.1` by default.

## Features
Expand Down
2 changes: 2 additions & 0 deletions bool_or_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func NewBoolOrSchema(v any) *BoolOrSchema {
return &BoolOrSchema{Allowed: v}
case *RefOrSpec[Schema]:
return &BoolOrSchema{Schema: v}
case *SchemaBulder:
return &BoolOrSchema{Schema: v.Build()}
default:
return nil
}
Expand Down
84 changes: 54 additions & 30 deletions components.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package openapi

import (
"regexp"
)

// Components holds a set of reusable objects for different aspects of the OAS.
// All objects defined within the components object will have no effect on the API unless they are explicitly referenced
// from properties outside the components object.
Expand Down Expand Up @@ -160,57 +164,77 @@ func (o *Components) Add(name string, v any) *Components {
return o
}

var namePattern = regexp.MustCompile(`^[a-zA-Z0-9\.\-_]+$`)

func (o *Components) validateSpec(location string, validator *Validator) []*validationError {
var errs []*validationError
if o.Schemas != nil {
for k, v := range o.Schemas {
errs = append(errs, v.validateSpec(joinLoc(location, "schemas", k), validator)...)
for k, v := range o.Schemas {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 173 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L173

Added line #L173 was not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "schemas", k), validator)...)
}
if o.Responses != nil {
for k, v := range o.Responses {
errs = append(errs, v.validateSpec(joinLoc(location, "responses", k), validator)...)

for k, v := range o.Responses {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 180 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L179-L180

Added lines #L179 - L180 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "responses", k), validator)...)

Check warning on line 182 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L182

Added line #L182 was not covered by tests
}
if o.Parameters != nil {
for k, v := range o.Parameters {
errs = append(errs, v.validateSpec(joinLoc(location, "parameters", k), validator)...)
for k, v := range o.Parameters {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 186 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L185-L186

Added lines #L185 - L186 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "parameters", k), validator)...)

Check warning on line 188 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L188

Added line #L188 was not covered by tests
}
if o.Examples != nil {
for k, v := range o.Examples {
errs = append(errs, v.validateSpec(joinLoc(location, "examples", k), validator)...)

for k, v := range o.Examples {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 193 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L192-L193

Added lines #L192 - L193 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "examples", k), validator)...)

Check warning on line 195 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L195

Added line #L195 was not covered by tests
}
if o.RequestBodies != nil {
for k, v := range o.RequestBodies {
errs = append(errs, v.validateSpec(joinLoc(location, "requestBodies", k), validator)...)

for k, v := range o.RequestBodies {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 200 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L199-L200

Added lines #L199 - L200 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "requestBodies", k), validator)...)

Check warning on line 202 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L202

Added line #L202 was not covered by tests
}
if o.Headers != nil {
for k, v := range o.Headers {
errs = append(errs, v.validateSpec(joinLoc(location, "headers", k), validator)...)

for k, v := range o.Headers {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 207 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L206-L207

Added lines #L206 - L207 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "headers", k), validator)...)

Check warning on line 209 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L209

Added line #L209 was not covered by tests
}
if o.SecuritySchemes != nil {
for k, v := range o.SecuritySchemes {
errs = append(errs, v.validateSpec(joinLoc(location, "securitySchemes", k), validator)...)

for k, v := range o.SecuritySchemes {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 214 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L214

Added line #L214 was not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "securitySchemes", k), validator)...)
}
if o.Links != nil {
for k, v := range o.Links {
errs = append(errs, v.validateSpec(joinLoc(location, "links", k), validator)...)

for k, v := range o.Links {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 221 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L221

Added line #L221 was not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "links", k), validator)...)
}
if o.Callbacks != nil {
for k, v := range o.Callbacks {
errs = append(errs, v.validateSpec(joinLoc(location, "callbacks", k), validator)...)

for k, v := range o.Callbacks {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 228 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L227-L228

Added lines #L227 - L228 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "callbacks", k), validator)...)

Check warning on line 230 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L230

Added line #L230 was not covered by tests
}
if o.Paths != nil {
for k, v := range o.Paths {
errs = append(errs, v.validateSpec(joinLoc(location, "paths", k), validator)...)

for k, v := range o.Paths {
if !namePattern.MatchString(k) {
errs = append(errs, newValidationError(joinLoc(location, "schemas", k), "invalid name %q, must match %q", k, namePattern.String()))

Check warning on line 235 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L234-L235

Added lines #L234 - L235 were not covered by tests
}
errs = append(errs, v.validateSpec(joinLoc(location, "paths", k), validator)...)

Check warning on line 237 in components.go

View check run for this annotation

Codecov / codecov/patch

components.go#L237

Added line #L237 was not covered by tests
}

return errs
Expand Down
Loading

0 comments on commit bd1e279

Please sign in to comment.