mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 19:47:56 +00:00
refactor: directly implement ValueValidator
inlineValidator unnecessary, since we already have access to the `new` object
This commit is contained in:
parent
4fb5f1a611
commit
30cf9ed567
@ -60,7 +60,7 @@ func (r *RatchetingSchemaValidator) Validate(new interface{}) *validate.Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *RatchetingSchemaValidator) ValidateUpdate(new, old interface{}) *validate.Result {
|
func (r *RatchetingSchemaValidator) ValidateUpdate(new, old interface{}) *validate.Result {
|
||||||
return newRatchetingValueValidator(new, old, r.schemaArgs).Validate()
|
return newRatchetingValueValidator(new, old, r.schemaArgs).Validate(new)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ratchetingValueValidator represents an invocation of SchemaValidator.ValidateUpdate
|
// ratchetingValueValidator represents an invocation of SchemaValidator.ValidateUpdate
|
||||||
@ -145,7 +145,7 @@ func (r *ratchetingValueValidator) getValidateOption() validate.Option {
|
|||||||
//
|
//
|
||||||
// This call has a side-effect of populating it's `children` variable with
|
// This call has a side-effect of populating it's `children` variable with
|
||||||
// the explored nodes of the object tree.
|
// the explored nodes of the object tree.
|
||||||
func (r *ratchetingValueValidator) Validate() *validate.Result {
|
func (r *ratchetingValueValidator) Validate(new interface{}) *validate.Result {
|
||||||
opts := append([]validate.Option{
|
opts := append([]validate.Option{
|
||||||
r.getValidateOption(),
|
r.getValidateOption(),
|
||||||
}, r.options...)
|
}, r.options...)
|
||||||
@ -177,29 +177,27 @@ func (r *ratchetingValueValidator) Validate() *validate.Result {
|
|||||||
// If the old value cannot be correlated, then default validation is used.
|
// If the old value cannot be correlated, then default validation is used.
|
||||||
func (r *ratchetingValueValidator) SubPropertyValidator(field string, schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry, options ...validate.Option) validate.ValueValidator {
|
func (r *ratchetingValueValidator) SubPropertyValidator(field string, schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry, options ...validate.Option) validate.ValueValidator {
|
||||||
// Find correlated old value
|
// Find correlated old value
|
||||||
asMap, ok := r.oldValue.(map[string]interface{})
|
oldAsMap, okOld := r.oldValue.(map[string]interface{})
|
||||||
if !ok {
|
newAsMap, okNew := r.value.(map[string]interface{})
|
||||||
|
if !okOld || !okNew {
|
||||||
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
oldValueForField, ok := asMap[field]
|
oldValueForField, okOld := oldAsMap[field]
|
||||||
if !ok {
|
newValueForField, okNew := newAsMap[field]
|
||||||
|
if !okOld || !okNew {
|
||||||
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inlineValidator(func(new interface{}) *validate.Result {
|
childNode := newRatchetingValueValidator(newValueForField, oldValueForField, schemaArgs{
|
||||||
childNode := newRatchetingValueValidator(new, oldValueForField, schemaArgs{
|
schema: schema,
|
||||||
schema: schema,
|
root: rootSchema,
|
||||||
root: rootSchema,
|
path: root,
|
||||||
path: root,
|
knownFormats: formats,
|
||||||
knownFormats: formats,
|
options: options,
|
||||||
options: options,
|
|
||||||
})
|
|
||||||
|
|
||||||
r.children[field] = childNode
|
|
||||||
return childNode.Validate()
|
|
||||||
})
|
})
|
||||||
|
r.children[field] = childNode
|
||||||
|
return childNode
|
||||||
}
|
}
|
||||||
|
|
||||||
// SubIndexValidator overrides the standard validator constructor for sub-indicies by
|
// SubIndexValidator overrides the standard validator constructor for sub-indicies by
|
||||||
@ -216,18 +214,21 @@ func (r *ratchetingValueValidator) SubIndexValidator(index int, schema *spec.Sch
|
|||||||
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
||||||
}
|
}
|
||||||
|
|
||||||
return inlineValidator(func(new interface{}) *validate.Result {
|
asList, ok := r.value.([]interface{})
|
||||||
childNode := newRatchetingValueValidator(new, oldValueForIndex, schemaArgs{
|
if !ok || len(asList) <= index {
|
||||||
schema: schema,
|
return validate.NewSchemaValidator(schema, rootSchema, root, formats, options...)
|
||||||
root: rootSchema,
|
}
|
||||||
path: root,
|
|
||||||
knownFormats: formats,
|
|
||||||
options: options,
|
|
||||||
})
|
|
||||||
|
|
||||||
r.children[index] = childNode
|
childNode := newRatchetingValueValidator(asList[index], oldValueForIndex, schemaArgs{
|
||||||
return childNode.Validate()
|
schema: schema,
|
||||||
|
root: rootSchema,
|
||||||
|
path: root,
|
||||||
|
knownFormats: formats,
|
||||||
|
options: options,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
r.children[index] = childNode
|
||||||
|
return childNode
|
||||||
}
|
}
|
||||||
|
|
||||||
// If oldValue is not a list, returns nil
|
// If oldValue is not a list, returns nil
|
||||||
@ -383,26 +384,13 @@ func (r *ratchetingValueValidator) CachedDeepEqual() (res bool) {
|
|||||||
return reflect.DeepEqual(r.oldValue, r.value)
|
return reflect.DeepEqual(r.oldValue, r.value)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A validator which just calls a validate function, and advertises that it
|
var _ validate.ValueValidator = (&ratchetingValueValidator{})
|
||||||
// validates anything
|
|
||||||
//
|
|
||||||
// In the future kube-openapi's ValueValidator interface can be simplified
|
|
||||||
// to be closer to `currentValidator.Options.NewValidator(value, ...).Validate()`
|
|
||||||
// so that a tree of "validation nodes" can be more formally encoded in the API.
|
|
||||||
// In that case this class would not be necessary.
|
|
||||||
type inlineValidator func(new interface{}) *validate.Result
|
|
||||||
|
|
||||||
var _ validate.ValueValidator = inlineValidator(nil)
|
func (f ratchetingValueValidator) SetPath(path string) {
|
||||||
|
|
||||||
func (f inlineValidator) Validate(new interface{}) *validate.Result {
|
|
||||||
return f(new)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f inlineValidator) SetPath(path string) {
|
|
||||||
// Do nothing
|
// Do nothing
|
||||||
// Unused by kube-openapi
|
// Unused by kube-openapi
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f inlineValidator) Applies(source interface{}, valueKind reflect.Kind) bool {
|
func (f ratchetingValueValidator) Applies(source interface{}, valueKind reflect.Kind) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user