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(); |
11 | 11 |
12 // Validate arguments. | 12 // Validate arguments. |
13 function validate(args, parameterSchemas) { | 13 function validate(args, parameterSchemas) { |
14 if (args.length > parameterSchemas.length) | 14 if (args.length > parameterSchemas.length) |
15 throw new Error("Too many arguments."); | 15 throw new $Error.self("Too many arguments."); |
16 for (var i = 0; i < parameterSchemas.length; i++) { | 16 for (var i = 0; i < parameterSchemas.length; i++) { |
17 if (i in args && args[i] !== null && args[i] !== undefined) { | 17 if (i in args && args[i] !== null && args[i] !== undefined) { |
18 schemaValidator.resetErrors(); | 18 schemaValidator.resetErrors(); |
19 schemaValidator.validate(args[i], parameterSchemas[i]); | 19 schemaValidator.validate(args[i], parameterSchemas[i]); |
20 if (schemaValidator.errors.length == 0) | 20 if (schemaValidator.errors.length == 0) |
21 continue; | 21 continue; |
22 var message = "Invalid value for argument " + (i + 1) + ". "; | 22 var message = "Invalid value for argument " + (i + 1) + ". "; |
23 for (var i = 0, err; | 23 for (var i = 0, err; |
24 err = schemaValidator.errors[i]; i++) { | 24 err = schemaValidator.errors[i]; i++) { |
25 if (err.path) { | 25 if (err.path) { |
26 message += "Property '" + err.path + "': "; | 26 message += "Property '" + err.path + "': "; |
27 } | 27 } |
28 message += err.message; | 28 message += err.message; |
29 message = message.substring(0, message.length - 1); | 29 message = message.substring(0, message.length - 1); |
30 message += ", "; | 30 message += ", "; |
31 } | 31 } |
32 message = message.substring(0, message.length - 2); | 32 message = message.substring(0, message.length - 2); |
33 message += "."; | 33 message += "."; |
34 throw new Error(message); | 34 throw new $Error.self(message); |
35 } else if (!parameterSchemas[i].optional) { | 35 } else if (!parameterSchemas[i].optional) { |
36 throw new Error("Parameter " + (i + 1) + " (" + | 36 throw new $Error.self("Parameter " + (i + 1) + " (" + |
37 parameterSchemas[i].name + ") is required."); | 37 parameterSchemas[i].name + ") is required."); |
38 } | 38 } |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 // Generate all possible signatures for a given API function. | 42 // Generate all possible signatures for a given API function. |
43 function getSignatures(parameterSchemas) { | 43 function getSignatures(parameterSchemas) { |
44 if (parameterSchemas.length === 0) | 44 if (parameterSchemas.length === 0) |
45 return [[]]; | 45 return [[]]; |
46 var signatures = []; | 46 var signatures = []; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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); |
108 return args; | 108 return args; |
109 } | 109 } |
110 var definedSignature = funDef.definition.parameters; | 110 var definedSignature = funDef.definition.parameters; |
111 var resolvedSignature = resolveSignature(args, definedSignature); | 111 var resolvedSignature = resolveSignature(args, definedSignature); |
112 if (!resolvedSignature) | 112 if (!resolvedSignature) |
113 throw new Error("Invocation of form " + | 113 throw new $Error.self("Invocation of form " + |
not at google - send to devlin
2014/08/19 16:45:56
new Error() for this whole file.
| |
114 getArgumentSignatureString(funDef.name, args) + | 114 getArgumentSignatureString(funDef.name, args) + |
115 " doesn't match definition " + | 115 " doesn't match definition " + |
116 getParameterSignatureString(funDef.name, definedSignature)); | 116 getParameterSignatureString(funDef.name, definedSignature)); |
117 validate(args, resolvedSignature); | 117 validate(args, resolvedSignature); |
118 var normalizedArgs = []; | 118 var normalizedArgs = []; |
119 var ai = 0; | 119 var ai = 0; |
120 for (var si = 0; si < definedSignature.length; si++) { | 120 for (var si = 0; si < definedSignature.length; si++) { |
121 if (definedSignature[si] === resolvedSignature[ai]) | 121 if (definedSignature[si] === resolvedSignature[ai]) |
122 $Array.push(normalizedArgs, args[ai++]); | 122 $Array.push(normalizedArgs, args[ai++]); |
123 else | 123 else |
(...skipping 23 matching lines...) Expand all Loading... | |
147 return true; | 147 return true; |
148 } | 148 } |
149 } | 149 } |
150 return false; | 150 return false; |
151 }; | 151 }; |
152 | 152 |
153 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous; | 153 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous; |
154 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate; | 154 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate; |
155 exports.schemaValidator = schemaValidator; | 155 exports.schemaValidator = schemaValidator; |
156 exports.validate = validate; | 156 exports.validate = validate; |
OLD | NEW |