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
Methods
Examples
using EinhugurJSONIII
var schemaDoc as JSONDocument = new JSONDocument()
if schemaDoc.Parse(Schema) then
var schema as JSONSchemaDocument = schemaDoc.BuildSchema()
if schema <> nil then
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 bitmacOS Intel 64 bitmacOS Apple SiliconWindows 32 bitWindows 64 bitWindows ARM 64 bitLinux 32 bitLinux 64 bitLinux ARM 32 bitLinux ARM 64 bitiOS