Skip to content

Commit

Permalink
Preparing for rollup to top level interface.
Browse files Browse the repository at this point in the history
Path item and path value need recording, to prevent multiple lookups and redundant cycles.

Signed-off-by: Dave Shanley <dave@quobix.com>
  • Loading branch information
daveshanley committed Apr 19, 2023
1 parent 84900ce commit d806d83
Show file tree
Hide file tree
Showing 15 changed files with 2,648 additions and 2,317 deletions.
211 changes: 109 additions & 102 deletions parameters/cookie_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,120 +4,127 @@
package parameters

import (
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
"net/http"
"strconv"
"strings"
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/libopenapi-validator/helpers"
"github.com/pb33f/libopenapi-validator/paths"
"github.com/pb33f/libopenapi/datamodel/high/base"
v3 "github.com/pb33f/libopenapi/datamodel/high/v3"
"net/http"
"strconv"
"strings"
)

// ValidateCookieParams validates the cookie parameters contained within *http.Request.
// It returns a boolean stating true if validation passed (false for failed),
// and a slice of errors if validation failed.
func (v *paramValidator) ValidateCookieParams(request *http.Request) (bool, []*errors.ValidationError) {

// find path
pathItem, errs, _ := paths.FindPath(request, v.document)
if pathItem == nil || errs != nil {
v.errors = errs
return false, errs
}
// find path
var pathItem *v3.PathItem
var errs []*errors.ValidationError
if v.pathItem == nil {
pathItem, errs, _ = paths.FindPath(request, v.document)
if pathItem == nil || errs != nil {
v.errors = errs
return false, errs
}
} else {
pathItem = v.pathItem
}

// extract params for the operation
var params = helpers.ExtractParamsForOperation(request, pathItem)
var validationErrors []*errors.ValidationError
for _, p := range params {
if p.In == helpers.Cookie {
for _, cookie := range request.Cookies() {
if cookie.Name == p.Name { // cookies are case-sensitive, an exact match is required
// extract params for the operation
var params = helpers.ExtractParamsForOperation(request, pathItem)
var validationErrors []*errors.ValidationError
for _, p := range params {
if p.In == helpers.Cookie {
for _, cookie := range request.Cookies() {
if cookie.Name == p.Name { // cookies are case-sensitive, an exact match is required

var sch *base.Schema
if p.Schema != nil {
sch = p.Schema.Schema()
}
pType := sch.Type
var sch *base.Schema
if p.Schema != nil {
sch = p.Schema.Schema()
}
pType := sch.Type

for _, ty := range pType {
switch ty {
case helpers.Integer, helpers.Number:
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
validationErrors = append(validationErrors,
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))
break
}
// check if enum is in range
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == enumVal {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
case helpers.Boolean:
if _, err := strconv.ParseBool(cookie.Value); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch))
}
case helpers.Object:
if !p.IsExploded() {
encodedObj := helpers.ConstructMapFromCSV(cookie.Value)
for _, ty := range pType {
switch ty {
case helpers.Integer, helpers.Number:
if _, err := strconv.ParseFloat(cookie.Value, 64); err != nil {
validationErrors = append(validationErrors,
errors.InvalidCookieParamNumber(p, strings.ToLower(cookie.Value), sch))
break
}
// check if enum is in range
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == enumVal {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
case helpers.Boolean:
if _, err := strconv.ParseBool(cookie.Value); err != nil {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamBool(p, strings.ToLower(cookie.Value), sch))
}
case helpers.Object:
if !p.IsExploded() {
encodedObj := helpers.ConstructMapFromCSV(cookie.Value)

// if a schema was extracted
if sch != nil {
validationErrors = append(validationErrors,
ValidateParameterSchema(sch, encodedObj, "",
"Cookie parameter",
"The cookie parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationQuery)...)
}
}
case helpers.Array:
// if a schema was extracted
if sch != nil {
validationErrors = append(validationErrors,
ValidateParameterSchema(sch, encodedObj, "",
"Cookie parameter",
"The cookie parameter",
p.Name,
helpers.ParameterValidation,
helpers.ParameterValidationQuery)...)
}
}
case helpers.Array:

if !p.IsExploded() {
// well we're already in an array, so we need to check the items schema
// to ensure this array items matches the type
// only check if items is a schema, not a boolean
if sch.Items.IsA() {
validationErrors = append(validationErrors,
ValidateCookieArray(sch, p, cookie.Value)...)
}
}
if !p.IsExploded() {
// well we're already in an array, so we need to check the items schema
// to ensure this array items matches the type
// only check if items is a schema, not a boolean
if sch.Items.IsA() {
validationErrors = append(validationErrors,
ValidateCookieArray(sch, p, cookie.Value)...)
}
}

case helpers.String:
case helpers.String:

// check if the schema has an enum, and if so, match the value against one of
// the defined enum values.
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == enumVal {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
}
}
}
}
}
}
if len(validationErrors) > 0 {
return false, validationErrors
}
return true, nil
// check if the schema has an enum, and if so, match the value against one of
// the defined enum values.
if sch.Enum != nil {
matchFound := false
for _, enumVal := range sch.Enum {
if strings.TrimSpace(cookie.Value) == enumVal {
matchFound = true
break
}
}
if !matchFound {
validationErrors = append(validationErrors,
errors.IncorrectCookieParamEnum(p, strings.ToLower(cookie.Value), sch))
}
}
}
}
}
}
}
}
if len(validationErrors) > 0 {
return false, validationErrors
}
return true, nil
}
Loading

0 comments on commit d806d83

Please sign in to comment.