github.com/kaisawind/go-swagger@v0.19.0/cmd/swagger/commands/validate.go (about)

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package commands
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"log"
    21  
    22  	"github.com/go-openapi/loads"
    23  	"github.com/go-openapi/strfmt"
    24  	"github.com/go-openapi/validate"
    25  )
    26  
    27  const (
    28  	// Output messages
    29  	missingArgMsg  = "The validate command requires the swagger document url to be specified"
    30  	validSpecMsg   = "\nThe swagger spec at %q is valid against swagger specification %s\n"
    31  	invalidSpecMsg = "\nThe swagger spec at %q is invalid against swagger specification %s.\nSee errors below:\n"
    32  	warningSpecMsg = "\nThe swagger spec at %q showed up some valid but possibly unwanted constructs."
    33  )
    34  
    35  // ValidateSpec is a command that validates a swagger document
    36  // against the swagger specification
    37  type ValidateSpec struct {
    38  	// SchemaURL string `long:"schema" description:"The schema url to use" default:"http://swagger.io/v2/schema.json"`
    39  	SkipWarnings bool `long:"skip-warnings" description:"when present will not show up warnings upon validation"`
    40  	StopOnError  bool `long:"stop-on-error" description:"when present will not continue validation after critical errors are found"`
    41  }
    42  
    43  // Execute validates the spec
    44  func (c *ValidateSpec) Execute(args []string) error {
    45  	if len(args) == 0 {
    46  		return errors.New(missingArgMsg)
    47  	}
    48  
    49  	swaggerDoc := args[0]
    50  
    51  	specDoc, err := loads.Spec(swaggerDoc)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	// Attempts to report about all errors
    57  	validate.SetContinueOnErrors(!c.StopOnError)
    58  
    59  	v := validate.NewSpecValidator(specDoc.Schema(), strfmt.Default)
    60  	result, _ := v.Validate(specDoc) // returns fully detailed result with errors and warnings
    61  
    62  	if result.IsValid() {
    63  		log.Printf(validSpecMsg, swaggerDoc, specDoc.Version())
    64  	}
    65  	if result.HasWarnings() {
    66  		log.Printf(warningSpecMsg, swaggerDoc)
    67  		if !c.SkipWarnings {
    68  			log.Printf("See warnings below:\n")
    69  			for _, desc := range result.Warnings {
    70  				log.Printf("- WARNING: %s\n", desc.Error())
    71  			}
    72  		}
    73  	}
    74  	if result.HasErrors() {
    75  		str := fmt.Sprintf(invalidSpecMsg, swaggerDoc, specDoc.Version())
    76  		for _, desc := range result.Errors {
    77  			str += fmt.Sprintf("- %s\n", desc.Error())
    78  		}
    79  		return errors.New(str)
    80  	}
    81  
    82  	return nil
    83  }