util.jsonschema

This implements a JSON Schema validator, allowing data to be validated against a declarative schema that specifies types and various properties.

Implements most, but not all of JSON Schema core and Validation.

Example

local schema = {
    type = "object";
    required = { "name"; "version" };
    properties = {
        name = { type = "string" };
        version = { type = "string" };
        os = { type = "string" };
    };
};

local data = { name = "Prosody"; version = 12 };

if jsonschema.validate(schema, data) then
    print("OK")
else
    print("Wrong!")
end

Implementation Notes

  • Objects and Arrays are not distinct types in Lua. Notably empty arrays and empty objects are indistinguishable.
  • $anchor, $dynamicAnchor, $dynamicRef are not implemented.
  • Deep comparison of objects and arrays is not implemented, affecting e.g. const, enum and uniqueItems.
  • minProperties and maxProperties are not implemented.
  • $id is not used.
  • Referencing external schemas is not implemented.
  • pattern is not implemented as it requires RegEx, not available in Lua without a 3rd party module. Instead a luaPattern that uses Lua Patterns is supported.
  • multipleOf and floating point numbers are unreliable.