| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 function assert(truth) { | 5 function assert(truth) { |
| 6 if (!truth) | 6 if (!truth) |
| 7 throw new Error("Assertion failed."); | 7 throw new Error("Assertion failed."); |
| 8 } | 8 } |
| 9 | 9 |
| 10 function assertValid(type, instance, schema, types) { | 10 function assertValid(type, instance, schema, types) { |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 assertValid("", 42, schema); | 160 assertValid("", 42, schema); |
| 161 assertNotValid("", "42", schema, | 161 assertNotValid("", "42", schema, |
| 162 [formatError("invalidType", ["number", "string"])]); | 162 [formatError("invalidType", ["number", "string"])]); |
| 163 | 163 |
| 164 // Make the derived schema more restrictive | 164 // Make the derived schema more restrictive |
| 165 parent.minimum = 43; | 165 parent.minimum = 43; |
| 166 assertNotValid("", 42, schema, [formatError("numberMinValue", [43])]); | 166 assertNotValid("", 42, schema, [formatError("numberMinValue", [43])]); |
| 167 assertValid("", 43, schema); | 167 assertValid("", 43, schema); |
| 168 } | 168 } |
| 169 | 169 |
| 170 function ClassA() { | |
| 171 this.a = "a"; | |
| 172 } | |
| 173 function ClassB() { | |
| 174 } | |
| 175 ClassB.prototype = new ClassA(); | |
| 176 function ClassC() { | |
| 177 this.a = "a"; | |
| 178 } | |
| 179 | |
| 180 function testObject() { | 170 function testObject() { |
| 181 var schema = { | 171 var schema = { |
| 182 properties: { | 172 properties: { |
| 183 "foo": { | 173 "foo": { |
| 184 type: "string" | 174 type: "string" |
| 185 }, | 175 }, |
| 186 "bar": { | 176 "bar": { |
| 187 type: "integer" | 177 type: "integer" |
| 188 } | 178 } |
| 189 } | 179 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 207 [formatError("invalidType", ["boolean", "string"])]); | 197 [formatError("invalidType", ["boolean", "string"])]); |
| 208 | 198 |
| 209 schema.properties.bar.optional = true; | 199 schema.properties.bar.optional = true; |
| 210 assertValid("Object", {foo:"foo", bar:42}, schema); | 200 assertValid("Object", {foo:"foo", bar:42}, schema); |
| 211 assertValid("Object", {foo:"foo"}, schema); | 201 assertValid("Object", {foo:"foo"}, schema); |
| 212 assertNotValid("Object", {foo:"foo", bar:null}, schema, | 202 assertNotValid("Object", {foo:"foo", bar:null}, schema, |
| 213 [formatError("invalidType", ["integer", "null"])]); | 203 [formatError("invalidType", ["integer", "null"])]); |
| 214 assertValid("Object", {foo:"foo", bar:undefined}, schema); | 204 assertValid("Object", {foo:"foo", bar:undefined}, schema); |
| 215 assertNotValid("Object", {foo:"foo", bar:"42"}, schema, | 205 assertNotValid("Object", {foo:"foo", bar:"42"}, schema, |
| 216 [formatError("invalidType", ["integer", "string"])]); | 206 [formatError("invalidType", ["integer", "string"])]); |
| 217 | |
| 218 var classASchema = { | |
| 219 properties: { | |
| 220 "a": { type: "string" } | |
| 221 }, | |
| 222 isInstanceOf: "ClassA" | |
| 223 }; | |
| 224 | |
| 225 var classBSchema = { | |
| 226 properties: {}, | |
| 227 isInstanceOf: "ClassB" | |
| 228 }; | |
| 229 | |
| 230 var a = new ClassA(); | |
| 231 var b = new ClassB(); | |
| 232 var c = new ClassC(); | |
| 233 | |
| 234 assertValid("Object", a, classASchema); | |
| 235 assertValid("Object", b, classBSchema); | |
| 236 assertValid("Object", b, classASchema); | |
| 237 assertNotValid("Object", c, classASchema, | |
| 238 [formatError("notInstance", [classASchema.isInstanceOf])]); | |
| 239 } | 207 } |
| 240 | 208 |
| 241 function testTypeReference() { | 209 function testTypeReference() { |
| 242 var referencedTypes = [ | 210 var referencedTypes = [ |
| 243 { | 211 { |
| 244 id: "MinLengthString", | 212 id: "MinLengthString", |
| 245 type: "string", | 213 type: "string", |
| 246 minLength: 2 | 214 minLength: 2 |
| 247 }, | 215 }, |
| 248 { | 216 { |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 [formatError("invalidType", ["integer", "number"])]); | 442 [formatError("invalidType", ["integer", "number"])]); |
| 475 assertNotValid("Type", 1, {type: "boolean"}, | 443 assertNotValid("Type", 1, {type: "boolean"}, |
| 476 [formatError("invalidType", ["boolean", "integer"])]); | 444 [formatError("invalidType", ["boolean", "integer"])]); |
| 477 assertNotValid("Type", false, {type: "null"}, | 445 assertNotValid("Type", false, {type: "null"}, |
| 478 [formatError("invalidType", ["null", "boolean"])]); | 446 [formatError("invalidType", ["null", "boolean"])]); |
| 479 assertNotValid("Type", undefined, {type: "null"}, | 447 assertNotValid("Type", undefined, {type: "null"}, |
| 480 [formatError("invalidType", ["null", "undefined"])]); | 448 [formatError("invalidType", ["null", "undefined"])]); |
| 481 assertNotValid("Type", {}, {type: "function"}, | 449 assertNotValid("Type", {}, {type: "function"}, |
| 482 [formatError("invalidType", ["function", "object"])]); | 450 [formatError("invalidType", ["function", "object"])]); |
| 483 } | 451 } |
| OLD | NEW |