Skip to content

Example and default values with dedicated named type enum #132

Answered by vearutop
pboguslawski asked this question in Q&A
Discussion options

You must be logged in to vote

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 and RawExposer.

So for this might be a good solution to define Preparer that sets all needed fields at once:

func (s SortDir) PrepareJSONSchema(schema *jsonschema.Schema) error {
	schema.Enum = []interface{}{"asc", "desc"}
	schema.WithDefault("asc")
	schema.WithExamples("desc")

	return nil
}

Enum can be combined with Preparer if that brings convenience.

In cases when direct import dependency on "github.com/swaggest/jsonschema-go" is unwanted (e.g. in …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@pboguslawski
Comment options

Answer selected by vearutop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants