invopop / invopop/jsonschema

Override jsonschema property

Open
#63 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
952
Forks
136
PR merge metrics
No merged PRs in 30d

Description

Hi, I would like to know how to override/remove some `jsonschema` property.

In the example below, the `MAC` attribute is a slice of bytes and is represented as `{"type":"string","contentEncoding":"base64"}` in the generated schema. However, in my case I made a custom marshaller so as to turn this slice into a string. So I would like to remove the `contentEncoding` attribute in the schema.

I have tried to change its value but it is not reflected in the schema.

```go
package main

import (
"encoding/json"
"fmt"
"net"

"github.com/invopop/jsonschema"
)

type NIC struct {
Name string `json:"name,omitempty"`
MAC net.HardwareAddr `json:"mac,omitempty" jsonschema:"contentEncoding=quoted-printable"`
}

func (nic *NIC) MarshalJSON() ([]byte, error) {
type Alias NIC

mac := ""
if nic.MAC != nil {
mac = nic.MAC.String()
}

return json.Marshal(&struct {
MAC string `json:"mac,omitempty"`
*Alias
}{
MAC: mac,
Alias: (*Alias)(nic),
})
}

func main() {
schema := jsonschema.Reflect(&NIC{})
data, _ := schema.MarshalJSON()
fmt.Println(string(data))
}
```
```shell
# output
{"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/$defs/NIC","$defs":{"HardwareAddr":{"type":"string","contentEncoding":"base64"},"NIC":{"properties":{"name":{"type":"string"},"mac":{"$ref":"#/$defs/HardwareAddr"}},"additionalProperties":false,"type":"object"}}}
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.