it("should validate a number against a number schema", () => { constvalidator = newjsonschema.Validator({ type:"number" }); const [isValid, error] = validator.validate(42);
it("should fail to validate a number against a string schema", () => { constvalidator = newjsonschema.Validator({ type:"string" }); const [isValid, error] = validator.validate(42);
expect.equal(isValid, false); expect.equal(error, '42 is not of type "string"'); });
it("should fail to compile an invalid schema", () => { try { newjsonschema.Validator({ type:"invalidType" }); assert(false, "Expected an error to be thrown for invalid schema"); } catch (e) { expect.equal( `${string.match(`${e}`, "([^\r\n]+)")[0]}`, `"invalidType" is not valid under any of the schemas listed in the 'anyOf' keyword`, ); } }); });
Lua Example
--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] local____exports = {} local____pelican = require("pelican") localjsonschema = ____pelican.jsonschema local____lester = require("tests.lester") localdescribe = ____lester.describe localexpect = ____lester.expect localit = ____lester.it describe( "jsonschema", function() it( "should successfully instantiate using the new keyword", function() localvalidator = jsonschema.Validator.new({type = "string"}) expect.equal( tostring(validator), "Validator({\"type\":\"string\"})" ) end ) it( "should successfully instantiate using the static new method", function() localvalidator = jsonschema.Validator.new({type = "string"}) expect.equal( tostring(validator), "Validator({\"type\":\"string\"})" ) end ) it( "should validate a string against a string schema", function() localvalidator = jsonschema.Validator.new({type = "string"}) localisValid, ____error = validator:validate("Hello World") expect.equal(isValid, true) expect.equal(____error, nil) end ) it( "should validate a number against a number schema", function() localvalidator = jsonschema.Validator.new({type = "number"}) localisValid, ____error = validator:validate(42) expect.equal(isValid, true) expect.equal(____error, nil) end ) it( "should fail to validate a number against a string schema", function() localvalidator = jsonschema.Validator.new({type = "string"}) localisValid, ____error = validator:validate(42) expect.equal(isValid, false) expect.equal(____error, "42 is not of type \"string\"") end ) it( "should validate an object against an object schema", function() localvalidator = jsonschema.Validator.new({type = "object", properties = {name = {type = "string"}, age = {type = "number"}}, required = {"name", "age"}}) localisValid, ____error = validator:validate({name = "John", age = 30}) expect.equal(isValid, true) expect.equal(____error, nil) end ) it( "should fail to compile an invalid schema", function() do localfunction____catch(e) expect.equal((string.match( tostring(e), "([^\r\n]+)" )), "\"invalidType\" is not valid under any of the schemas listed in the 'anyOf' keyword") end local____try, ____hasReturned = pcall(function() jsonschema.Validator.new({type = "invalidType"}) assert(false, "Expected an error to be thrown for invalid schema") end) if not ____trythen ____catch(____hasReturned) end end end ) end ) return____exports
The
json_schema
module provides functions to validate Lua tables against JSON schemas.TypeScript Example
Lua Example
No Self