Skip to content
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

JSchema.Load is not resolving references #303

Open
markericson opened this issue Oct 16, 2022 · 1 comment
Open

JSchema.Load is not resolving references #303

markericson opened this issue Oct 16, 2022 · 1 comment

Comments

@markericson
Copy link

I started off simply trying to load a schema with references using JSchema.Load() and JSchema.Load() fails for every scenario with the error: "Unhandled exception. Newtonsoft.Json.Schema.JSchemaReaderException: Could not resolve schema reference './Common.schema.json#/definitions/Statement'."

I have tried the following three approaches without success.

  1. a simple call to JSchema.Load() without a JsonSchemaSettings.BaseUri hoping that the library would look at the current working directory to find the files
  2. providing a JSchemaReaderSettings to JSchema.Load() with a file scheme Uri to the path of my schema files (e.g. file://Users/.../testschema/ )
    • note: '...' is simply redacting a very long Uri
    • referenced file successfully loads in a browser using a full path file://Users/.../testschema/Common.schema.json
  3. hosting my testschema folder with a simple static web server and using the baseUri http://localhost:8080/

Test.schema.json

{
  "schema": "https://json-schema.org/draft-4/schema",
  "title": "AccountInfo",
  "type": "object",
  "properties": {
    "accountNumber": {
      "type": "string"
    },
    "accountTerms": {
      "ref$": "Common.schema.json#/definitions/AccountsTypeEnum"
    },
    "stmtHistory": {
      "type": "array",
      "items": {
        "$ref": "Common.schema.json#/definitions/Statement"
      }
    }
  }
}

Common.schema.json

{
  "schema": "https://json-schema.org/draft-4/schema",
  "definitions": {
    "AccountTermsEnum": {
      "type": "string",
      "enum": ["Prepaid","Net10","Net30","5/15 Net 30","2/10 Net 30"]
    },
    "Statement": {
        "type": "object",
        "properties": {
          "createdDate": {"type": "string", "format": "date"},
          "totalBalance": {"type": "number"},
          "overdueBalance":  {"type": "number"}
        }
      }
  }
}
@markericson
Copy link
Author

I discovered that when using the file:// scheme URI as BaseUri, I needed to set JSchemaUrlResolver in the JSchemaReaderSettings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant