Replies: 1 comment
-
you can add entire openapi document as resource. below is the example: package main
import (
"strings"
"github.com/santhosh-tekuri/jsonschema/v6"
)
func main() {
openapiJSON := `{
"components": {
"schemas":{
"cat": {
"type": "object",
"properties": {
"speak": { "const": "meow" }
},
"required": ["speak"]
},
"dog": {
"type": "object",
"properties": {
"speak": { "const": "bow" }
},
"required": ["speak"]
},
"pet": {
"oneOf": [
{ "$ref": "#/components/schemas/dog" },
{ "$ref": "#/components/schemas/cat" }
]
}
}
}
}`
c := jsonschema.NewCompiler()
data, err := jsonschema.UnmarshalJSON(strings.NewReader(openapiJSON))
if err != nil {
panic(err)
}
if err := c.AddResource("openapi.json", data); err != nil {
panic(err)
}
inst, err := jsonschema.UnmarshalJSON(strings.NewReader(`{"speak": "bow"}`))
if err != nil {
panic(err)
}
sch := c.MustCompile("openapi.json#/components/schemas/pet")
if err := sch.Validate(inst); err != nil {
panic(err)
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there! Maintainer of kin-openapi here,
I'm trying to use this jsonschema parser/validator but I fear the only way there is to add schemas is through
AddResource
which only takes in URLs (including file paths) that actually exist; andAny fragment in url is ignored.
its docs says.As you probably know, an OpenAPI document describes many schemas. So in my case I have potentially many schemas to compile. They may come from STDIN or from multiple places in the same file (usually identified through anchors) or from one or more files. Thus I have no general way of providing valid paths.
Here's a simplified example of my attempted usage:
I see that versions of this lib, before v6, also have this "must not include fragment" requirement for AddResource.
Is there any way around this lib's hard requirement to have a real/existing path to each schema?
I'm guessing the point of this is to craft detailed errors?
Couldn't JSON Pointer paths be enough?
Would it be possible to either drop that constraint, or use a well known placeholder?
Or to impl a
URLLoader
that has an underlying in-memoryio/fs.FS
somehow?Beta Was this translation helpful? Give feedback.
All reactions