lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Norman Ramsey <nr <at> eecs.harvard.edu> writes:

> But I think the real intellectual work lies in the design
> of the schema language.  How, for example, should I specify...

Keep it simple!  You said you wanted something to protect against 
spelling mistakes and such, and the simple "prototype" schema works 
just fine for that.

If you have a few nodes with crazy constraints that absolutely must
be checked, it's probably easier just to give them their own validation
functions.  Below, the schema's "track" and "time" nodes are functions.
When validate() encounters a function in the schema, it can pass it the
corresponding data node and make sure it approves.

schema = {
    title = "Window",
    dimensions = {
        height = 1,
        width = 1
    },
    track = function (t) 
        if type(t) ~= "table" then return false end
        for i,v in ipairs(t) do
            if type(v) ~= "table" or not validate(v, {
                lat = 1,
                lon = 1,
                ele = 1,
                time = is_iso_8601_time
            }) then return false end
        end
    end
}

--  field track contains
--      a list of which each element is 
--          a table containing the keys
--             'lat', which is a number, and
--             'lon', which is a number, and 
--             'ele', which is a number, and
--             'time' which is a time
-- where
--   a time is a string which can be be validated with the function 
--   is_iso_8601_time