code.vegaprotocol.io/vega@v0.79.0/commands/update_party_profile.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package commands
    17  
    18  import (
    19  	"fmt"
    20  
    21  	commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
    22  )
    23  
    24  func CheckUpdatePartyProfile(cmd *commandspb.UpdatePartyProfile) error {
    25  	return checkUpdatePartyProfile(cmd).ErrorOrNil()
    26  }
    27  
    28  func checkUpdatePartyProfile(cmd *commandspb.UpdatePartyProfile) Errors {
    29  	errs := NewErrors()
    30  
    31  	if cmd == nil {
    32  		return errs.FinalAddForProperty("update_party_profile", ErrIsRequired)
    33  	}
    34  
    35  	if len(cmd.Alias) > 32 {
    36  		errs.AddForProperty("update_party_profile.alias", ErrIsLimitedTo32Characters)
    37  	}
    38  
    39  	if len(cmd.Metadata) > 10 {
    40  		errs.AddForProperty("update_party_profile.metadata", ErrIsLimitedTo10Entries)
    41  	} else {
    42  		seenKeys := map[string]interface{}{}
    43  		for i, m := range cmd.Metadata {
    44  			if len(m.Key) > 32 {
    45  				errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrIsLimitedTo32Characters)
    46  			} else if len(m.Key) == 0 {
    47  				errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrCannotBeBlank)
    48  			}
    49  
    50  			_, alreadySeen := seenKeys[m.Key]
    51  			if alreadySeen {
    52  				errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.key", i), ErrIsDuplicated)
    53  			}
    54  			seenKeys[m.Key] = nil
    55  
    56  			if len(m.Value) > 255 {
    57  				errs.AddForProperty(fmt.Sprintf("update_party_profile.metadata.%d.value", i), ErrIsLimitedTo255Characters)
    58  			}
    59  		}
    60  	}
    61  
    62  	return errs
    63  }