-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjson-simple-schema-tests.js
199 lines (166 loc) · 5.69 KB
/
json-simple-schema-tests.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var packageJsonSchema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Product',
'description': 'A product from Acme\'s catalog',
'type': 'object',
'properties': {
'id': {
'description': 'The unique identifier for a product',
'type': 'integer'
},
'name': {
'description': 'Name of the product',
'type': 'string'
},
'price': {
'type': 'number',
'minimum': '0', //test parsing of string values for number
'exclusiveMinimum': 1 //test parsing string values for boolean
},
'tags': {
'type': 'array',
'items': {
'type': 'string'
},
'minItems': 1,
'uniqueItems': true
},
'arrayOfObjects': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'foo': {'type': 'string'}
}
}
},
'objectWithAdditionalProps': {
'type': 'object',
'properties': {
'blah': {
'type': 'string'
}
},
'additionalProperties': true
},
'arrayWithAdditionalProperties': {
'type': 'array',
'items': {
'type': 'object',
'additionalProperties': true,
'properties': {
'test': {'type': 'string'}
}
}
},
'color': {
'type': 'string',
'enum': ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet', null]
},
'emailAddress': {
'type': 'string',
'format': 'email'
}
},
'required': ['id', 'name', 'price'],
'additionalProperties': true //ignore additionalProperties option at root level since SimpleSchema doesn't support blackbox at root level.
};
Tinytest.add('JSONSchema - convert a basic JSON schema object to a SimpleSchema object', function(test) {
var jsonSchema = new JSONSchema(packageJsonSchema);
var simpleSchema = jsonSchema.toSimpleSchema();
//Make sure .toSimpleSchema returned a SimpleSchema object
test.instanceOf(simpleSchema, SimpleSchema);
var rawSchema = simpleSchema._schema;
test.equal(rawSchema.id.type, Number);
test.equal(rawSchema.id.optional, false);
test.equal(rawSchema.name.type, String);
test.equal(rawSchema.name.optional, false);
test.equal(rawSchema.price.type, Number);
test.equal(rawSchema.price.min, 0);
test.equal(rawSchema.price.optional, false);
test.equal(rawSchema.price.exclusiveMin, true);
test.equal(rawSchema.price.decimal, true);
test.equal(rawSchema.tags.type, Array);
test.equal(rawSchema.tags.minCount, 1);
test.equal(rawSchema['tags.$'].type, String);
test.equal(rawSchema['arrayOfObjects.$.foo'].type, String);
test.equal(rawSchema['arrayWithAdditionalProperties.$'].type, Object);
test.equal(rawSchema['arrayWithAdditionalProperties.$'].blackbox, true);
test.equal(rawSchema.objectWithAdditionalProps.blackbox, true);
test.equal(rawSchema.color.type, String);
test.equal(rawSchema.color.allowedValues.length, 7);
test.equal(rawSchema.emailAddress.type, String);
test.equal(rawSchema.emailAddress.regEx, SimpleSchema.RegEx.Email);
});
Tinytest.add('JSONSchema - uses toSimpleSchemaProps to manually add a property before creating SimpleSchema instance', function(test) {
var jsonSchema = new JSONSchema(packageJsonSchema);
var props = jsonSchema.toSimpleSchemaProps();
props.extraProperty = {
type: String,
optional: true
}
var simpleSchema = new SimpleSchema(props);
//Make sure .toSimpleSchema returned a SimpleSchema object
test.instanceOf(simpleSchema, SimpleSchema);
var rawSchema = simpleSchema._schema;
test.equal(rawSchema.id.type, Number);
test.equal(rawSchema.id.optional, false);
test.equal(rawSchema.name.type, String);
test.equal(rawSchema.name.optional, false);
test.equal(rawSchema.price.type, Number);
test.equal(rawSchema.price.min, 0);
test.equal(rawSchema.price.optional, false);
test.equal(rawSchema.price.exclusiveMin, true);
test.equal(rawSchema.price.decimal, true);
test.equal(rawSchema.tags.type, Array);
test.equal(rawSchema.tags.minCount, 1);
test.equal(rawSchema['tags.$'].type, String);
test.equal(rawSchema['arrayOfObjects.$.foo'].type, String);
test.equal(rawSchema['arrayWithAdditionalProperties.$'].type, Object);
test.equal(rawSchema['arrayWithAdditionalProperties.$'].blackbox, true);
test.equal(rawSchema.objectWithAdditionalProps.blackbox, true);
test.equal(rawSchema.color.type, String);
test.equal(rawSchema.color.allowedValues.length, 7);
test.equal(rawSchema.emailAddress.type, String);
test.equal(rawSchema.emailAddress.regEx, SimpleSchema.RegEx.Email);
test.equal(rawSchema.extraProperty.type, String);
test.equal(rawSchema.extraProperty.optional, true);
});
// https://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-04
var packageJsonSchemaWithInternalRef = {
'$schema': 'http://json-schema.org/draft-04/schema#',
'title': 'Thing',
'type': 'object',
'properties': {
'prop': {'$ref': '#/definitions/definitionWithSpecialChars~0~1%25'},
'prop2': {'$ref': '#/definitions/arrayOfDefs/1'},
'prop3': {'$ref': '#'},
'refItems': {
'type': 'array',
'items': {'$ref': '#/definitions/arrayOfDefs/1'}
}
},
'definitions': {
'definitionWithSpecialChars~/%': {
'type': 'string',
'format': 'email'
},
'arrayOfDefs': [
{'type': 'string'},
{'type': 'number'}
]
}
}
Tinytest.add('JSONSchema - convert a JSON schema object with internal references to a SimpleSchema object', function(test) {
var jsonSchema = new JSONSchema(packageJsonSchemaWithInternalRef);
var simpleSchema = jsonSchema.toSimpleSchema();
//Make sure .toSimpleSchema returned a SimpleSchema object
test.instanceOf(simpleSchema, SimpleSchema);
var rawSchema = simpleSchema._schema;
test.equal(rawSchema.prop.type, String);
test.equal(rawSchema.prop.regEx, SimpleSchema.RegEx.Email);
test.equal(rawSchema.prop2.type, Number);
test.equal(rawSchema.prop3.type, Object);
test.equal(rawSchema.refItems.type, Array);
test.equal(rawSchema['refItems.$'].type, Number);
});