Skip to content

Commit fdffc70

Browse files
author
Carolina Wright
committed
add type validations when using type declaration
1 parent 50ea600 commit fdffc70

File tree

9 files changed

+316867
-35046
lines changed

9 files changed

+316867
-35046
lines changed

dist/scripts/api-console-vendor.js

Lines changed: 316823 additions & 35020 deletions
Large diffs are not rendered by default.

dist/scripts/api-console.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@
654654
scope: {
655655
src: '=',
656656
context: '=',
657+
types: '=',
657658
type: '@',
658659
title: '@'
659660
},
@@ -1427,6 +1428,7 @@
14271428
scope: {
14281429
context: '=',
14291430
type: '=',
1431+
types: '=',
14301432
model: '=',
14311433
param: '='
14321434
},
@@ -2639,7 +2641,7 @@
26392641
var errors;
26402642

26412643
value = typeof value !== 'undefined' && value !== null && value.length === 0 ? undefined : value;
2642-
current[validation.id] = value;
2644+
current[validationId] = value;
26432645

26442646
errors = validator(sanitizer(current)).errors;
26452647

@@ -2648,24 +2650,30 @@
26482650
// Note: We want to allow invalid errors for testing purposes
26492651
return value;
26502652
} else {
2651-
clear(control, validationRules[validation.id]);
2653+
clear(control, validationRules[validationId]);
26522654
return value;
26532655
}
26542656
}
26552657

26562658
var validation = $parse($attrs.validate)($scope);
2659+
var validationId = validation.id;
26572660
var sanitationRules = {};
26582661
var validationRules = {};
26592662
var control = $ctrl;
26602663

2661-
sanitationRules[validation.id] = {
2664+
if (validation && validation.type) {
2665+
var declaredType = RAML.Inspector.Types.findType(validation.type[0], $scope.types);
2666+
if (declaredType) { validation = declaredType; }
2667+
}
2668+
2669+
sanitationRules[validationId] = {
26622670
type: validation.type || null,
26632671
repeat: validation.repeat || null
26642672
};
26652673

2666-
sanitationRules[validation.id] = RAML.Utils.filterEmpty(sanitationRules[validation.id]);
2674+
sanitationRules[validationId] = RAML.Utils.filterEmpty(sanitationRules[validationId]);
26672675

2668-
validationRules[validation.id] = {
2676+
validationRules[validationId] = {
26692677
type: validation.type || null,
26702678
minLength: validation.minLength || null,
26712679
maxLength: validation.maxLength || null,
@@ -2677,7 +2685,7 @@
26772685
repeat: validation.repeat || null
26782686
};
26792687

2680-
validationRules[validation.id] = RAML.Utils.filterEmpty(validationRules[validation.id]);
2688+
validationRules[validationId] = RAML.Utils.filterEmpty(validationRules[validationId]);
26812689

26822690
$ctrl.$formatters.unshift(function(value) {
26832691
return validate(value);
@@ -5689,7 +5697,8 @@ RAML.Inspector = (function() {
56895697
* @return {Boolean}
56905698
*/
56915699
var toBoolean = function (value) {
5692-
return [0, false, '', '0', 'false'].indexOf(value) === -1;
5700+
var index = [0, false, '0', 'false', 1, true, '1', 'true'].indexOf(value);
5701+
return (index !== -1) ? Boolean(index > 3) : null;
56935702
};
56945703

56955704
/**
@@ -5729,7 +5738,7 @@ RAML.Inspector = (function() {
57295738
*/
57305739
var returnValue = function (value) {
57315740
return value;
5732-
}
5741+
};
57335742

57345743
var toUnion = function (value, key, object, configs) {
57355744
var any = null;
@@ -5740,7 +5749,7 @@ RAML.Inspector = (function() {
57405749
});
57415750

57425751
return any;
5743-
}
5752+
};
57445753

57455754
function isNativeType(typeName) {
57465755
typeName = typeName.replace('[]', '');
@@ -6708,7 +6717,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
67086717
" </span>\n" +
67096718
" </span>\n" +
67106719
"\n" +
6711-
" <raml-field context=\"context\" type=\"type\" param=\"param.definitions[0]\" model=\"context[type].values[param.definitions[0].id]\"></raml-field>\n" +
6720+
" <raml-field context=\"context\" type=\"type\" types=\"types\" param=\"param.definitions[0]\" model=\"context[type].values[param.definitions[0].id]\"></raml-field>\n" +
67126721
" </p>\n" +
67136722
" </div>\n" +
67146723
"</section>\n"
@@ -6982,7 +6991,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
69826991
" </div>\n" +
69836992
"\n" +
69846993
" <div ng-if=\"param.properties\" style=\"padding-left: 10px\">\n" +
6985-
" <raml-field ng-repeat=\"aParam in param.properties\" context=\"context\" type=\"type\" param=\"aParam[0]\" ng-init=\"model[0][aParam[0].name] = [undefined]\" model=\"model[0][aParam[0].name]\"></raml-field>\n" +
6994+
" <raml-field ng-repeat=\"aParam in param.properties\" context=\"context\" type=\"type\" types=\"types\" param=\"aParam[0]\" ng-init=\"model[0][aParam[0].name] = [undefined]\" model=\"model[0][aParam[0].name]\"></raml-field>\n" +
69866995
" </div>\n" +
69876996
"</div>\n"
69886997
);
@@ -7212,7 +7221,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
72127221
"\n" +
72137222
" <named-parameters src=\"methodInfo.headers.plain\" context=\"context\" type=\"headers\" title=\"Headers\" enable-custom-parameters></named-parameters>\n" +
72147223
"\n" +
7215-
" <named-parameters src=\"methodInfo.queryParameters\" context=\"context\" type=\"queryParameters\" title=\"Query Parameters\" enable-custom-parameters></named-parameters>\n" +
7224+
" <named-parameters src=\"methodInfo.queryParameters\" context=\"context\" type=\"queryParameters\" types=\"types\" title=\"Query Parameters\" enable-custom-parameters></named-parameters>\n" +
72167225
"\n" +
72177226
" <section ng-if=\"methodInfo.queryString\">\n" +
72187227
" <header class=\"raml-console-sidebar-row raml-console-sidebar-subheader\">\n" +
@@ -7263,7 +7272,7 @@ angular.module('ramlConsoleApp').run(['$templateCache', function($templateCache)
72637272
" </span>\n" +
72647273
" </span>\n" +
72657274
"\n" +
7266-
" <raml-field context=\"context\" type=\"type\" param=\"param.definitions[0]\" model=\"context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]\"></raml-field>\n" +
7275+
" <raml-field context=\"context\" type=\"type\" types=\"types\" param=\"param.definitions[0]\" model=\"context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]\"></raml-field>\n" +
72677276
" </p>\n" +
72687277
" </div>\n" +
72697278
" </div>\n" +

src/app/directives/named-parameters.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
scope: {
1010
src: '=',
1111
context: '=',
12+
types: '=',
1213
type: '@',
1314
title: '@'
1415
},

src/app/directives/named-parameters.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ <h4 class="raml-console-sidebar-subhead">{{title}}</h4>
3030
</span>
3131
</span>
3232

33-
<raml-field context="context" type="type" param="param.definitions[0]" model="context[type].values[param.definitions[0].id]"></raml-field>
33+
<raml-field context="context" type="type" types="types" param="param.definitions[0]" model="context[type].values[param.definitions[0].id]"></raml-field>
3434
</p>
3535
</div>
3636
</section>

src/app/directives/raml-field.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
scope: {
1010
context: '=',
1111
type: '=',
12+
types: '=',
1213
model: '=',
1314
param: '='
1415
},

src/app/directives/raml-field.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@
5050
</div>
5151

5252
<div ng-if="param.properties" style="padding-left: 10px">
53-
<raml-field ng-repeat="aParam in param.properties" context="context" type="type" param="aParam[0]" ng-init="model[0][aParam[0].name] = [undefined]" model="model[0][aParam[0].name]"></raml-field>
53+
<raml-field ng-repeat="aParam in param.properties" context="context" type="type" types="types" param="aParam[0]" ng-init="model[0][aParam[0].name] = [undefined]" model="model[0][aParam[0].name]"></raml-field>
5454
</div>
5555
</div>

src/app/directives/sidebar.tpl.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ <h4 class="raml-console-sidebar-subhead">Authentication</h4>
5454

5555
<named-parameters src="methodInfo.headers.plain" context="context" type="headers" title="Headers" enable-custom-parameters></named-parameters>
5656

57-
<named-parameters src="methodInfo.queryParameters" context="context" type="queryParameters" title="Query Parameters" enable-custom-parameters></named-parameters>
57+
<named-parameters src="methodInfo.queryParameters" context="context" type="queryParameters" types="types" title="Query Parameters" enable-custom-parameters></named-parameters>
5858

5959
<section ng-if="methodInfo.queryString">
6060
<header class="raml-console-sidebar-row raml-console-sidebar-subheader">
@@ -105,7 +105,7 @@ <h4 class="raml-console-sidebar-subhead">Body</h4>
105105
</span>
106106
</span>
107107

108-
<raml-field context="context" type="type" param="param.definitions[0]" model="context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]"></raml-field>
108+
<raml-field context="context" type="type" types="types" param="param.definitions[0]" model="context.bodyContent.definitions[context.bodyContent.selected].values[param.definitions[0].id]"></raml-field>
109109
</p>
110110
</div>
111111
</div>

src/app/directives/validate.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
var errors;
1919

2020
value = typeof value !== 'undefined' && value !== null && value.length === 0 ? undefined : value;
21-
current[validation.id] = value;
21+
current[validationId] = value;
2222

2323
errors = validator(sanitizer(current)).errors;
2424

@@ -27,24 +27,30 @@
2727
// Note: We want to allow invalid errors for testing purposes
2828
return value;
2929
} else {
30-
clear(control, validationRules[validation.id]);
30+
clear(control, validationRules[validationId]);
3131
return value;
3232
}
3333
}
3434

3535
var validation = $parse($attrs.validate)($scope);
36+
var validationId = validation.id;
3637
var sanitationRules = {};
3738
var validationRules = {};
3839
var control = $ctrl;
3940

40-
sanitationRules[validation.id] = {
41+
if (validation && validation.type) {
42+
var declaredType = RAML.Inspector.Types.findType(validation.type[0], $scope.types);
43+
if (declaredType) { validation = declaredType; }
44+
}
45+
46+
sanitationRules[validationId] = {
4147
type: validation.type || null,
4248
repeat: validation.repeat || null
4349
};
4450

45-
sanitationRules[validation.id] = RAML.Utils.filterEmpty(sanitationRules[validation.id]);
51+
sanitationRules[validationId] = RAML.Utils.filterEmpty(sanitationRules[validationId]);
4652

47-
validationRules[validation.id] = {
53+
validationRules[validationId] = {
4854
type: validation.type || null,
4955
minLength: validation.minLength || null,
5056
maxLength: validation.maxLength || null,
@@ -56,7 +62,7 @@
5662
repeat: validation.repeat || null
5763
};
5864

59-
validationRules[validation.id] = RAML.Utils.filterEmpty(validationRules[validation.id]);
65+
validationRules[validationId] = RAML.Utils.filterEmpty(validationRules[validationId]);
6066

6167
$ctrl.$formatters.unshift(function(value) {
6268
return validate(value);

src/vendor/raml-sanitize/raml-sanitize.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
* @return {Boolean}
1717
*/
1818
var toBoolean = function (value) {
19-
return [0, false, '', '0', 'false'].indexOf(value) === -1;
19+
var index = [0, false, '0', 'false', 1, true, '1', 'true'].indexOf(value);
20+
return (index !== -1) ? Boolean(index > 3) : null;
2021
};
2122

2223
/**
@@ -56,7 +57,7 @@
5657
*/
5758
var returnValue = function (value) {
5859
return value;
59-
}
60+
};
6061

6162
var toUnion = function (value, key, object, configs) {
6263
var any = null;
@@ -67,7 +68,7 @@
6768
});
6869

6970
return any;
70-
}
71+
};
7172

7273
function isNativeType(typeName) {
7374
typeName = typeName.replace('[]', '');

0 commit comments

Comments
 (0)