Home | History | Annotate | Download | only in resources

Lines Matching refs:schema

11 // This file contains a class that implements a subset of JSON Schema.
12 // See: http://www.json.com/json-schema-proposal/ for more details.
14 // The following features of JSON Schema are not implemented:
31 // There are also these departures from the JSON Schema proposal:
36 // - by default an "object" typed schema does not allow additional properties.
37 // if present, "additionalProperties" is to be a schema against which all
64 * Validates an instance against a schema and accumulates errors. Usage:
67 * validator.validate(inst, schema);
103 schemaRequired: "Schema value required.",
104 unknownSchemaReference: "Unknown schema reference: *.",
123 * Classifies a value as one of the JSON schema primitive types. Note that we
150 * with "$ref": <typeId>. Each type must be a valid schema and define an
170 * Returns a list of strings of the types that this schema accepts.
172 JSONSchemaValidator.prototype.getAllTypesForSchema = function(schema) {
174 if (schema.type)
175 $Array.push(schemaTypes, schema.type);
176 if (schema.choices) {
177 for (var i = 0; i < schema.choices.length; i++) {
178 var choiceTypes = this.getAllTypesForSchema(schema.choices[i]);
182 var ref = schema['$ref'];
198 * Returns true if |schema| would accept an argument of type |type|.
200 JSONSchemaValidator.prototype.isValidSchemaType = function(type, schema) {
205 if (schema.optional && (type == "null" || type == "undefined"))
208 var schemaTypes = this.getAllTypesForSchema(schema);
232 * Validates an instance against a schema. The instance can be any JavaScript
236 JSONSchemaValidator.prototype.validate = function(instance, schema, opt_path) {
239 if (!schema) {
244 // If this schema defines itself as reference type, save it in this.types.
245 if (schema.id)
246 this.types[schema.id] = schema;
248 // If the schema has an extends property, the instance must validate against
249 // that schema too.
250 if (schema.extends)
251 this.validate(instance, schema.extends, path);
253 // If the schema has a $ref property, the instance must validate against
254 // that schema too. It must be present in this.types to be referenced.
255 var ref = schema["$ref"];
263 // If the schema has a choices property, the instance must validate against at
265 if (schema.choices) {
266 this.validateChoices(instance, schema, path);
270 // If the schema has an enum property, the instance must be one of those
272 if (schema.enum) {
273 if (!this.validateEnum(instance, schema, path))
277 if (schema.type && schema.type != "any") {
278 if (!this.validateType(instance, schema, path))
282 switch (schema.type) {
284 this.validateObject(instance, schema, path);
287 this.validateArray(instance, schema, path);
290 this.validateString(instance, schema, path);
294 this.validateNumber(instance, schema, path);
301 * Validates an instance against a choices schema. The instance must match at
305 function(instance, schema, path) {
308 for (var i = 0; i < schema.choices.length; i++) {
310 this.validate(instance, schema.choices[i], path);
322 * Validates an instance against a schema with an enum type. Populates the
326 JSONSchemaValidator.prototype.validateEnum = function(instance, schema, path) {
327 for (var i = 0; i < schema.enum.length; i++) {
328 if (instance === enumToString(schema.enum[i]))
333 [schema.enum.map(enumToString).join(", ")]);
338 * Validates an instance against an object schema and populates the errors
342 function(instance, schema, path) {
343 if (schema.properties) {
344 for (var prop in schema.properties) {
347 // schema properties.
349 // there are other checks we could put here, like requiring that schema
351 if (!$Object.hasOwnProperty(schema.properties, prop))
355 if (schema.properties[prop] == undefined) {
358 this.validate(instance[prop], schema.properties[prop], propPath);
359 } else if (!schema.properties[prop].optional) {
367 if (schema.isInstanceOf) {
368 if (!isInstanceOfClass(instance, schema.isInstanceOf))
369 this.addError(propPath, "notInstance", [schema.isInstanceOf]);
373 if (schema.additionalProperties &&
374 schema.additionalProperties.type &&
375 schema.additionalProperties.type == "any") {
380 // can be overridden by setting the additionalProperties property to a schema
383 if (schema.properties && prop in schema.properties)
391 if (schema.additionalProperties)
392 this.validate(instance[prop], schema.additionalProperties, propPath);
399 * Validates an instance against an array schema and populates the errors
402 JSONSchemaValidator.prototype.validateArray = function(instance, schema, path) {
403 var typeOfItems = JSONSchemaValidator.getType(schema.items);
406 if (schema.minItems && instance.length < schema.minItems) {
407 this.addError(path, "arrayMinItems", [schema.minItems]);
410 if (typeof schema.maxItems != "undefined" &&
411 instance.length > schema.maxItems) {
412 this.addError(path, "arrayMaxItems", [schema.maxItems]);
415 // If the items property is a single schema, each item in the array must
416 // have that schema.
418 this.validate(instance[i], schema.items, path + "." + i);
422 // validate against the corresponding schema.
423 for (var i = 0; i < schema.items.length; i++) {
426 this.validate(instance[i], schema.items[i], itemPath);
427 } else if (!schema.items[i].optional) {
432 if (schema.additionalProperties) {
433 for (var i = schema.items.length; i < instance.length; i++) {
435 this.validate(instance[i], schema.additionalProperties, itemPath);
438 if (instance.length > schema.items.length) {
439 this.addError(path, "arrayMaxItems", [schema.items.length]);
449 function(instance, schema, path) {
450 if (schema.minLength && instance.length < schema.minLength)
451 this.addError(path, "stringMinLength", [schema.minLength]);
453 if (schema.maxLength && instance.length > schema.maxLength)
454 this.addError(path, "stringMaxLength", [schema.maxLength]);
456 if (schema.pattern && !schema.pattern.test(instance))
457 this.addError(path, "stringPattern", [schema.pattern]);
465 function(instance, schema, path) {
474 if (schema.minimum !== undefined && instance < schema.minimum)
475 this.addError(path, "numberMinValue", [schema.minimum]);
477 if (schema.maximum !== undefined && instance > schema.maximum)
478 this.addError(path, "numberMaxValue", [schema.maximum]);
481 if (schema.type === "integer" && (instance | 0) !== instance)
484 if (schema.maxDecimal && instance * Math.pow(10, schema.maxDecimal) % 1)
485 this.addError(path, "numberMaxDecimal", [schema.maxDecimal]);
492 JSONSchemaValidator.prototype.validateType = function(instance, schema, path) {
494 if (schema.type == actualType ||
495 (schema.type == "number" && actualType == "integer")) {
497 } else if (schema.type == "integer" && actualType == "number") {
501 this.addError(path, "invalidType", [schema.type, actualType]);