| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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("Invocation of form " + |
| 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 // Handle integer -0 as 0. |
| 122 if (JSONSchemaValidator.getType(args[ai]) === "integer" && args[ai] === 0) |
| 123 args[ai] = 0; |
| 121 if (definedSignature[si] === resolvedSignature[ai]) | 124 if (definedSignature[si] === resolvedSignature[ai]) |
| 122 $Array.push(normalizedArgs, args[ai++]); | 125 $Array.push(normalizedArgs, args[ai++]); |
| 123 else | 126 else |
| 124 $Array.push(normalizedArgs, null); | 127 $Array.push(normalizedArgs, null); |
| 125 } | 128 } |
| 126 return normalizedArgs; | 129 return normalizedArgs; |
| 127 }; | 130 }; |
| 128 | 131 |
| 129 // Validates that a given schema for an API function is not ambiguous. | 132 // Validates that a given schema for an API function is not ambiguous. |
| 130 function isFunctionSignatureAmbiguous(functionDef) { | 133 function isFunctionSignatureAmbiguous(functionDef) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 147 return true; | 150 return true; |
| 148 } | 151 } |
| 149 } | 152 } |
| 150 return false; | 153 return false; |
| 151 }; | 154 }; |
| 152 | 155 |
| 153 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous; | 156 exports.isFunctionSignatureAmbiguous = isFunctionSignatureAmbiguous; |
| 154 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate; | 157 exports.normalizeArgumentsAndValidate = normalizeArgumentsAndValidate; |
| 155 exports.schemaValidator = schemaValidator; | 158 exports.schemaValidator = schemaValidator; |
| 156 exports.validate = validate; | 159 exports.validate = validate; |
| OLD | NEW |