-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrestschema.js
121 lines (114 loc) · 3.72 KB
/
crestschema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*!
* crestschema
* https://github.com/jimpurbrick/crestschema
*
* Copyright 2015, Jim Purbrick (http://jimpurbrick.com)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/GPL-2.0
*/
/*
* A JavaScript library which converts CREST OPTIONS responses in to
* JSON schema. Designed to be usable by browser or node applications.
*/
(function (exports) {
'use strict';
function propertiesFromCrestProperties(crestProperties) {
var result = {};
var name;
var crestSchema;
for (name in crestProperties) {
if (crestProperties.hasOwnProperty(name)) {
crestSchema = crestProperties[name];
result[name] =
jsonSchemaFromCrestSchema(crestSchema.type,
crestSchema,
name);
}
}
return result;
}
function requiredPropertiesFromCrestProperties(crestProperties) {
var result = [];
var name;
for (name in crestProperties) {
if (crestProperties.hasOwnProperty(name)) {
if (!crestProperties[name].isOptional) {
result.push(name);
}
}
}
return result;
}
function jsonSchemaFromCrestSchema(type, crestSchema, id) {
var result = {};
result.id = id;
if (crestSchema.description) {
result.description = crestSchema.description;
}
switch (type) {
case "Array":
result.type = "array";
result.items = jsonSchemaFromCrestSchema(
crestSchema.extraData,
crestSchema,
'0'
);
break;
case "String":
case "Uri":
result.type = "string";
break;
case "Long":
result.type = "integer";
break;
case "Float":
result.type = "number";
break;
case "Bool":
result.type = "boolean";
break;
case "Dict":
case "Ref":
case "ExternalRef":
default: // 'Collection of...', 'vnd.ccp.eve...'
result.type = "object";
result.properties =
propertiesFromCrestProperties(crestSchema.subContent);
result.required =
requiredPropertiesFromCrestProperties(crestSchema.subContent);
break;
}
return result;
}
exports.jsonSchemaFromCrestOptions = function (optionsData) {
var result = {
GET: {},
PUT: {},
POST: {},
OPTIONS: {}
};
var options = JSON.parse(optionsData);
options.representations.map(function (representation) {
['acceptType', 'contentType'].map(function (type) {
var crestSchema;
var jsonSchema;
if (representation.hasOwnProperty(type)) {
crestSchema = JSON.parse(
representation[type].jsonDumpOfStructure
);
jsonSchema = jsonSchemaFromCrestSchema(
crestSchema.type,
crestSchema,
'/'
);
jsonSchema.$schema =
"http://json-schema.org/draft-04/schema#";
result[representation.verb][representation[type].name] =
jsonSchema;
}
});
});
return result;
};
})(typeof exports === 'undefined'? this['crestschema']={}:exports);