// validateAgainstSchema validates the input setter value against user provided
// openAI schema
validateAgainstSchema validates the input setter value against user provided
openAI schema
// validateAgainstSchema validates the input setter value against user provided // openAI schema
func validateAgainstSchema(ext *CliExtension, sch *spec.Schema) error {
	fixSchemaTypes(sch)
	sc := spec.Schema{}
	sc.Properties = map[string]spec.Schema{}
	sc.Properties[ext.Setter.Name] = *sch
	var inputYAML string
	if len(ext.Setter.ListValues) > 0 {
		var tmplText string
		if sch.Items != nil && sch.Items.Schema != nil &&
			shouldQuoteSetterValue(ext.Setter.ListValues, sch.Items.Schema.Type) {
			tmplText = `{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{printf "- %q\n" .}}{{end}}{{end}}`
		} else {
			tmplText = `{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}`
		}
		tmpl, err := template.New("validator").Parse(tmplText)
		if err != nil {
			return err
		}
		var builder strings.Builder
		err = tmpl.Execute(&builder, map[string]interface{}{
			"key":    ext.Setter.Name,
			"values": ext.Setter.ListValues,
		})
		if err != nil {
			return err
		}
		inputYAML = builder.String()
	} else {
		var format string
		if shouldQuoteSetterValue([]string{ext.Setter.Value}, sch.Type) {
			format = "%s: \"%s\""
		} else {
			format = "%s: %s"
		}
		inputYAML = fmt.Sprintf(format, ext.Setter.Name, ext.Setter.Value)
	}
	input := map[string]interface{}{}
	err := goyaml.Unmarshal([]byte(inputYAML), &input)
	if err != nil {
		return err
	}
	err = validate.AgainstSchema(&sc, input, strfmt.Default)
	if err != nil {
		return errors.Errorf("The input value doesn't validate against provided OpenAPI schema: %v\n", err.Error())
	}
	return nil
}
func validateAgainstSchema ( ext * CliExtension , sch * spec . Schema ) error { fixSchemaTypes ( sch ) sc := spec . Schema { } sc . Properties = map [ string ] spec . Schema { } sc . Properties [ ext . Setter . Name ] = * sch var inputYAML string if len ( ext . Setter . ListValues ) > 0 { var tmplText string if sch . Items != nil && sch . Items . Schema != nil && shouldQuoteSetterValue ( ext . Setter . ListValues , sch . Items . Schema . Type ) { tmplText = `{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{printf "- %q\n" .}}{{end}}{{end}}` } else { tmplText = `{{.key}}:{{block "list" .values}}{{"\n"}}{{range .}}{{println "-" .}}{{end}}{{end}}` } tmpl , err := template . New ( " " ) . Parse ( tmplText ) if err != nil { return err } var builder strings . Builder err = tmpl . Execute ( & builder , map [ string ] interface { } { " " : ext . Setter . Name , " " : ext . Setter . ListValues , } ) if err != nil { return err } inputYAML = builder . String ( ) } else { var format string if shouldQuoteSetterValue ( [ ] string { ext . Setter . Value } , sch . Type ) { format = " \" \" " } else { format = " " } inputYAML = fmt . Sprintf ( format , ext . Setter . Name , ext . Setter . Value ) } input := map [ string ] interface { } { } err := goyaml . Unmarshal ( [ ] byte ( inputYAML ) , & input ) if err != nil { return err } err = validate . AgainstSchema ( & sc , input , strfmt . Default ) if err != nil { return errors . Errorf ( " \n " , err . Error ( ) ) } return nil }
validateAgainstSchema validates the input setter value against user provided
openAI schema
validateAgainstSchema validates the input setter value against user provided openAI schema
// tmplText contains the template we will use to produce a yaml // document that we can use for validation. // If string is one of the legal types for the value, we // output it with quotes in the yaml document to make sure it // is later parsed as a string. // If string is not specifically set as the type, we just // let the yaml unmarshaller detect the correct type. Thus, we // do not add quotes around the value. // Only add quotes around the value is string is one of the // types in the schema.