podman
215 строк · 7.1 Кб
1package spec2
3// CommonValidations describe common JSON-schema validations
4type CommonValidations struct {5Maximum *float64 `json:"maximum,omitempty"`6ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`7Minimum *float64 `json:"minimum,omitempty"`8ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`9MaxLength *int64 `json:"maxLength,omitempty"`10MinLength *int64 `json:"minLength,omitempty"`11Pattern string `json:"pattern,omitempty"`12MaxItems *int64 `json:"maxItems,omitempty"`13MinItems *int64 `json:"minItems,omitempty"`14UniqueItems bool `json:"uniqueItems,omitempty"`15MultipleOf *float64 `json:"multipleOf,omitempty"`16Enum []interface{} `json:"enum,omitempty"`17}
18
19// SetValidations defines all validations for a simple schema.
20//
21// NOTE: the input is the larger set of validations available for schemas.
22// For simple schemas, MinProperties and MaxProperties are ignored.
23func (v *CommonValidations) SetValidations(val SchemaValidations) {24v.Maximum = val.Maximum25v.ExclusiveMaximum = val.ExclusiveMaximum26v.Minimum = val.Minimum27v.ExclusiveMinimum = val.ExclusiveMinimum28v.MaxLength = val.MaxLength29v.MinLength = val.MinLength30v.Pattern = val.Pattern31v.MaxItems = val.MaxItems32v.MinItems = val.MinItems33v.UniqueItems = val.UniqueItems34v.MultipleOf = val.MultipleOf35v.Enum = val.Enum36}
37
38type clearedValidation struct {39Validation string40Value interface{}41}
42
43type clearedValidations []clearedValidation44
45func (c clearedValidations) apply(cbs []func(string, interface{})) {46for _, cb := range cbs {47for _, cleared := range c {48cb(cleared.Validation, cleared.Value)49}50}51}
52
53// ClearNumberValidations clears all number validations.
54//
55// Some callbacks may be set by the caller to capture changed values.
56func (v *CommonValidations) ClearNumberValidations(cbs ...func(string, interface{})) {57done := make(clearedValidations, 0, 5)58defer func() {59done.apply(cbs)60}()61
62if v.Minimum != nil {63done = append(done, clearedValidation{Validation: "minimum", Value: v.Minimum})64v.Minimum = nil65}66if v.Maximum != nil {67done = append(done, clearedValidation{Validation: "maximum", Value: v.Maximum})68v.Maximum = nil69}70if v.ExclusiveMaximum {71done = append(done, clearedValidation{Validation: "exclusiveMaximum", Value: v.ExclusiveMaximum})72v.ExclusiveMaximum = false73}74if v.ExclusiveMinimum {75done = append(done, clearedValidation{Validation: "exclusiveMinimum", Value: v.ExclusiveMinimum})76v.ExclusiveMinimum = false77}78if v.MultipleOf != nil {79done = append(done, clearedValidation{Validation: "multipleOf", Value: v.MultipleOf})80v.MultipleOf = nil81}82}
83
84// ClearStringValidations clears all string validations.
85//
86// Some callbacks may be set by the caller to capture changed values.
87func (v *CommonValidations) ClearStringValidations(cbs ...func(string, interface{})) {88done := make(clearedValidations, 0, 3)89defer func() {90done.apply(cbs)91}()92
93if v.Pattern != "" {94done = append(done, clearedValidation{Validation: "pattern", Value: v.Pattern})95v.Pattern = ""96}97if v.MinLength != nil {98done = append(done, clearedValidation{Validation: "minLength", Value: v.MinLength})99v.MinLength = nil100}101if v.MaxLength != nil {102done = append(done, clearedValidation{Validation: "maxLength", Value: v.MaxLength})103v.MaxLength = nil104}105}
106
107// ClearArrayValidations clears all array validations.
108//
109// Some callbacks may be set by the caller to capture changed values.
110func (v *CommonValidations) ClearArrayValidations(cbs ...func(string, interface{})) {111done := make(clearedValidations, 0, 3)112defer func() {113done.apply(cbs)114}()115
116if v.MaxItems != nil {117done = append(done, clearedValidation{Validation: "maxItems", Value: v.MaxItems})118v.MaxItems = nil119}120if v.MinItems != nil {121done = append(done, clearedValidation{Validation: "minItems", Value: v.MinItems})122v.MinItems = nil123}124if v.UniqueItems {125done = append(done, clearedValidation{Validation: "uniqueItems", Value: v.UniqueItems})126v.UniqueItems = false127}128}
129
130// Validations returns a clone of the validations for a simple schema.
131//
132// NOTE: in the context of simple schema objects, MinProperties, MaxProperties
133// and PatternProperties remain unset.
134func (v CommonValidations) Validations() SchemaValidations {135return SchemaValidations{136CommonValidations: v,137}138}
139
140// HasNumberValidations indicates if the validations are for numbers or integers
141func (v CommonValidations) HasNumberValidations() bool {142return v.Maximum != nil || v.Minimum != nil || v.MultipleOf != nil143}
144
145// HasStringValidations indicates if the validations are for strings
146func (v CommonValidations) HasStringValidations() bool {147return v.MaxLength != nil || v.MinLength != nil || v.Pattern != ""148}
149
150// HasArrayValidations indicates if the validations are for arrays
151func (v CommonValidations) HasArrayValidations() bool {152return v.MaxItems != nil || v.MinItems != nil || v.UniqueItems153}
154
155// HasEnum indicates if the validation includes some enum constraint
156func (v CommonValidations) HasEnum() bool {157return len(v.Enum) > 0158}
159
160// SchemaValidations describes the validation properties of a schema
161//
162// NOTE: at this moment, this is not embedded in SchemaProps because this would induce a breaking change
163// in the exported members: all initializers using litterals would fail.
164type SchemaValidations struct {165CommonValidations
166
167PatternProperties SchemaProperties `json:"patternProperties,omitempty"`168MaxProperties *int64 `json:"maxProperties,omitempty"`169MinProperties *int64 `json:"minProperties,omitempty"`170}
171
172// HasObjectValidations indicates if the validations are for objects
173func (v SchemaValidations) HasObjectValidations() bool {174return v.MaxProperties != nil || v.MinProperties != nil || v.PatternProperties != nil175}
176
177// SetValidations for schema validations
178func (v *SchemaValidations) SetValidations(val SchemaValidations) {179v.CommonValidations.SetValidations(val)180v.PatternProperties = val.PatternProperties181v.MaxProperties = val.MaxProperties182v.MinProperties = val.MinProperties183}
184
185// Validations for a schema
186func (v SchemaValidations) Validations() SchemaValidations {187val := v.CommonValidations.Validations()188val.PatternProperties = v.PatternProperties189val.MinProperties = v.MinProperties190val.MaxProperties = v.MaxProperties191return val192}
193
194// ClearObjectValidations returns a clone of the validations with all object validations cleared.
195//
196// Some callbacks may be set by the caller to capture changed values.
197func (v *SchemaValidations) ClearObjectValidations(cbs ...func(string, interface{})) {198done := make(clearedValidations, 0, 3)199defer func() {200done.apply(cbs)201}()202
203if v.MaxProperties != nil {204done = append(done, clearedValidation{Validation: "maxProperties", Value: v.MaxProperties})205v.MaxProperties = nil206}207if v.MinProperties != nil {208done = append(done, clearedValidation{Validation: "minProperties", Value: v.MinProperties})209v.MinProperties = nil210}211if v.PatternProperties != nil {212done = append(done, clearedValidation{Validation: "patternProperties", Value: v.PatternProperties})213v.PatternProperties = nil214}215}
216