Einhugur JSON Plugin III for Xojo

EinhugurJSONIII.JSONSchemaValidator Class (console safe)

Class to validate JSON documents against validation schema.

Validators can be re-used if you reset them in between runs.

Object
   JSONSchemaValidator

class EinhugurJSONIII.JSONSchemaValidator

Constructors

JSONSchemaValidatorPrivate constructor.

Methods

GetErrorAsJSONStringGets error info as detailed JSON message.
GetErrorMessageGets error info as simple string message.
GetInvalidDocumentPointerGets invalid document pointer.
GetInvalidSchemaKeywordGets invalid schema keyword.
GetInvalidSchemaPointerGets invalid schema pointer.
ResetResets the validator so that it is ready for run on other JSON document or message.
ValidateValidates JSON document against given schema.

Examples


using EinhugurJSONIII

// First we read the schema (Scehma.json which is included with this project)
var schemaDoc as JSONDocument = new JSONDocument()

if schemaDoc.Parse(Schema) then
    // We build the schema - Note the built scema we can store and reuse
    var schema as JSONSchemaDocument = schemaDoc.BuildSchema()
   
    if schema <> nil then
       // We create SchemaValidator - validator can be re-used if resetting it in between runs with the Reset method.
       var validator as JSONSchemaValidator = schema.CreateValidator()
      
       var doc as JSONDocument = new JSONDocument()
      
       if not doc.Parse(GoodTest) then
          MessageBox "Could not parse the JSON file named GoodTest"
          return
       end if
      
       if validator.Validate(doc) then
          MessageBox "The file named GoodTest.json tested valid against the schema called Schema.json"
       else
          MessageBox "The file named GoodTest.json tested not valid against the schema called Schema.json"
         
          MessageBox "Document pointer:" + validator.GetInvalidDocumentPointer()
          MessageBox "Invalid schema keyword: "+ validator.GetInvalidSchemaKeyword()
          MessageBox "Invalid schema pointer: " + validator.GetInvalidSchemaPointer()
         
          MessageBox "Error message: " + validator.GetErrorMessage()
          MEssageBox "Full error rport: " + validator.GetErrorAsJSONString()
       end if
      
    else
       MessageBox "Could not build schema document"
    end if
   
else
    MessageBox "Could not read schema document"
end if32



In this case our validation schema was:
{
"$id": "https://example.com/person.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "Person",
"type": "object",
"required": [ "firstName", "lastName" ],
"properties": {
"firstName": {
"type": "string",
"description": "The person's first name."
},
"lastName": {
"type": "string",
"description": "The person's last name."
},
"age": {
"description": "Age in years which must be equal to or greater than zero.",
"type": "integer",
"minimum": 0
}
}
}


Example of json document that would pass the validation against this schema:
{
"firstName": "John",
"lastName": "Doe",
"age": 21
}


Example of json document that would fail the validation against this schema:
{
"lastName": "Doe",
"age": 21
}

Supported Platforms:

  • macOS Intel 32 bit
  • macOS Intel 64 bit
  • macOS Apple Silicon
  • Windows 32 bit
  • Windows 64 bit
  • Windows ARM 64 bit
  • Linux 32 bit
  • Linux 64 bit
  • Linux ARM 32 bit
  • Linux ARM 64 bit
  • iOS