LibSchema-1.0

Warning: This project is experimental. Its files will not synchronize across the CurseForge network.

Overview

LibSchema-1.0 allows you to define schemas for a (complex) Lua types and then validate Lua values against those schemas. A typical use of LibSchema-1.0 is the validation of values received via AceSerializer-3.0 and AceComm-3.0 from other game clients. Of couse, LibSchema-1.0 can also be used for other validation tasks.

API Documentation

See API/LibSchema-1.0.lua.

Usage Example

-- A small add-on that uses LibSchema-1.0.
MyAddOn = LibStub("AceAddon-3.0"):NewAddon("MyAddOn", "LibSchema-1.0")

function MyAddOn:OnInitialize ()
	-- Create a new schema for the Achievement type
	local achievement = self:NewSchema("Achievement")
	
	-- An achievement is a table with some fields and some
	-- constraints. The 'status' field is optional.
	achievement:Type("table")
	achievement:Field("id"):Integer():Range(1, "*")
	achievement:Field("name"):Type("string"):Length(1, 30)
	achievement:Field("status"):Optional():Type("string"):Enum("in_progress", "completed")

	-- The category is a field with a nested table value that
	-- has some fields with some constraints.
	local category = achievement:Field("category"):Type("table")
	category:Field("id"):Integer():Range(1, "*")
	category:Field("name"):Type("string"):Length(1, 30)
	
	-- The 'kind' field declares a union on achievements. Simple
	-- achievements have no additional fields; meta achievements
	-- have a field criteria with an array table value the members
	-- of which are again achievements.
	local simple, meta = achievement:Union("kind", "simple", "meta")
	local criterion = meta:Field("criteria"):Length(1, 100):Array()
	criterion:Type(achievement)
end

-- Declares and then checks an achievement.
function MyAddOn:Check ()
	local achievement = {
		id = 1,
		name = "Meta Achievement",
		status = "in_progress",
		category = {
			id = 1,
			name = "General"
		},
		kind = "meta",
		criteria = {
			{
				id = 2,
				name = "Sub Achievement 1",
				category = {
					id = 2,
					name = "Dungeons"
				},
				kind = "simple"
			},
			{
				id = 3,
				name = "Sub Achievement 2",
				category = {
					id = 2,
					name = "Dungeons"
 				},
				kind = "simple"
			}
		}
	}
	local result, message = self:GetSchema("Achievement"):Validate(achievement)
	if not result then
		error(string.format("Validation failed: %s", message))
	end
end

Comments

Posts Quoted:
Reply
Clear All Quotes

About This Project

  • Project ID
    21725
  • Created
    Dec 23, 2009
  • Last Released File
    Never
  • Total Downloads
    670
  • License

Categories

Members

Recent Files

WoW Retail

  • r13
    Oct 13, 2010