-
Notifications
You must be signed in to change notification settings - Fork 93
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
About using jsonschema.CompileString #98
Comments
the url you pass to jsonschema.CompileString("schema.json", schemaString)
in above error message you can see also in then you might ask, why can't we just internally use some default say jsonschema.CompileString(`{
"type": "object",
"properties": {
"p": { "$ref": "schema.json" }
}
}`) assume that we have I hope I am able to explain why you need to pass URL parameter to |
@santhosh-tekuri Thank you very much! I get it. |
What happens when that URL is a valid URL? E.g., if I do this:
Then my json sample seems to pass my schema validation when it should fail. But if I introduce a typo,
I'm wondering if this string is just for local error reporting, what's happening here? (v5.3.1). |
it is treated as if the schema is downloaded from that url. for example if you have "$ref" with same url, then your schema is used in that place |
$ref is not used in the document at all. Are you able to help me understand why the behaviour of the validation changes when we go from Here's a minimal sample (https://go.dev/play/p/P7RqtVbTT_e), where schema2 fails as expected but schema1 passes: package main
import (
"encoding/json"
"log"
"github.com/santhosh-tekuri/jsonschema/v5"
)
var sampleSchema string = `{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
}
},
"required": ["id", "name"]
}`
var sampleJSON string = `{
"id": 456
}`
func main() {
schema1, err := jsonschema.CompileString("http://json-schema.org/schema", sampleSchema)
if err != nil {
log.Fatalf("%#v", err)
}
schema2, err := jsonschema.CompileString("http://json-schema.orrg/schema", sampleSchema)
if err != nil {
log.Fatalf("%#v", err)
}
var v interface{}
if err := json.Unmarshal([]byte(sampleJSON), &v); err != nil {
log.Fatalf("%#v", err)
}
if err = schema1.Validate(v); err != nil {
log.Printf("Failed validation against schema 1: %s\n", err)
}
if err = schema2.Validate(v); err != nil {
log.Printf("Failed validation against schema 2: %s\n", err)
}
} Output:
|
@saward From the example, it is easier to understand your question.
standard meta schemas are treated superior to use added resources and get precedence always. when you make a spell mistake, then it longer matches with any standard metaschema, so it is actually compiling the schemaString you have given. |
Hello, I have a question about using jsonschema: Why do I need to pass a URL parameter when calling CompileString?
The text was updated successfully, but these errors were encountered: