define(['dojo/_base/lang'],function(lang){ /** Note on dojox.json.schema * This implementation is not according to the version 03 of the json-schema * draft. Differences: * - doesn't support the 'required' property, but uses 'optional' (which is * false by default). */ //////////////////////////////////////// // Base schema types // var dateRe = "\\d{4}-\\d{2}-\\d{2}"; // YYYY-MM-DD var timeRe = "\\d{2}:\\d{2}:\\d{2}"; // hh:mm:ss var datetimeRe = dateRe+"T"+timeRe+"Z"; // YYYY-MM-DDThh:mm:ssZ function mixin(obj1,obj2) { return lang.mixin({},obj1||{},obj2||{}); } function mkType(base) { return function(props) { return mixin(base,props); }; } var nestring = mkType({ type:'string', minLength:1 }); var string = mkType({ type:'string' }); var integer = mkType({ type: 'integer' }); var bool = mkType({ type: 'boolean' }); var pattern = function(p){ return mixin({ type: 'string', pattern: p }); }; var date = mkType(pattern(dateRe)); var time = mkType(pattern(timeRe)); var datetime = mkType(pattern(datetimeRe)); var array = function(items,props) { return mixin({ type: 'array', items: items },props); }; var object = function(properties,props) { return mixin({ type: 'object', properties: properties, additionalProperties: false },props); }; var optional = mkType({ optional: true }); //////////////////////////////////////// // Domain specific // var typedobject = function(type,properties,props) { return object(mixin(properties,{ type: pattern(type) }),props); }; var dbobject = function(type,properties,props) { return typedobject(type,mixin(properties,{ _id: nestring(optional()), _rev: nestring(optional()) }),props); }; //////////////////////////////////////// // The QED schema // var Respondent = dbobject('Respondent',{ secret: nestring() }); var Question = dbobject('Question',{ code:nestring(), title: nestring(), description: string(optional()), topic: string(optional()), categories: array(nestring(),optional()), content: array([ typedobject('Header',{ content:string(optional()) }), typedobject('Text',{ content:string(optional()) }), typedobject('Divider'), typedobject('StringInput'), typedobject('TextInput',{ maxLength:integer() }), typedobject('IntegerInput',{ min:integer(), max:integer(), step:integer() }), typedobject('MultipleChoiceInput',{ multiple:bool(), items:array(nestring()) }) ],optional()), publicationDate:datetime(optional()) }); var Survey = dbobject('Survey',{ title: nestring(), description: string(optional()), questions: array(Question,optional()), publicationDate:datetime(optional()) }); var SurveyRun = dbobject('SurveyRun',{ description: string(optional()), survey: Survey, publicationDate:datetime(optional()), startDate:datetime(optional()), endDate:datetime(optional()), mode:{type:'string','enum':['open','closed'],optional:true}, metadata:string(optional()) }); var Response = dbobject('Response',{ surveyRunId: nestring(), answers:{type:'object',optional:true}, publicationDate:datetime(optional()) }); var Session = dbobject('Session',{ title: nestring(), description: string(optional()), publicationDate: datetime(optional()) }); var SessionTemplate = dbobject('SessionTemplate',{ title: nestring(), description: string(optional()), publicationDate: datetime(optional()), plannedDate: datetime(optional()) }); var schema = {type:[ Question, Survey, SurveyRun, Response, Session, SessionTemplate ]}; return schema; });