Skip to content

What would be the best way to override a type? #89

Closed Answered by danaradg
danaradg asked this question in Q&A
Discussion options

You must be logged in to vote

OK, I found a way using Reflector.Mapper:

reflector.Mapper = func(t reflect.Type) *jsonschema.Schema {
  if reflect.TypeOf(intstr.IntOrString{}) == t {
    return *jsonschema.Schema{
		AnyOf: []*jsonschema.Schema{
			{
				Type: "string",
			},
			{
				Type: "integer",
			},
		},
	}
  }

  return nil
}

One thing I noted is that I can't set the Type field to an array which doesn't match RFC draft-bhutton-json-schema-validation-00, section 6 as documented in the jsonschema.Schema structure. As a result I get

"anyOf": [
  {
    "type": "string"
  },
  {
    "type": "integer"
  }
]

instead of

"type": ["integer", "string"]

but this is good enough for now.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by danaradg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant