github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/jsonschema/validator.go (about)

     1  package jsonschema
     2  
     3  import (
     4  	"encoding/json"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/go-multierror"
     8  	"github.com/pkg/errors"
     9  	"github.com/xeipuuv/gojsonschema"
    10  )
    11  
    12  type validator struct {
    13  	schema *gojsonschema.Schema
    14  }
    15  
    16  // ValidationResult missing godoc
    17  type ValidationResult struct {
    18  	Valid bool
    19  	Error error
    20  }
    21  
    22  // NewValidatorFromStringSchema missing godoc
    23  func NewValidatorFromStringSchema(jsonSchema string) (*validator, error) {
    24  	if jsonSchema == "" {
    25  		return &validator{schema: nil}, nil
    26  	}
    27  
    28  	sl := gojsonschema.NewStringLoader(jsonSchema)
    29  	schema, err := gojsonschema.NewSchema(sl)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	return &validator{
    35  		schema: schema,
    36  	}, nil
    37  }
    38  
    39  // NewValidatorFromRawSchema missing godoc
    40  func NewValidatorFromRawSchema(jsonSchema interface{}) (*validator, error) {
    41  	if jsonSchema == nil {
    42  		return &validator{}, nil
    43  	}
    44  
    45  	sl := gojsonschema.NewGoLoader(jsonSchema)
    46  	schema, err := gojsonschema.NewSchema(sl)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  
    51  	return &validator{
    52  		schema: schema,
    53  	}, nil
    54  }
    55  
    56  // ValidateString missing godoc
    57  func (v *validator) ValidateString(json string) (ValidationResult, error) {
    58  	if v.schema == nil {
    59  		return ValidationResult{
    60  			Valid: true,
    61  			Error: nil,
    62  		}, nil
    63  	}
    64  
    65  	jsonLoader := gojsonschema.NewStringLoader(json)
    66  	result, err := v.schema.Validate(jsonLoader)
    67  	if err != nil {
    68  		return ValidationResult{}, err
    69  	}
    70  
    71  	var validationError *multierror.Error
    72  	for _, e := range result.Errors() {
    73  		validationError = multierror.Append(validationError, errors.New(e.String()))
    74  	}
    75  
    76  	if validationError != nil {
    77  		validationError.ErrorFormat = func(i []error) string {
    78  			var s []string
    79  			for _, v := range i {
    80  				s = append(s, v.Error())
    81  			}
    82  			return strings.Join(s, ", ")
    83  		}
    84  	}
    85  
    86  	return ValidationResult{
    87  		Valid: result.Valid(),
    88  		Error: validationError.ErrorOrNil(),
    89  	}, nil
    90  }
    91  
    92  // ValidateRaw missing godoc
    93  func (v *validator) ValidateRaw(value interface{}) (ValidationResult, error) {
    94  	if v.schema == nil {
    95  		return ValidationResult{
    96  			Valid: true,
    97  			Error: nil,
    98  		}, nil
    99  	}
   100  
   101  	valueMarshalled, err := json.Marshal(value)
   102  	if err != nil {
   103  		return ValidationResult{}, err
   104  	}
   105  
   106  	return v.ValidateString(string(valueMarshalled))
   107  }