...

Source file src/github.com/chaos-mesh/chaos-mesh/api/genericwebhook/validater.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/genericwebhook

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package genericwebhook
    17  
    18  import (
    19  	"reflect"
    20  	"strings"
    21  
    22  	"github.com/pkg/errors"
    23  	"k8s.io/apimachinery/pkg/util/validation/field"
    24  )
    25  
    26  type FieldValidator interface {
    27  	Validate(root interface{}, path *field.Path) field.ErrorList
    28  }
    29  
    30  // Validate would walk through all the fields of target struct recursively, and validate the value with validator declared with struct tag "webhook".
    31  //
    32  // Parameter obj should be a pointer to a data struct.
    33  //
    34  // Validate should return an empty field.ErrorList if all the fields are valid, or return each field.Error for every invalid values.
    35  func Validate(obj interface{}) field.ErrorList {
    36  	// TODO: how to resolve invalid input, for example: obj is a pointer to pointer
    37  	errorList := field.ErrorList{}
    38  
    39  	root := obj
    40  	walker := NewFieldWalker(obj, func(path *field.Path, obj interface{}, field *reflect.StructField) bool {
    41  		webhookAttr := ""
    42  		if field != nil {
    43  			webhookAttr = field.Tag.Get("webhook")
    44  		}
    45  		attributes := strings.Split(webhookAttr, ",")
    46  
    47  		webhook := ""
    48  		nilable := false
    49  		if len(attributes) > 0 {
    50  			webhook = attributes[0]
    51  		}
    52  		if len(attributes) > 1 {
    53  			nilable = attributes[1] == "nilable"
    54  		}
    55  
    56  		validator := getValidator(obj, webhook, nilable)
    57  		if validator != nil {
    58  			if err := validator.Validate(root, path); err != nil {
    59  				errorList = append(errorList, err...)
    60  			}
    61  		}
    62  
    63  		return true
    64  	})
    65  	walker.Walk()
    66  
    67  	return errorList
    68  }
    69  
    70  func Aggregate(errs field.ErrorList) error {
    71  	if errs == nil || len(errs) == 0 {
    72  		return nil
    73  	}
    74  	return errors.New(errs.ToAggregate().Error())
    75  }
    76  
    77  func getValidator(obj interface{}, webhook string, nilable bool) FieldValidator {
    78  	// There are two possible situations:
    79  	// 1. The field is a value (int, string, normal struct, etc), and the obj is the reference of it.
    80  	// 2. The field is a pointer to a value or a slice, then the obj is itself.
    81  
    82  	val := reflect.ValueOf(obj)
    83  
    84  	if validator, ok := obj.(FieldValidator); ok {
    85  		if nilable || !val.IsZero() {
    86  			return validator
    87  		}
    88  	}
    89  
    90  	if webhook != "" {
    91  		webhookImpl := webhooks[webhook]
    92  
    93  		v := val.Convert(webhookImpl).Interface()
    94  		if validator, ok := v.(FieldValidator); ok {
    95  			if nilable || !val.IsZero() {
    96  				return validator
    97  			}
    98  		}
    99  	}
   100  
   101  	return nil
   102  }
   103