source: Dev/branches/rest-dojo-ui/client/rft/schema.js @ 416

Last change on this file since 416 was 415, checked in by hendrikvanantwerpen, 12 years ago

Support data validation and increase dendency separation.

Added json-schema validation before documents are saved. Acts both as
a safe-guard and as a reference of the data model.

Added directory for server related material. For now it contains scripts
to configure CouchDB and check validity of documents against the schema.

Started separating out parts that depend on the data model from parts
that do not.

File size: 4.1 KB
Line 
1define(['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            surveyId: nestring(),
115            publicationDate:datetime(optional()),
116            startDate:datetime(optional()),
117            endDate:datetime(optional()),
118            mode:{type:'string',enum:['open','closed']},
119            respondents: array(nestring(),optional()),
120            metadata:{type:'object'}
121        }),
122        dbobject('Session',{
123            title: nestring(),
124            description: string(optional()),
125            publicationDate: datetime(optional())
126        }),
127        dbobject('SessionTemplate',{
128            title: nestring(),
129            description: string(optional()),
130            publicationDate: datetime(optional()),
131            plannedDate: datetime(optional())
132        })
133    ]};
134
135    return schema;
136});
Note: See TracBrowser for help on using the repository browser.