OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Routines used to validate and normalize arguments. | 5 // Routines used to validate and normalize arguments. |
6 // TODO(benwells): unit test this file. | 6 // TODO(benwells): unit test this file. |
7 | 7 |
8 var JSONSchemaValidator = require('json_schema').JSONSchemaValidator; | 8 var JSONSchemaValidator = require('json_schema').JSONSchemaValidator; |
9 | 9 |
10 var schemaValidator = new JSONSchemaValidator(); | 10 var schemaValidator = new JSONSchemaValidator(); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // Example return value for chrome.windows.getCurrent: | 79 // Example return value for chrome.windows.getCurrent: |
80 // "windows.getCurrent(optional object populate, function callback)" | 80 // "windows.getCurrent(optional object populate, function callback)" |
81 function getParameterSignatureString(name, definedSignature) { | 81 function getParameterSignatureString(name, definedSignature) { |
82 var getSchemaTypeString = function(schema) { | 82 var getSchemaTypeString = function(schema) { |
83 var schemaTypes = schemaValidator.getAllTypesForSchema(schema); | 83 var schemaTypes = schemaValidator.getAllTypesForSchema(schema); |
84 var typeName = schemaTypes.join(" or ") + " " + schema.name; | 84 var typeName = schemaTypes.join(" or ") + " " + schema.name; |
85 if (schema.optional) | 85 if (schema.optional) |
86 return "optional " + typeName; | 86 return "optional " + typeName; |
87 return typeName; | 87 return typeName; |
88 }; | 88 }; |
89 var typeNames = definedSignature.map(getSchemaTypeString); | 89 var typeNames = $Array.map(definedSignature, getSchemaTypeString); |
90 return name + "(" + typeNames.join(", ") + ")"; | 90 return name + "(" + typeNames.join(", ") + ")"; |
91 }; | 91 }; |
92 | 92 |
93 // Returns a string representing a call to an API function. | 93 // Returns a string representing a call to an API function. |
94 // Example return value for call: chrome.windows.get(1, callback) is: | 94 // Example return value for call: chrome.windows.get(1, callback) is: |
95 // "windows.get(int, function)" | 95 // "windows.get(int, function)" |
96 function getArgumentSignatureString(name, args) { | 96 function getArgumentSignatureString(name, args) { |
97 var typeNames = args.map(JSONSchemaValidator.getType); | 97 var typeNames = $Array.map(args, JSONSchemaValidator.getType); |
98 return name + "(" + typeNames.join(", ") + ")"; | 98 return name + "(" + typeNames.join(", ") + ")"; |
99 }; | 99 }; |
100 | 100 |
101 // Finds the correct signature for the given arguments, then validates the | 101 // Finds the correct signature for the given arguments, then validates the |
102 // arguments against that signature. Returns a 'normalized' arguments list | 102 // arguments against that signature. Returns a 'normalized' arguments list |
103 // where nulls are inserted where optional parameters were omitted. | 103 // where nulls are inserted where optional parameters were omitted. |
104 // |args| is expected to be an array. | 104 // |args| is expected to be an array. |
105 function normalizeArgumentsAndValidate(args, funDef) { | 105 function normalizeArgumentsAndValidate(args, funDef) { |
106 if (funDef.allowAmbiguousOptionalArguments) { | 106 if (funDef.allowAmbiguousOptionalArguments) { |
107 validate(args, funDef.definition.parameters); | 107 validate(args, funDef.definition.parameters); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 return true; | 150 return true; |
151 } | 151 } |
152 } | 152 } |
153 return false; | 153 return false; |
154 }; | 154 }; |
155 | 155 |
156 exports.$set('isFunctionSignatureAmbiguous', isFunctionSignatureAmbiguous); | 156 exports.$set('isFunctionSignatureAmbiguous', isFunctionSignatureAmbiguous); |
157 exports.$set('normalizeArgumentsAndValidate', normalizeArgumentsAndValidate); | 157 exports.$set('normalizeArgumentsAndValidate', normalizeArgumentsAndValidate); |
158 exports.$set('schemaValidator', schemaValidator); | 158 exports.$set('schemaValidator', schemaValidator); |
159 exports.$set('validate', validate); | 159 exports.$set('validate', validate); |
OLD | NEW |