google / google/jsonschema-go

Proposal/Request: Expose `False()` and `True()` constructors

Open
#13 1 comment 2 reactions 0 assignees View on GitHub
Extra Small P3
Dominant language
Go
Stars
478
Forks
37
PR merge metrics
No merged PRs in 30d

Description

**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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.