-
Notifications
You must be signed in to change notification settings - Fork 136
feat(type): marshals and unmarshals multivalued type fields #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "properties": { | ||
| "other": { | ||
| "type": "string" | ||
| }, | ||
| "withdrawals": { | ||
| "type": [ | ||
| "array", | ||
| "null" | ||
| ] | ||
| } | ||
| }, | ||
| "required": [ | ||
| "withdrawals" | ||
| ], | ||
| "description": "test of Schema with multivalued property Type" | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package jsonschema | |||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| orderedmap "github.com/wk8/go-ordered-map/v2" | ||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,7 +41,7 @@ type Schema struct { | |||||||||||||||||||||||||||||||||||||||||||||
| AdditionalProperties *Schema `json:"additionalProperties,omitempty"` // section 10.3.2.3 | ||||||||||||||||||||||||||||||||||||||||||||||
| PropertyNames *Schema `json:"propertyNames,omitempty"` // section 10.3.2.4 | ||||||||||||||||||||||||||||||||||||||||||||||
| // RFC draft-bhutton-json-schema-validation-00, section 6 | ||||||||||||||||||||||||||||||||||||||||||||||
| Type string `json:"type,omitempty"` // section 6.1.1 | ||||||||||||||||||||||||||||||||||||||||||||||
| Type Type `json:"type,omitempty"` // section 6.1.1 | ||||||||||||||||||||||||||||||||||||||||||||||
| Enum []any `json:"enum,omitempty"` // section 6.1.2 | ||||||||||||||||||||||||||||||||||||||||||||||
| Const any `json:"const,omitempty"` // section 6.1.3 | ||||||||||||||||||||||||||||||||||||||||||||||
| MultipleOf json.Number `json:"multipleOf,omitempty"` // section 6.2.1 | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -92,3 +93,73 @@ var ( | |||||||||||||||||||||||||||||||||||||||||||||
| // http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26 | ||||||||||||||||||||||||||||||||||||||||||||||
| // RFC draft-wright-json-schema-validation-00, section 5.26 | ||||||||||||||||||||||||||||||||||||||||||||||
| type Definitions map[string]*Schema | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Type represents the property types allowed by the JSONSchema specification,s | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| // Type represents the property types allowed by the JSONSchema specification,s | |
| // Type represents the property types allowed by the JSONSchema specification's |
Copilot
AI
Apr 23, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Type methods use pointer receivers, which makes the API awkward to use: constants and temporary values (e.g. TypeArray.IsMultivalued() or NewMultivaluedType(...).IsMultivalued()) are not addressable, so these calls won’t compile. Consider switching IsMultivalued and MarshalJSON to value receivers (func (t Type) ...) while keeping UnmarshalJSON as a pointer receiver.
| func (t *Type) IsMultivalued() bool { | |
| return strings.Contains(string(*t), ",") | |
| } | |
| // MarshalJSON implements json.Marshaler. | |
| func (t *Type) MarshalJSON() ([]byte, error) { | |
| if t.IsMultivalued() { | |
| return json.Marshal(strings.Split(string(*t), ",")) | |
| } | |
| return json.Marshal(string(*t)) | |
| func (t Type) IsMultivalued() bool { | |
| return strings.Contains(string(t), ",") | |
| } | |
| // MarshalJSON implements json.Marshaler. | |
| func (t Type) MarshalJSON() ([]byte, error) { | |
| if t.IsMultivalued() { | |
| return json.Marshal(strings.Split(string(t), ",")) | |
| } | |
| return json.Marshal(string(t)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing Schema.Type from string to the new Type defined type is a compile-time breaking API change for callers that previously assigned a string variable (not a string literal) to Schema.Type. If the intent is to remain source-compatible in v1, consider an approach that keeps the public field as string (or adds a new field) and uses custom marshal/unmarshal to support both string and []string in JSON, or clearly document this as a breaking change requiring a major version bump.