Skip to content

JSON schema validator #280

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
omar-bb opened this issue Oct 27, 2020 · 6 comments
Closed

JSON schema validator #280

omar-bb opened this issue Oct 27, 2020 · 6 comments

Comments

@omar-bb
Copy link

omar-bb commented Oct 27, 2020

JSON schema validator

  • Validating JSON documents based on a JSON Schema
  • (Check if the user's input matches program expectations in a simpler way and avoid if statements overload)

What other libraries (C++ or other) have this feature?

Include a code fragment with sample data that illustrates the use of this feature

{
  "type": "object",
  "properties": {
    "first_name": { "type": "string" },
    "last_name": { "type": "string" },
    "birthday": { "type": "string", "format": "date" },
    "address": {
      "type": "object",
      "properties": {
        "street_address": { "type": "string" },
        "city": { "type": "string" },
        "state": { "type": "string" },
        "country": { "type" : "string" }
      }
    }
  }
}
  • Would match this JSON document :
{
  "first_name": "George",
  "last_name": "Washington",
  "birthday": "22-02-1732",
  "address": {
    "street_address": "3200 Mount Vernon Memorial Highway",
    "city": "Mount Vernon",
    "state": "Virginia",
    "country": "United States"
  }
}
@danielaparker
Copy link
Owner

Okay, we'll add it to the list.

@danielaparker
Copy link
Owner

The jsonschema extension is now on master. Feedback appreciated.

@dineshkumar02
Copy link

@danielaparker ,

I am trying to use the JSON schema validator, by having the json schema is defined in another file, and referring the file,
and referring that file for the validation.

{
"title": "testing properties",
"type": "object",
"properties": {
"ReferenceToExternalSchema": {
"$ref": "other_file.json#/definitions/ExternalType"
}
}
}

The other_file.json is located in the same .cpp file, but while executing the code I am getting the below error.
Don't know how to load JSON Schema /other_file.json

Also, I have tested some other basic functionalities with the JSON schema validator, and it's working as expected.

@dineshkumar02
Copy link

I found the example, in the schema validator documentation.

https://github.com/danielaparker/jsoncons/blob/master/doc/ref/jsonschema/jsonschema.md

@danielaparker
Copy link
Owner

danielaparker commented Dec 27, 2020

To resolve a reference to an external JSONSchema document, the jsoncons implementation needs a URIResolver provided. The error message "Don't know how to load JSON Schema" suggests you haven't provided one. Compare with the example
Using a URIResolver to resolve references to schemas defined in external files and make sure that make_schema is provided with a second argument,

auto sch = jsonschema::make_schema(schema, resolver);

@dineshkumar02
Copy link

Thanks @danielaparker

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

3 participants