Add schema validator, make schemas root relative.
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5b875f7..041d88c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,7 +26,7 @@
 # Add library and test executable.
 file(GLOB SectionSources sections/*.c)
 file(GLOB EDKSources edk/*.c)
-add_library(CPERParseLibrary STATIC cper-parse.c ir-parse.c cper-utils.c ${SectionSources} ${EDKSources})
+add_library(CPERParseLibrary STATIC cper-parse.c ir-parse.c cper-utils.c json-schema.c json-schema.h ${SectionSources} ${EDKSources})
 add_executable(CPERParseTest testing/cper-test.c)
 
 # Link library.
diff --git a/json-schema.c b/json-schema.c
new file mode 100644
index 0000000..c86ce4c
--- /dev/null
+++ b/json-schema.c
@@ -0,0 +1,237 @@
+/**
+ * A very basic, non-complete implementation of a validator for the JSON Schema specification,
+ * for validating CPER-JSON.
+ * 
+ * Author: Lawrence.Tang@arm.com
+ **/
+
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <libgen.h>
+#include <limits.h>
+#include "json.h"
+#include "json-schema.h"
+#include "edk/BaseTypes.h"
+
+//Private pre-definitions.
+int validate_field(const char* name, json_object* schema, json_object* object, char* error_message);
+int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message);
+int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message);
+int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message);
+int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message);
+
+//Validates a single JSON object against a provided schema file, returning 1 on success and 0 on failure to validate.
+//Error message space must be allocated prior to call.
+int validate_schema_from_file(const char* schema_file, json_object* object, char* error_message)
+{
+    //Load schema IR from file.
+    json_object* schema_ir = json_object_from_file(schema_file);
+    if (schema_ir == NULL)
+    {
+        sprintf(error_message, "Failed to load schema from file '%s'.", schema_file);
+        return 0;
+    }
+
+    //Get the directory of the file.
+    char* schema_file_copy = malloc(strlen(schema_file) + 1);
+    strcpy(schema_file_copy, schema_file);
+    char* schema_dir = dirname(schema_file_copy);
+
+    int result = validate_schema(schema_ir, schema_dir, object, error_message);
+
+    //Free memory from directory call.
+    free(schema_file_copy);
+
+    return result;
+}
+
+//Validates a single JSON object against a provided schema, returning 1 on success and 0 on failure to validate.
+//Error message space must be allocated prior to call.
+//If the schema does not include any other sub-schemas using "$ref", then leaving schema_directory as NULL is valid.
+int validate_schema(json_object* schema, char* schema_directory, json_object* object, char* error_message)
+{
+    //Check that the schema version is the same as this validator.
+    json_object* schema_ver = json_object_object_get(schema, "$schema");
+    if (schema_ver == NULL || strcmp(json_object_get_string(schema_ver), JSON_SCHEMA_VERSION))
+    {
+        sprintf(error_message, "Provided schema is not of the same version that is referenced by this validator, or is not a schema.");
+        return 0;
+    }
+
+    //Change current directory into the schema directory.
+    char* original_cwd = malloc(PATH_MAX);
+    if (getcwd(original_cwd, PATH_MAX) == NULL)
+    {
+        sprintf(error_message, "Failed fetching the current directory.");
+        return 0;
+    }
+    if (chdir(schema_directory))
+    {
+        sprintf(error_message, "Failed to chdir into schema directory.");
+        return 0;
+    }
+
+    //Parse the top level structure appropriately.
+    int result = validate_field("parent", schema, object, error_message);
+
+    //Change back to original CWD.
+    chdir(original_cwd);
+    free(original_cwd);
+
+    return result;
+}
+
+//Validates a single JSON field given a schema/object.
+int validate_field(const char* field_name, json_object* schema, json_object* object, char* error_message)
+{
+    //If there is a "$ref" field, attempt to load the referenced schema.
+    json_object* ref_schema = json_object_object_get(schema, "$ref");
+    if (ref_schema != NULL && json_object_get_type(ref_schema) == json_type_string)
+    {
+        //Attempt to load. If loading fails, report error.
+        const char* ref_path = json_object_get_string(ref_schema);
+        schema = json_object_from_file(ref_path);
+        if (schema == NULL)
+        {
+            sprintf(error_message, "Failed to open referenced schema file '%s'.", ref_path);
+            return 0;
+        }
+    }
+
+    //Get the schema field type.
+    json_object* desired_field_type = json_object_object_get(schema, "type");
+    if (desired_field_type == NULL || !json_object_is_type(desired_field_type, json_type_string))
+    {
+        sprintf(error_message, "Desired field type not provided within schema/is not a string for field '%s' (schema violation).", field_name);
+        return 0;
+    }
+
+    //Check the field types are actually equal.
+    const char* desired_field_type_str = json_object_get_string(desired_field_type);
+    if (!(
+        (!strcmp(desired_field_type_str, "object") && json_object_is_type(object, json_type_object))
+        || (!strcmp(desired_field_type_str, "array") && json_object_is_type(object, json_type_array))
+        || (!strcmp(desired_field_type_str, "integer") && json_object_is_type(object, json_type_int))
+        || (!strcmp(desired_field_type_str, "string") && json_object_is_type(object, json_type_string))
+        || (!strcmp(desired_field_type_str, "boolean") && json_object_is_type(object, json_type_boolean))
+        || (!strcmp(desired_field_type_str, "double") && json_object_is_type(object, json_type_double))
+    ))
+    {
+        sprintf(error_message, "Field type match failed for field '%s'.", field_name);
+        return 0;
+    }
+
+    //Switch and validate each type in turn.
+    switch (json_object_get_type(object))
+    {
+        case json_type_int:
+            return validate_integer(field_name, schema, object, error_message);
+        case json_type_string:
+            return validate_string(field_name, schema, object, error_message);
+        case json_type_object:
+            return validate_object(field_name, schema, object, error_message);
+        case json_type_array:
+            return validate_object(field_name, schema, object, error_message);
+
+        //We don't perform extra validation on this type.
+        default:
+            return 1;
+    }
+}
+
+//Validates a single integer value according to the given specification.
+int validate_integer(const char* field_name, json_object* schema, json_object* object, char* error_message)
+{
+    //Is there a minimum/maximum specified? If so, check those.
+    //Validate minimum.
+    json_object* min_value = json_object_object_get(schema, "minimum");
+    if (min_value != NULL && json_object_is_type(min_value, json_type_int))
+    {
+        int min_value_int = json_object_get_int(min_value);
+        if (json_object_get_uint64(object) < min_value_int)
+        {
+            sprintf(error_message, "Failed to validate integer field '%s'. Value was below minimum of %d.", field_name, min_value_int);
+            return 0;
+        }
+    }
+
+    //Validate maximum.
+    json_object* max_value = json_object_object_get(schema, "maximum");
+    if (max_value != NULL && json_object_is_type(max_value, json_type_int))
+    {
+        int max_value_int = json_object_get_int(max_value);
+        if (json_object_get_uint64(object) > max_value_int)
+        {
+            sprintf(error_message, "Failed to validate integer field '%s'. Value was above maximum of %d.", field_name, max_value_int);
+            return 0;
+        }
+    }
+
+    return 1;
+}
+
+//Validates a single string value according to the given specification.
+int validate_string(const char* field_name, json_object* schema, json_object* object, char* error_message)
+{
+    //todo: if there is a "pattern" field, verify the string with RegEx.
+    return 1;
+}
+
+//Validates a single object value according to the given specification.
+int validate_object(const char* field_name, json_object* schema, json_object* object, char* error_message)
+{
+    //Are there a set of "required" fields? If so, check they all exist.
+    json_object* required_fields = json_object_object_get(schema, "required");
+    if (required_fields != NULL && json_object_get_type(required_fields) == json_type_array)
+    {
+        int len = json_object_array_length(required_fields);
+        for (int i=0; i<len; i++)
+        {
+            //Get the required field from schema.
+            json_object* required_field = json_object_array_get_idx(required_fields, i);
+            if (json_object_get_type(required_field) != json_type_string)
+            {
+                sprintf(error_message, "Required field for object '%s' is not a string (schema violation).", field_name);
+                return 0;
+            }
+
+            //Does it exist in the object?
+            const char* required_field_str = json_object_get_string(required_field);
+            if (json_object_object_get(object, required_field_str) == NULL)
+            {
+                sprintf(error_message, "Required field '%s' was not present in object '%s'.", required_field_str, field_name);
+                return 0;
+            }
+        }
+    }
+
+    //If the boolean field "additionalProperties" exists and is set to false, ensure there are no
+    //extra properties apart from those required in the object.
+    //... todo
+
+    //Run through the "properties" object and validate each of those in turn.
+    json_object* properties = json_object_object_get(schema, "properties");
+    if (properties != NULL && json_object_get_type(properties) == json_type_object)
+    {
+        json_object_object_foreach(properties, key, value) {
+
+            //If the given property name does not exist on the target object, ignore and continue next.
+            json_object* object_prop = json_object_object_get(object, key);
+            if (object_prop == NULL)
+                continue;
+
+            //Validate against the schema.
+            if (!validate_field(key, value, object_prop, error_message))
+                return 0;
+        }
+    }
+
+    return 1;
+}
+
+//Validates a single array value according to the given specification.
+int validate_array(const char* field_name, json_object* schema, json_object* object, char* error_message)
+{
+    return 1;
+}
\ No newline at end of file
diff --git a/json-schema.h b/json-schema.h
new file mode 100644
index 0000000..997736d
--- /dev/null
+++ b/json-schema.h
@@ -0,0 +1,11 @@
+#ifndef JSON_SCHEMA_H
+#define JSON_SCHEMA_H
+
+#include "json.h"
+
+#define JSON_SCHEMA_VERSION "https://json-schema.org/draft/2020-12/schema"
+
+int validate_schema(json_object* schema, char* schema_directory, json_object* object, char* error_message);
+int validate_schema_from_file(const char* schema_file, json_object* object, char* error_message);
+
+#endif
\ No newline at end of file
diff --git a/specification/json/sections/cper-arm-processor.json b/specification/json/sections/cper-arm-processor.json
index 55b1dbc..4e473fc 100644
--- a/specification/json/sections/cper-arm-processor.json
+++ b/specification/json/sections/cper-arm-processor.json
@@ -92,11 +92,11 @@
                     },
                     "errorType": {
                         "type": "object",
-                        "$ref": "../common/cper-json-nvp.json"
+                        "$ref": "./common/cper-json-nvp.json"
                     },
                     "multipleError": {
                         "type": "object",
-                        "$ref": "../common/cper-json-nvp.json"
+                        "$ref": "./common/cper-json-nvp.json"
                     },
                     "flags": {
                         "type": "object",
@@ -153,11 +153,11 @@
                                     },
                                     "transactionType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "operation": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "level": {
                                         "type": "integer"
@@ -225,11 +225,11 @@
                                     },
                                     "transactionType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "operation": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "level": {
                                         "type": "integer"
@@ -251,15 +251,15 @@
                                     },
                                     "participationType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "addressSpace": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "accessMode": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "memoryAttributes": {
                                         "type": "integer"
@@ -286,7 +286,7 @@
                 "properties": {
                     "registerContextType": {
                         "type": "object",
-                        "$ref": "../common/cper-json-nvp.json"
+                        "$ref": "./common/cper-json-nvp.json"
                     },
                     "registerArraySize": {
                         "type": "integer"
diff --git a/specification/json/sections/cper-cxl-protocol.json b/specification/json/sections/cper-cxl-protocol.json
index 43ca251..fa25e81 100644
--- a/specification/json/sections/cper-cxl-protocol.json
+++ b/specification/json/sections/cper-cxl-protocol.json
@@ -34,7 +34,7 @@
         },
         "agentType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "agentAddress": {
             "type": "object",
diff --git a/specification/json/sections/cper-firmware.json b/specification/json/sections/cper-firmware.json
index 5c7126a..2003f6e 100644
--- a/specification/json/sections/cper-firmware.json
+++ b/specification/json/sections/cper-firmware.json
@@ -7,7 +7,7 @@
     "properties": {
         "errorRecordType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "revision": {
             "type": "integer"
diff --git a/specification/json/sections/cper-generic-dmar.json b/specification/json/sections/cper-generic-dmar.json
index e8f9d0e..ddccc14 100644
--- a/specification/json/sections/cper-generic-dmar.json
+++ b/specification/json/sections/cper-generic-dmar.json
@@ -28,15 +28,15 @@
         },
         "accessType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "addressType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "architectureType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "deviceAddress": {
             "type" : "integer"
diff --git a/specification/json/sections/cper-generic-processor.json b/specification/json/sections/cper-generic-processor.json
index 568046f..d30719c 100644
--- a/specification/json/sections/cper-generic-processor.json
+++ b/specification/json/sections/cper-generic-processor.json
@@ -52,19 +52,19 @@
         },
         "processorType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "processorISA": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "errorType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "operation": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "flags": {
             "type": "object",
diff --git a/specification/json/sections/cper-ia32x64-processor.json b/specification/json/sections/cper-ia32x64-processor.json
index ff482e5..33e6511 100644
--- a/specification/json/sections/cper-ia32x64-processor.json
+++ b/specification/json/sections/cper-ia32x64-processor.json
@@ -118,11 +118,11 @@
                                     },
                                     "transactionType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "operation": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "level": {
                                         "type": "integer",
@@ -188,11 +188,11 @@
                                     },
                                     "transactionType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "operation": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "level": {
                                         "type": "integer",
@@ -215,11 +215,11 @@
                                     },
                                     "participationType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "addressSpace": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "timedOut": {
                                         "type": "boolean"
@@ -257,7 +257,7 @@
                                     },
                                     "errorType": {
                                         "type": "object",
-                                        "$ref": "../common/cper-json-nvp.json"
+                                        "$ref": "./common/cper-json-nvp.json"
                                     },
                                     "processorContextCorrupt": {
                                         "type": "boolean"
@@ -302,7 +302,7 @@
                 "properties": {
                     "registerContextType": {
                         "type": "object",
-                        "$ref": "../common/cper-json-nvp.json"
+                        "$ref": "./common/cper-json-nvp.json"
                     },
                     "registerArraySize": {
                         "type": "integer"
diff --git a/specification/json/sections/cper-memory.json b/specification/json/sections/cper-memory.json
index d4686b1..38243c1 100644
--- a/specification/json/sections/cper-memory.json
+++ b/specification/json/sections/cper-memory.json
@@ -67,7 +67,7 @@
         },
         "errorStatus": {
             "type": "object",
-            "$ref": "../common/cper-json-error-status.json"
+            "$ref": "./common/cper-json-error-status.json"
         },
         "bank": {
             "type": "object",
@@ -97,7 +97,7 @@
         },
         "memoryErrorType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "extended": {
             "type": "object",
diff --git a/specification/json/sections/cper-memory2.json b/specification/json/sections/cper-memory2.json
index 6f615c3..cd3da93 100644
--- a/specification/json/sections/cper-memory2.json
+++ b/specification/json/sections/cper-memory2.json
@@ -79,7 +79,7 @@
         },
         "errorStatus": {
             "type": "object",
-            "$ref": "../common/cper-json-error-status.json"
+            "$ref": "./common/cper-json-error-status.json"
         },
         "bank": {
             "type": "object",
@@ -109,7 +109,7 @@
         },
         "memoryErrorType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "status": {
             "type": "object",
diff --git a/specification/json/sections/cper-pci-bus.json b/specification/json/sections/cper-pci-bus.json
index 2666610..0e9c121 100644
--- a/specification/json/sections/cper-pci-bus.json
+++ b/specification/json/sections/cper-pci-bus.json
@@ -40,11 +40,11 @@
         },
         "errorStatus": {
             "type": "object",
-            "$ref": "../common/cper-json-error-status.json"
+            "$ref": "./common/cper-json-error-status.json"
         },
         "errorType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "busID": {
             "type": "object",
diff --git a/specification/json/sections/cper-pci-component.json b/specification/json/sections/cper-pci-component.json
index 4e46485..878713e 100644
--- a/specification/json/sections/cper-pci-component.json
+++ b/specification/json/sections/cper-pci-component.json
@@ -28,7 +28,7 @@
         },
         "errorStatus": {
             "type": "object",
-            "$ref": "../common/cper-json-error-status.json"
+            "$ref": "./common/cper-json-error-status.json"
         },
         "idInfo": {
             "type": "object",
diff --git a/specification/json/sections/cper-pcie.json b/specification/json/sections/cper-pcie.json
index 5521e2b..05e6dad 100644
--- a/specification/json/sections/cper-pcie.json
+++ b/specification/json/sections/cper-pcie.json
@@ -37,7 +37,7 @@
         },
         "portType": {
             "type": "object",
-            "$ref": "../common/cper-json-nvp.json"
+            "$ref": "./common/cper-json-nvp.json"
         },
         "version": {
             "type": "object",
diff --git a/specification/json/sections/cper-vtd-dmar.json b/specification/json/sections/cper-vtd-dmar.json
index aa6716d..baa5942 100644
--- a/specification/json/sections/cper-vtd-dmar.json
+++ b/specification/json/sections/cper-vtd-dmar.json
@@ -56,7 +56,7 @@
                 },
                 "type": {
                     "type": "object",
-                    "$ref": "../common/cper-json-nvp.json"
+                    "$ref": "./common/cper-json-nvp.json"
                 }
             }
         },
diff --git a/testing/cper-test.c b/testing/cper-test.c
index 99a85c1..ca698b3 100644
--- a/testing/cper-test.c
+++ b/testing/cper-test.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include "../cper-parse.h"
 #include "json.h"
+#include "../json-schema.h"
 
 int main(int argc, char* argv[]) {
 
@@ -14,5 +15,21 @@
     json_object* ir = cper_to_ir(cper_file);
     fclose(cper_file);
 
-    printf("\n%s\n", json_object_to_json_string(ir));
+    const char* json_output = json_object_to_json_string(ir);
+    printf("\n%s\n", json_output);
+
+    //Test JSON validator.
+    if (argc >= 3)
+    {
+        printf("Validating output with specification %s...\n", argv[2]);
+        char* error_message = malloc(300);
+        if (!validate_schema_from_file(argv[2], ir, error_message))
+        {
+            printf("Validation failed: %s\n", error_message);
+        }
+        else
+        {
+            printf("Validation passed!\n");
+        }
+    }
 }
\ No newline at end of file