Skip to content

Commit 1b96fa3

Browse files
committed
Merge pull request #292 from ulion/support_simple_null_mix_type
Support ['null', other_type] type. Fix #146
2 parents dc32574 + 744c444 commit 1b96fa3

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/services/schema-form.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
*/
66
angular.module('schemaForm').provider('schemaForm',
77
['sfPathProvider', function(sfPathProvider) {
8+
var stripNullType = function(type) {
9+
if (Array.isArray(type) && type.length == 2) {
10+
if (type[0] === 'null')
11+
return type[1];
12+
if (type[1] === 'null')
13+
return type[0];
14+
}
15+
return type;
16+
}
817

918
//Creates an default titleMap list from an enum, i.e. a list of strings.
1019
var enumToTitleMap = function(enm) {
@@ -35,7 +44,7 @@ angular.module('schemaForm').provider('schemaForm',
3544
};
3645

3746
var defaultFormDefinition = function(name, schema, options) {
38-
var rules = defaults[schema.type];
47+
var rules = defaults[stripNullType(schema.type)];
3948
if (rules) {
4049
var def;
4150
for (var i = 0; i < rules.length; i++) {
@@ -88,7 +97,7 @@ angular.module('schemaForm').provider('schemaForm',
8897
};
8998

9099
var text = function(name, schema, options) {
91-
if (schema.type === 'string' && !schema['enum']) {
100+
if (stripNullType(schema.type) === 'string' && !schema['enum']) {
92101
var f = stdFormObj(name, schema, options);
93102
f.key = options.path;
94103
f.type = 'text';
@@ -100,7 +109,7 @@ angular.module('schemaForm').provider('schemaForm',
100109
//default in json form for number and integer is a text field
101110
//input type="number" would be more suitable don't ya think?
102111
var number = function(name, schema, options) {
103-
if (schema.type === 'number') {
112+
if (stripNullType(schema.type) === 'number') {
104113
var f = stdFormObj(name, schema, options);
105114
f.key = options.path;
106115
f.type = 'number';
@@ -110,7 +119,7 @@ angular.module('schemaForm').provider('schemaForm',
110119
};
111120

112121
var integer = function(name, schema, options) {
113-
if (schema.type === 'integer') {
122+
if (stripNullType(schema.type) === 'integer') {
114123
var f = stdFormObj(name, schema, options);
115124
f.key = options.path;
116125
f.type = 'number';
@@ -120,7 +129,7 @@ angular.module('schemaForm').provider('schemaForm',
120129
};
121130

122131
var checkbox = function(name, schema, options) {
123-
if (schema.type === 'boolean') {
132+
if (stripNullType(schema.type) === 'boolean') {
124133
var f = stdFormObj(name, schema, options);
125134
f.key = options.path;
126135
f.type = 'checkbox';
@@ -130,7 +139,7 @@ angular.module('schemaForm').provider('schemaForm',
130139
};
131140

132141
var select = function(name, schema, options) {
133-
if (schema.type === 'string' && schema['enum']) {
142+
if (stripNullType(schema.type) === 'string' && schema['enum']) {
134143
var f = stdFormObj(name, schema, options);
135144
f.key = options.path;
136145
f.type = 'select';
@@ -143,7 +152,7 @@ angular.module('schemaForm').provider('schemaForm',
143152
};
144153

145154
var checkboxes = function(name, schema, options) {
146-
if (schema.type === 'array' && schema.items && schema.items['enum']) {
155+
if (stripNullType(schema.type) === 'array' && schema.items && schema.items['enum']) {
147156
var f = stdFormObj(name, schema, options);
148157
f.key = options.path;
149158
f.type = 'checkboxes';
@@ -156,7 +165,7 @@ angular.module('schemaForm').provider('schemaForm',
156165
};
157166

158167
var fieldset = function(name, schema, options) {
159-
if (schema.type === 'object') {
168+
if (stripNullType(schema.type) === 'object') {
160169
var f = stdFormObj(name, schema, options);
161170
f.type = 'fieldset';
162171
f.items = [];
@@ -188,7 +197,7 @@ angular.module('schemaForm').provider('schemaForm',
188197

189198
var array = function(name, schema, options) {
190199

191-
if (schema.type === 'array') {
200+
if (stripNullType(schema.type) === 'array') {
192201
var f = stdFormObj(name, schema, options);
193202
f.type = 'array';
194203
f.key = options.path;
@@ -386,7 +395,7 @@ angular.module('schemaForm').provider('schemaForm',
386395
ignore = ignore || {};
387396
globalOptions = globalOptions || {};
388397

389-
if (schema.type === 'object') {
398+
if (stripNullType(schema.type) === 'object') {
390399
angular.forEach(schema.properties, function(v, k) {
391400
if (ignore[k] !== true) {
392401
var required = schema.required && schema.required.indexOf(k) !== -1;

0 commit comments

Comments
 (0)