github.com/gogf/gf/v2@v2.7.4/util/gvalid/internal/builtin/builtin_json.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package builtin
     8  
     9  import (
    10  	"errors"
    11  
    12  	"github.com/gogf/gf/v2/internal/json"
    13  )
    14  
    15  // RuleJson implements `json` rule:
    16  // JSON.
    17  //
    18  // Format: json
    19  type RuleJson struct{}
    20  
    21  func init() {
    22  	Register(RuleJson{})
    23  }
    24  
    25  func (r RuleJson) Name() string {
    26  	return "json"
    27  }
    28  
    29  func (r RuleJson) Message() string {
    30  	return "The {field} value `{value}` is not a valid JSON string"
    31  }
    32  
    33  func (r RuleJson) Run(in RunInput) error {
    34  	if json.Valid(in.Value.Bytes()) {
    35  		return nil
    36  	}
    37  	return errors.New(in.Message)
    38  }