github.com/gocaveman/caveman@v0.0.0-20191211162744-0ddf99dbdf6e/valid/messages.go (about)

     1  package valid
     2  
     3  import "fmt"
     4  
     5  type Message struct {
     6  
     7  	// indicates which object by name
     8  	Object string `json:"object,omitempty"`
     9  
    10  	// for multiple this is the index, starting with zero
    11  	Index interface{} `json:"index,omitempty"`
    12  
    13  	// name of the field
    14  	FieldName string `json:"field_name,omitempty"`
    15  
    16  	// the code indicating what validation rule failed
    17  	Code interface{} `json:"code,omitempty"`
    18  
    19  	// the English language message describing the failure
    20  	Message string `json:"messsage,omitempty"`
    21  
    22  	// the additional data (if any), which can be used to learn more about the validation failure or for i18n translation
    23  	Data interface{} `json:"data,omitempty"`
    24  }
    25  
    26  type Messages []Message
    27  
    28  func (ms Messages) Error() string {
    29  	return fmt.Sprintf("%d validation message(s)", len(ms))
    30  	// b, err := json.Marshal(ms)
    31  	// if err != nil {
    32  	// 	return err.Error()
    33  	// }
    34  	// return string(b)
    35  }
    36  
    37  func (ms Messages) ContainsCode(code string) bool {
    38  	for _, m := range ms {
    39  		if m.Code == code {
    40  			return true
    41  		}
    42  	}
    43  	return false
    44  }