Esempio n. 1
0
func (s *Schema) Generate(indent bool, valid bool) []byte {
	var (
		j   []byte
		err error
	)
	if indent {
		j, err = json.MarshalIndent(s, "", "    ")
	} else {
		j, err = json.Marshal(s)
	}
	if err != nil {
		s.AddError(err)
		return nil
	}
	if valid {
		doc, err := spec.New(j, "")
		if err != nil {
			s.AddError(err)
			return nil
		}
		result := validate.Spec(doc, strfmt.Default)
		if result != nil {
			for _, desc := range result.(*swaggererrors.CompositeError).Errors {
				s.AddError(fmt.Errorf("The swagger spec is invalid against swagger specification %s. %s", doc.Version(), desc.Error()))
			}
			return nil
		}
	}
	return j
}
Esempio n. 2
0
// validateSwagger validates that the given swagger object represents a valid Swagger spec.
func validateSwagger(swagger *genswagger.Swagger) {
	b, err := json.Marshal(swagger)
	Ω(err).ShouldNot(HaveOccurred())
	doc, err := spec.New(b, "")
	Ω(err).ShouldNot(HaveOccurred())
	err = validate.Spec(doc, strfmt.NewFormats())
	Ω(err).ShouldNot(HaveOccurred())
}
Esempio n. 3
0
// Execute validates the spec
func (c *ValidateSpec) Execute(args []string) error {
	if len(args) == 0 {
		return errors.New("The validate command requires the swagger document url to be specified")
	}

	swaggerDoc := args[0]
	specDoc, err := spec.Load(swaggerDoc)
	if err != nil {
		return nil
	}

	result := validate.Spec(specDoc, strfmt.Default)
	if result == nil {
		fmt.Printf("The swagger spec at %q is valid against swagger specification %s\n", swaggerDoc, specDoc.Version())
	} else {
		str := fmt.Sprintf("The swagger spec at %q is invalid against swagger specification %s. see errors :\n", swaggerDoc, specDoc.Version())
		for _, desc := range result.(*swaggererrors.CompositeError).Errors {
			str += fmt.Sprintf("- %s\n", desc)
		}
		return errors.New(str)
	}
	return nil
}