You've already forked nginx-proxy-manager
							
							
				mirror of
				https://github.com/NginxProxyManager/nginx-proxy-manager.git
				synced 2025-10-30 18:05:34 +03:00 
			
		
		
		
	
		
			
				
	
	
		
			47 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package http
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 
 | |
| 	"github.com/qri-io/jsonschema"
 | |
| 	"github.com/rotisserie/eris"
 | |
| )
 | |
| 
 | |
| var (
 | |
| 	// ErrInvalidJSON is an error for invalid json
 | |
| 	ErrInvalidJSON = eris.New("JSON is invalid")
 | |
| 	// ErrInvalidPayload is an error for invalid incoming data
 | |
| 	ErrInvalidPayload = eris.New("Payload is invalid")
 | |
| )
 | |
| 
 | |
| // ValidateRequestSchema takes a Schema and the Content to validate against it
 | |
| func ValidateRequestSchema(schema string, requestBody []byte) ([]jsonschema.KeyError, error) {
 | |
| 	var jsonErrors []jsonschema.KeyError
 | |
| 	var schemaBytes = []byte(schema)
 | |
| 
 | |
| 	// Make sure the body is valid JSON
 | |
| 	if !isJSON(requestBody) {
 | |
| 		return jsonErrors, ErrInvalidJSON
 | |
| 	}
 | |
| 
 | |
| 	rs := &jsonschema.Schema{}
 | |
| 	if err := json.Unmarshal(schemaBytes, rs); err != nil {
 | |
| 		return jsonErrors, err
 | |
| 	}
 | |
| 
 | |
| 	var validationErr error
 | |
| 	ctx := context.TODO()
 | |
| 	if jsonErrors, validationErr = rs.ValidateBytes(ctx, requestBody); len(jsonErrors) > 0 {
 | |
| 		return jsonErrors, validationErr
 | |
| 	}
 | |
| 
 | |
| 	// Valid
 | |
| 	return nil, nil
 | |
| }
 | |
| 
 | |
| func isJSON(bytes []byte) bool {
 | |
| 	var js map[string]interface{}
 | |
| 	return json.Unmarshal(bytes, &js) == nil
 | |
| }
 |