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 boolean = 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 schema = {type:[ |
---|
77 | dbobject('Question',{ |
---|
78 | code:nestring(), |
---|
79 | title: nestring(), |
---|
80 | description: string(optional()), |
---|
81 | topic: string(optional()), |
---|
82 | categories: array(nestring(),optional()), |
---|
83 | content: array([ |
---|
84 | typedobject('Header',{ |
---|
85 | content:string(optional()) |
---|
86 | }), |
---|
87 | typedobject('Text',{ |
---|
88 | content:string(optional()) |
---|
89 | }), |
---|
90 | typedobject('Divider'), |
---|
91 | typedobject('StringInput'), |
---|
92 | typedobject('TextInput',{ |
---|
93 | maxLength:integer() |
---|
94 | }), |
---|
95 | typedobject('IntegerInput',{ |
---|
96 | min:integer(), |
---|
97 | max:integer(), |
---|
98 | step:integer() |
---|
99 | }), |
---|
100 | typedobject('MultipleChoiceInput',{ |
---|
101 | multiple:boolean(), |
---|
102 | items:array(nestring()) |
---|
103 | }) |
---|
104 | ],optional()), |
---|
105 | publicationDate:datetime(optional()) |
---|
106 | }), |
---|
107 | dbobject('Survey',{ |
---|
108 | title: nestring(), |
---|
109 | description: string(optional()), |
---|
110 | questions: array(nestring(),optional()), |
---|
111 | publicationDate:datetime(optional()) |
---|
112 | }), |
---|
113 | dbobject('SurveyRun',{ |
---|
114 | description: string(optional()), |
---|
115 | surveyId: nestring(), |
---|
116 | publicationDate:datetime(optional()), |
---|
117 | startDate:datetime(), |
---|
118 | endDate:datetime(), |
---|
119 | mode:{type:'string',enum:['open','closed']}, |
---|
120 | respondents: array(nestring(),optional()), |
---|
121 | metadata:{type:'object',optional:true} |
---|
122 | }), |
---|
123 | dbobject('Session',{ |
---|
124 | title: nestring(), |
---|
125 | description: string(optional()), |
---|
126 | publicationDate: datetime(optional()) |
---|
127 | }), |
---|
128 | dbobject('SessionTemplate',{ |
---|
129 | title: nestring(), |
---|
130 | description: string(optional()), |
---|
131 | publicationDate: datetime(optional()), |
---|
132 | plannedDate: datetime(optional()) |
---|
133 | }) |
---|
134 | ]}; |
---|
135 | |
---|
136 | return schema; |
---|
137 | }); |
---|