github.com/blend/go-sdk@v1.20220411.3/selector/check_name.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package selector
     9  
    10  import (
    11  	"unicode/utf8"
    12  )
    13  
    14  // CheckName checks the characters in a name but does not validate the length.
    15  func CheckName(value string) (err error) {
    16  	valueLen := len(value)
    17  	var state int
    18  	var ch rune
    19  	var width int
    20  	for pos := 0; pos < valueLen; pos += width {
    21  		ch, width = utf8.DecodeRuneInString(value[pos:])
    22  		switch state {
    23  		case 0: //check prefix/suffix
    24  			if !isAlpha(ch) {
    25  				err = ErrLabelInvalidCharacter
    26  				return
    27  			}
    28  			state = 1
    29  			continue
    30  		case 1:
    31  			if !(isNameSymbol(ch) || isAlpha(ch)) {
    32  				err = ErrLabelInvalidCharacter
    33  				return
    34  			}
    35  			if pos == valueLen-2 {
    36  				state = 0
    37  			}
    38  			continue
    39  		}
    40  	}
    41  	return
    42  }