Example and default values with dedicated named type enum #132
-
According to https://pkg.go.dev/github.com/swaggest/jsonschema-go#Enum one can use (1) dedicated named type or (2) field tag to set up enum. With (2) it's possible do define default value for openapi.json; with
one gets
Is it possibe get
one gets stuff without
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Named types can express custom schema with help of special interfaces, Most important are So for this might be a good solution to define func (s SortDir) PrepareJSONSchema(schema *jsonschema.Schema) error {
schema.Enum = []interface{}{"asc", "desc"}
schema.WithDefault("asc")
schema.WithExamples("desc")
return nil
}
In cases when direct import dependency on func (s SortDir) JSONSchemaBytes() ([]byte, error) {
return []byte(`{"type":"string","enum":["asc","desc"],"default":"asc","examples":["desc"]}`), nil
} Setting up schema with field tags is usually the best way to describe properties, but is less optimal for type-based descriptions (as it can lead to code duplication if same type needs to be described in multiple properties). |
Beta Was this translation helpful? Give feedback.
Named types can express custom schema with help of special interfaces,
Enum
is one of such interfaces.Other are listed here: https://github.com/swaggest/jsonschema-go/#implementing-interfaces-on-a-type.
Most important are
Preparer
,Exposer
andRawExposer
.So for this might be a good solution to define
Preparer
that sets all needed fields at once:Enum
can be combined withPreparer
if that brings convenience.In cases when direct import dependency on
"github.com/swaggest/jsonschema-go"
is unwanted (e.g. in …