Skip to content

Proposal/Request: Expose False() and True() constructors #13

@loreto

Description

@loreto

Is your feature request related to a problem? Please describe.
While using schema inference is nice, sometimes I need to define a jsonschema "manually" in Go code. In those cases, it's common to need to define certain fields
of the schema as either true or false.

With the current interface, this looks like this:

schema := &jsonschema.Schema{
	Type: "object",
	Properties: map[string]*jsonschema.Schema{
		"field": {
			Type: "string",
		},
	},
	// We want AdditionalProperties: false
	AdditionalProperties: &jsonschema.Schema{Not: &jsonschema.Schema{}},
},

Not only is that hard to read, but it's not obvious to most users that &jsonschema.Schema{Not: &jsonschema.Schema{}} is equivalent to false in the spec.

Describe the solution you'd like
I think it'd be great to have two publicly available constructors:

func True() *Schema {
	return &Schema{}
}

func False() *Schema {
	return &Schema{Not: &Schema{}}
}

That would make it possible to write:

schema := &jsonschema.Schema{
	Type: "object",
	Properties: map[string]*jsonschema.Schema{
		"field": {
			Type: "string",
		},
	},
	AdditionalProperties: jsonschema.False(),
},

Describe alternatives you've considered
For now I've implemented True() and False() in my own code, but I'm hoping
these can be part of jsonschema.

Additional context
I'm happy to put together the PR and send it for review if this proposal is approved.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions