Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package jsonschema

import (
"maps"
"reflect"
"regexp"
"strings"

"github.com/go-json-experiment/json"
"github.com/go-json-experiment/json/jsontext"
Expand Down Expand Up @@ -108,6 +111,9 @@ type Schema struct {
ReadOnly *bool `json:"readOnly,omitempty"` // Indicates that the property is read-only.
WriteOnly *bool `json:"writeOnly,omitempty"` // Indicates that the property is write-only.
Examples []any `json:"examples,omitempty"` // Examples of the instance data that validates against this schema.

// Extra keywords not in specification
Extra map[string]any `json:"-"`
}

// newSchema parses JSON schema data and returns a Schema object.
Expand Down Expand Up @@ -528,6 +534,8 @@ func (s *Schema) MarshalJSON() ([]byte, error) {
result["const"] = s.Const.Value
}

maps.Copy(result, s.Extra)

return json.Marshal(result)
}

Expand Down Expand Up @@ -557,9 +565,38 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
if s.Const == nil {
s.Const = &ConstValue{}
}
return s.Const.UnmarshalJSON(constData)
err := s.Const.UnmarshalJSON(constData)
if err != nil {
return err
}
}

return s.collectExtraFields(data)
}

func (s *Schema) collectExtraFields(raw []byte) error {
extra := make(map[string]any)
err := json.Unmarshal(raw, &extra)
if err != nil {
return err
}

t := reflect.TypeOf(Schema{})
for i := range t.NumField() {
field := t.Field(i)
jsonTagValues := strings.Split(field.Tag.Get("json"), ",")
if len(jsonTagValues) == 0 {
continue
}
tagName := jsonTagValues[0]
if tagName == "" || tagName == "-" {
continue
}
delete(extra, tagName)
}
if len(extra) != 0 {
s.Extra = extra
}
return nil
}

Expand Down
70 changes: 70 additions & 0 deletions tests/extras_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tests

import (
"testing"

"github.com/go-json-experiment/json"
"github.com/kaptinlin/jsonschema"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSchemaWithExtraFields(t *testing.T) {
testCases := []struct {
name string
schemaJSON string
expectedSchema jsonschema.Schema
}{
{
name: "extra fields collected into Extra",
schemaJSON: `{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"x-component": {
"component1": 1,
"component2": "2",
"componentNested": {"nested": "value"},
"componentBool": true
},
"some-extra-component": true
}`,
expectedSchema: jsonschema.Schema{
Schema: "https://json-schema.org/draft/2020-12/schema",
Extra: map[string]any{
"x-component": map[string]any{
"component1": 1,
"component2": "2",
"componentNested": map[string]any{
"nested": "value",
},
"componentBool": true,
},
"some-extra-component": true,
},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var schema jsonschema.Schema
err := json.Unmarshal([]byte(tc.schemaJSON), &schema)
require.NoError(t, err, "Unmarshalling failed unexpectedly")
assert.Equal(t, tc.expectedSchema.ID, schema.ID)
assert.Equal(t, tc.expectedSchema.Schema, schema.Schema)
assert.Equal(t, tc.expectedSchema.Type, schema.Type)

// Now test marshaling back to JSON
marshaledJSON, err := json.Marshal(schema)
require.NoError(t, err, "Marshalling failed unexpectedly")

// Unmarshal marshaled JSON to verify it matches the original schema object
var reUnmarshaledSchema jsonschema.Schema
err = json.Unmarshal(marshaledJSON, &reUnmarshaledSchema)
require.NoError(t, err, "Unmarshalling the marshaled JSON failed")
assert.Equal(t, schema, reUnmarshaledSchema, "Re-unmarshaled schema does not match the original")

// Check if the marshaled JSON matches the original JSON input
assert.JSONEq(t, tc.schemaJSON, string(marshaledJSON), "The marshaled JSON should match the original input JSON")
})
}
}