1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-16 15:14:24 +00:00
gitea/vendor/github.com/go-openapi/spec/validations.go
6543 d1353e1f7c
Vendor Update (#14496)
* update code.gitea.io/sdk/gitea v0.13.1 -> v0.13.2

* update github.com/go-swagger/go-swagger v0.25.0 -> v0.26.0

* update github.com/google/uuid v1.1.2 -> v1.2.0

* update github.com/klauspost/compress v1.11.3 -> v1.11.7

* update github.com/lib/pq 083382b7e6fc -> v1.9.0

* update github.com/markbates/goth v1.65.0 -> v1.66.1

* update github.com/mattn/go-sqlite3 v1.14.4 -> v1.14.6

* update github.com/mgechev/revive 246eac737dc7 -> v1.0.3

* update github.com/minio/minio-go/v7 v7.0.6 -> v7.0.7

* update github.com/niklasfasching/go-org v1.3.2 -> v1.4.0

* update github.com/olivere/elastic/v7 v7.0.21 -> v7.0.22

* update github.com/pquerna/otp v1.2.0 -> v1.3.0

* update github.com/xanzy/go-gitlab v0.39.0 -> v0.42.0

* update github.com/yuin/goldmark v1.2.1 -> v1.3.1
2021-01-28 17:56:38 +01:00

216 lines
7.1 KiB
Go
Vendored

package spec
// CommonValidations describe common JSON-schema validations
type CommonValidations struct {
Maximum *float64 `json:"maximum,omitempty"`
ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
MaxLength *int64 `json:"maxLength,omitempty"`
MinLength *int64 `json:"minLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
MaxItems *int64 `json:"maxItems,omitempty"`
MinItems *int64 `json:"minItems,omitempty"`
UniqueItems bool `json:"uniqueItems,omitempty"`
MultipleOf *float64 `json:"multipleOf,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
}
// SetValidations defines all validations for a simple schema.
//
// NOTE: the input is the larger set of validations available for schemas.
// For simple schemas, MinProperties and MaxProperties are ignored.
func (v *CommonValidations) SetValidations(val SchemaValidations) {
v.Maximum = val.Maximum
v.ExclusiveMaximum = val.ExclusiveMaximum
v.Minimum = val.Minimum
v.ExclusiveMinimum = val.ExclusiveMinimum
v.MaxLength = val.MaxLength
v.MinLength = val.MinLength
v.Pattern = val.Pattern
v.MaxItems = val.MaxItems
v.MinItems = val.MinItems
v.UniqueItems = val.UniqueItems
v.MultipleOf = val.MultipleOf
v.Enum = val.Enum
}
type clearedValidation struct {
Validation string
Value interface{}
}
type clearedValidations []clearedValidation
func (c clearedValidations) apply(cbs []func(string, interface{})) {
for _, cb := range cbs {
for _, cleared := range c {
cb(cleared.Validation, cleared.Value)
}
}
}
// ClearNumberValidations clears all number validations.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *CommonValidations) ClearNumberValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 5)
defer func() {
done.apply(cbs)
}()
if v.Minimum != nil {
done = append(done, clearedValidation{Validation: "minimum", Value: v.Minimum})
v.Minimum = nil
}
if v.Maximum != nil {
done = append(done, clearedValidation{Validation: "maximum", Value: v.Maximum})
v.Maximum = nil
}
if v.ExclusiveMaximum {
done = append(done, clearedValidation{Validation: "exclusiveMaximum", Value: v.ExclusiveMaximum})
v.ExclusiveMaximum = false
}
if v.ExclusiveMinimum {
done = append(done, clearedValidation{Validation: "exclusiveMinimum", Value: v.ExclusiveMinimum})
v.ExclusiveMinimum = false
}
if v.MultipleOf != nil {
done = append(done, clearedValidation{Validation: "multipleOf", Value: v.MultipleOf})
v.MultipleOf = nil
}
}
// ClearStringValidations clears all string validations.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *CommonValidations) ClearStringValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 3)
defer func() {
done.apply(cbs)
}()
if v.Pattern != "" {
done = append(done, clearedValidation{Validation: "pattern", Value: v.Pattern})
v.Pattern = ""
}
if v.MinLength != nil {
done = append(done, clearedValidation{Validation: "minLength", Value: v.MinLength})
v.MinLength = nil
}
if v.MaxLength != nil {
done = append(done, clearedValidation{Validation: "maxLength", Value: v.MaxLength})
v.MaxLength = nil
}
}
// ClearArrayValidations clears all array validations.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *CommonValidations) ClearArrayValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 3)
defer func() {
done.apply(cbs)
}()
if v.MaxItems != nil {
done = append(done, clearedValidation{Validation: "maxItems", Value: v.MaxItems})
v.MaxItems = nil
}
if v.MinItems != nil {
done = append(done, clearedValidation{Validation: "minItems", Value: v.MinItems})
v.MinItems = nil
}
if v.UniqueItems {
done = append(done, clearedValidation{Validation: "uniqueItems", Value: v.UniqueItems})
v.UniqueItems = false
}
}
// Validations returns a clone of the validations for a simple schema.
//
// NOTE: in the context of simple schema objects, MinProperties, MaxProperties
// and PatternProperties remain unset.
func (v CommonValidations) Validations() SchemaValidations {
return SchemaValidations{
CommonValidations: v,
}
}
// HasNumberValidations indicates if the validations are for numbers or integers
func (v CommonValidations) HasNumberValidations() bool {
return v.Maximum != nil || v.Minimum != nil || v.MultipleOf != nil
}
// HasStringValidations indicates if the validations are for strings
func (v CommonValidations) HasStringValidations() bool {
return v.MaxLength != nil || v.MinLength != nil || v.Pattern != ""
}
// HasArrayValidations indicates if the validations are for arrays
func (v CommonValidations) HasArrayValidations() bool {
return v.MaxItems != nil || v.MinItems != nil || v.UniqueItems
}
// HasEnum indicates if the validation includes some enum constraint
func (v CommonValidations) HasEnum() bool {
return len(v.Enum) > 0
}
// SchemaValidations describes the validation properties of a schema
//
// NOTE: at this moment, this is not embedded in SchemaProps because this would induce a breaking change
// in the exported members: all initializers using litterals would fail.
type SchemaValidations struct {
CommonValidations
PatternProperties SchemaProperties `json:"patternProperties,omitempty"`
MaxProperties *int64 `json:"maxProperties,omitempty"`
MinProperties *int64 `json:"minProperties,omitempty"`
}
// HasObjectValidations indicates if the validations are for objects
func (v SchemaValidations) HasObjectValidations() bool {
return v.MaxProperties != nil || v.MinProperties != nil || v.PatternProperties != nil
}
// SetValidations for schema validations
func (v *SchemaValidations) SetValidations(val SchemaValidations) {
v.CommonValidations.SetValidations(val)
v.PatternProperties = val.PatternProperties
v.MaxProperties = val.MaxProperties
v.MinProperties = val.MinProperties
}
// Validations for a schema
func (v SchemaValidations) Validations() SchemaValidations {
val := v.CommonValidations.Validations()
val.PatternProperties = v.PatternProperties
val.MinProperties = v.MinProperties
val.MaxProperties = v.MaxProperties
return val
}
// ClearObjectValidations returns a clone of the validations with all object validations cleared.
//
// Some callbacks may be set by the caller to capture changed values.
func (v *SchemaValidations) ClearObjectValidations(cbs ...func(string, interface{})) {
done := make(clearedValidations, 0, 3)
defer func() {
done.apply(cbs)
}()
if v.MaxProperties != nil {
done = append(done, clearedValidation{Validation: "maxProperties", Value: v.MaxProperties})
v.MaxProperties = nil
}
if v.MinProperties != nil {
done = append(done, clearedValidation{Validation: "minProperties", Value: v.MinProperties})
v.MinProperties = nil
}
if v.PatternProperties != nil {
done = append(done, clearedValidation{Validation: "patternProperties", Value: v.PatternProperties})
v.PatternProperties = nil
}
}