LibSchema-1.0

This project is experimental.

This project is only experimental and may change drastically between now and when it comes out of experimental status. It may even be disapproved once that happens, worst-case.

Be forewarned before downloading anything from this project.

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

You must login to post a comment. Don't have an account? Register to get one!

Facts

Date created
Dec 23, 2009
Category
Last update
Oct 13, 2010
Development stage
Alpha
Language
  • enUS
License
MIT License
Reverse relationships
1
Downloads
191
Recent files
  • A: r13 for 4.0.1 Oct 13, 2010
  • A: r12 for 3.3.5 May 19, 2010
  • A: r11 for 3.3.5 May 15, 2010
  • A: r10 for 3.3.5 May 15, 2010
  • A: r9 for 3.3.5 May 15, 2010

Authors

Relationships

Embedded library
LibStub