go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/proto/gerrit/validation.go (about)

     1  // Copyright 2018 The LUCI 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  package gerritpb
    16  
    17  import (
    18  	"go.chromium.org/luci/common/errors"
    19  )
    20  
    21  // Validate returns an error if r is invalid.
    22  func (r *GetChangeRequest) Validate() error {
    23  	switch {
    24  	case r.Number <= 0:
    25  		return errors.New("number must be positive")
    26  	default:
    27  		return nil
    28  	}
    29  }
    30  
    31  // Validate returns an error if r is invalid.
    32  func (r *SetReviewRequest) Validate() error {
    33  	if r.Number <= 0 {
    34  		return errors.New("number must be positive")
    35  	}
    36  	if err := r.GetNotifyDetails().Validate(); err != nil {
    37  		return err
    38  	}
    39  	for _, att := range r.GetAddToAttentionSet() {
    40  		if err := att.Validate(); err != nil {
    41  			return err
    42  		}
    43  	}
    44  	for _, att := range r.GetRemoveFromAttentionSet() {
    45  		if err := att.Validate(); err != nil {
    46  			return err
    47  		}
    48  	}
    49  	return nil
    50  }
    51  
    52  // Validate returns an error if r is invalid.
    53  func (r *RevertChangeRequest) Validate() error {
    54  	if r.Number <= 0 {
    55  		return errors.New("number must be positive")
    56  	}
    57  	return nil
    58  }
    59  
    60  // Validate returns an error if n is invalid.
    61  func (n *NotifyDetails) Validate() error {
    62  	for _, r := range n.GetRecipients() {
    63  		if err := r.Validate(); err != nil {
    64  			return err
    65  		}
    66  	}
    67  	return nil
    68  }
    69  
    70  // Validate returns an error if n is invalid.
    71  func (n *NotifyDetails_Recipient) Validate() error {
    72  	if n.GetRecipientType() == NotifyDetails_RECIPIENT_TYPE_UNSPECIFIED {
    73  		return errors.New("must specify recipient type")
    74  	}
    75  	return nil
    76  }
    77  
    78  // Validate returns an error if r is invalid.
    79  func (r *AttentionSetRequest) Validate() error {
    80  	if r.Number <= 0 {
    81  		return errors.New("number must be positive")
    82  	}
    83  	if r.GetInput() == nil {
    84  		return errors.New("input is required")
    85  	}
    86  	return r.GetInput().Validate()
    87  }
    88  
    89  // Validate returns an error if i is invalid.
    90  func (i *AttentionSetInput) Validate() error {
    91  	if i.GetUser() == "" {
    92  		return errors.New("user is required")
    93  	}
    94  	if i.GetReason() == "" {
    95  		return errors.New("reason is required")
    96  	}
    97  	return nil
    98  }
    99  
   100  // Validate returns an error if r is invalid.
   101  func (r *GetMetaDiffRequest) Validate() error {
   102  	switch {
   103  	case r.Number <= 0:
   104  		return errors.New("number must be positive")
   105  	default:
   106  		return nil
   107  	}
   108  }