Skip to content

Commit

Permalink
feat: support json.RawMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Apr 2, 2024
1 parent 73362ec commit 0fc6b09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 6 additions & 3 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ const (

// Special JSON Schema formats.
var (
timeType = reflect.TypeOf(time.Time{})
ipType = reflect.TypeOf(net.IP{})
urlType = reflect.TypeOf(url.URL{})
timeType = reflect.TypeOf(time.Time{})
ipType = reflect.TypeOf(net.IP{})
urlType = reflect.TypeOf(url.URL{})
rawMessageType = reflect.TypeOf(json.RawMessage{})
)

func deref(t reflect.Type) reflect.Type {
Expand Down Expand Up @@ -597,6 +598,8 @@ func SchemaFromType(r Registry, t reflect.Type) *Schema {
return &Schema{Type: TypeString, Format: "uri"}
case ipType:
return &Schema{Type: TypeString, Format: "ipv4"}
case rawMessageType:
return &Schema{}
}

minZero := 0.0
Expand Down
5 changes: 5 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ func TestSchema(t *testing.T) {
input: net.IPv4(127, 0, 0, 1),
expected: `{"type": "string", "format": "ipv4"}`,
},
{
name: "json.RawMessage",
input: &json.RawMessage{},
expected: `{}`,
},
{
name: "bytes",
input: []byte("test"),
Expand Down
7 changes: 7 additions & 0 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,13 @@ var validateTests = []struct {
},
errs: []string{"expected object with at most 1 properties"},
},
{
name: "json.RawMessage success",
typ: reflect.TypeOf(struct {
Value json.RawMessage `json:"value"`
}{}),
input: map[string]any{"value": map[string]any{"some": []any{"thing"}}},
},
{
name: "object struct success",
typ: reflect.TypeOf(struct{}{}),
Expand Down

0 comments on commit 0fc6b09

Please sign in to comment.