1 | define(['dojo/_base/lang'],function(lang){ |
---|
2 | |
---|
3 | /** Note on dojox.json.schema |
---|
4 | * This implementation is not according to the version 03 of the json-schema |
---|
5 | * draft. Differences: |
---|
6 | * - doesn't support the 'required' property, but uses 'optional' (which is |
---|
7 | * false by default). |
---|
8 | */ |
---|
9 | |
---|
10 | //////////////////////////////////////// |
---|
11 | // Base schema types |
---|
12 | // |
---|
13 | |
---|
14 | var dateRe = "\\d{4}-\\d{2}-\\d{2}"; // YYYY-MM-DD |
---|
15 | var timeRe = "\\d{2}:\\d{2}:\\d{2}"; // hh:mm:ss |
---|
16 | var datetimeRe = dateRe+"T"+timeRe+"Z"; // YYYY-MM-DDThh:mm:ssZ |
---|
17 | |
---|
18 | function mixin(obj1,obj2) { |
---|
19 | return lang.mixin({},obj1||{},obj2||{}); |
---|
20 | } |
---|
21 | function mkType(base) { |
---|
22 | return function(props) { |
---|
23 | return mixin(base,props); |
---|
24 | }; |
---|
25 | } |
---|
26 | |
---|
27 | var nestring = mkType({ type:'string', minLength:1 }); |
---|
28 | var string = mkType({ type:'string' }); |
---|
29 | var integer = mkType({ type: 'integer' }); |
---|
30 | var bool = mkType({ type: 'boolean' }); |
---|
31 | var pattern = function(p){ |
---|
32 | return mixin({ |
---|
33 | type: 'string', |
---|
34 | pattern: p |
---|
35 | }); |
---|
36 | }; |
---|
37 | var date = mkType(pattern(dateRe)); |
---|
38 | var time = mkType(pattern(timeRe)); |
---|
39 | var datetime = mkType(pattern(datetimeRe)); |
---|
40 | var array = function(items,props) { |
---|
41 | return mixin({ |
---|
42 | type: 'array', |
---|
43 | items: items |
---|
44 | },props); |
---|
45 | }; |
---|
46 | var object = function(properties,props) { |
---|
47 | return mixin({ |
---|
48 | type: 'object', |
---|
49 | properties: properties, |
---|
50 | additionalProperties: false |
---|
51 | },props); |
---|
52 | }; |
---|
53 | var optional = mkType({ optional: true }); |
---|
54 | |
---|
55 | //////////////////////////////////////// |
---|
56 | // Domain specific |
---|
57 | // |
---|
58 | |
---|
59 | var typedobject = function(type,properties,props) { |
---|
60 | return object(mixin(properties,{ |
---|
61 | type: pattern(type) |
---|
62 | }),props); |
---|
63 | }; |
---|
64 | |
---|
65 | var dbobject = function(type,properties,props) { |
---|
66 | return typedobject(type,mixin(properties,{ |
---|
67 | _id: nestring(optional()), |
---|
68 | _rev: nestring(optional()) |
---|
69 | }),props); |
---|
70 | }; |
---|
71 | |
---|
72 | //////////////////////////////////////// |
---|
73 | // The QED schema |
---|
74 | // |
---|
75 | |
---|
76 | var Respondent = dbobject('Respondent',{ |
---|
77 | secret: nestring() |
---|
78 | }); |
---|
79 | |
---|
80 | var Question = dbobject('Question',{ |
---|
81 | code:nestring(), |
---|
82 | title: nestring(), |
---|
83 | description: string(optional()), |
---|
84 | topic: string(optional()), |
---|
85 | categories: array(nestring(),optional()), |
---|
86 | content: array([ |
---|
87 | typedobject('Header',{ |
---|
88 | content:string(optional()) |
---|
89 | }), |
---|
90 | typedobject('Text',{ |
---|
91 | content:string(optional()) |
---|
92 | }), |
---|
93 | typedobject('Divider'), |
---|
94 | typedobject('StringInput'), |
---|
95 | typedobject('TextInput',{ |
---|
96 | maxLength:integer() |
---|
97 | }), |
---|
98 | typedobject('IntegerInput',{ |
---|
99 | min:integer(), |
---|
100 | max:integer(), |
---|
101 | step:integer() |
---|
102 | }), |
---|
103 | typedobject('MultipleChoiceInput',{ |
---|
104 | multiple:bool(), |
---|
105 | items:array(nestring()) |
---|
106 | }) |
---|
107 | ],optional()), |
---|
108 | publicationDate:datetime(optional()) |
---|
109 | }); |
---|
110 | |
---|
111 | var Survey = dbobject('Survey',{ |
---|
112 | title: nestring(), |
---|
113 | description: string(optional()), |
---|
114 | questions: array(Question,optional()), |
---|
115 | publicationDate:datetime(optional()) |
---|
116 | }); |
---|
117 | |
---|
118 | var SurveyRun = dbobject('SurveyRun',{ |
---|
119 | description: string(optional()), |
---|
120 | survey: Survey, |
---|
121 | publicationDate:datetime(optional()), |
---|
122 | startDate:datetime(optional()), |
---|
123 | endDate:datetime(optional()), |
---|
124 | mode:{type:'string',enum:['open','closed'],optional:true}, |
---|
125 | metadata:string(optional()) |
---|
126 | }); |
---|
127 | |
---|
128 | var Response = dbobject('Response',{ |
---|
129 | surveyRunId: nestring(), |
---|
130 | answers:{type:'object',optional:true}, |
---|
131 | publicationDate:datetime(optional()) |
---|
132 | }); |
---|
133 | |
---|
134 | var Session = dbobject('Session',{ |
---|
135 | title: nestring(), |
---|
136 | description: string(optional()), |
---|
137 | publicationDate: datetime(optional()) |
---|
138 | }); |
---|
139 | |
---|
140 | var SessionTemplate = dbobject('SessionTemplate',{ |
---|
141 | title: nestring(), |
---|
142 | description: string(optional()), |
---|
143 | publicationDate: datetime(optional()), |
---|
144 | plannedDate: datetime(optional()) |
---|
145 | }); |
---|
146 | |
---|
147 | var schema = {type:[ |
---|
148 | Question, |
---|
149 | Survey, |
---|
150 | SurveyRun, |
---|
151 | Response, |
---|
152 | Session, |
---|
153 | SessionTemplate |
---|
154 | ]}; |
---|
155 | |
---|
156 | return schema; |
---|
157 | }); |
---|