76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav GlassYUI.add('dataschema-json', function(Y) {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
f805ad34c19740fa0c9729ce35fe59d191912f32Luke SmithProvides a DataSchema implementation which can be used to work with JSON data.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@module dataschema
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@submodule dataschema-json
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith**/
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/**
f805ad34c19740fa0c9729ce35fe59d191912f32Luke SmithProvides a DataSchema implementation which can be used to work with JSON data.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke SmithSee the `apply` method for usage.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@class DataSchema.JSON
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@extends DataSchema.Base
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@static
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith**/
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glassvar LANG = Y.Lang,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith isFunction = LANG.isFunction,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith isObject = LANG.isObject,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith isArray = LANG.isArray,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // TODO: I don't think the calls to Base.* need to be done via Base since
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Base is mixed into SchemaJSON. Investigate for later.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Base = Y.DataSchema.Base,
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith SchemaJSON;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke SmithSchemaJSON = {
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith/////////////////////////////////////////////////////////////////////////////
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith//
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith// DataSchema.JSON static methods
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith//
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith/////////////////////////////////////////////////////////////////////////////
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /**
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Utility function converts JSON locator strings into walkable paths
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith *
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @method getPath
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param locator {String} JSON value locator.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @return {String[]} Walkable path to data value.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @static
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith */
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith getPath: function(locator) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith var path = null,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith keys = [],
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith i = 0;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (locator) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Strip the ["string keys"] and [1] array indexes
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // TODO: the first two steps can probably be reduced to one with
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // /\[\s*(['"])?(.*?)\1\s*\]/g, but the array indices would be
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // stored as strings. This is not likely an issue.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith locator = locator.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith replace(/\[\s*(['"])(.*?)\1\s*\]/g,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith function (x,$1,$2) {keys[i]=$2;return '.@'+(i++);}).
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith replace(/\[(\d+)\]/g,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith function (x,$1) {keys[i]=parseInt($1,10)|0;return '.@'+(i++);}).
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith replace(/^\./,''); // remove leading dot
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Validate against problematic characters.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // commented out because the path isn't sent to eval, so it
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // should be safe. I'm not sure what makes a locator invalid.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith //if (!/[^\w\.\$@]/.test(locator)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path = locator.split('.');
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (i=path.length-1; i >= 0; --i) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (path[i].charAt(0) === '@') {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path[i] = keys[parseInt(path[i].substr(1),10)];
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /*}
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Y.log("Invalid locator: " + locator, "error", "dataschema-json");
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith */
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return path;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /**
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Utility function to walk a path and return the value located there.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith *
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @method getLocationValue
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param path {String[]} Locator path.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param data {String} Data to traverse.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @return {Object} Data value at location.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @static
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith */
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith getLocationValue: function (path, data) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith var i = 0,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith len = path.length;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (;i<len;i++) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (isObject(data) && (path[i] in data)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data = data[path[i]];
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith } else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data = undefined;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith break;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return data;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /**
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith Applies a schema to an array of data located in a JSON structure, returning
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith a normalized object with results in the `results` property. Additional
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith information can be parsed out of the JSON for inclusion in the `meta`
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith property of the response object. If an error is encountered during
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith processing, an `error` property will be added.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith The input _data_ is expected to be an object or array. If it is a string,
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith it will be passed through `Y.JSON.parse()`.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith If _data_ contains an array of data records to normalize, specify the
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith _schema.resultListLocator_ as a dot separated path string just as you would
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith reference it in JavaScript. So if your _data_ object has a record array at
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith _data.response.results_, use _schema.resultListLocator_ =
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith "response.results". Bracket notation can also be used for array indices or
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith object properties (e.g. "response['results']"); This is called a "path
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith locator"
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith Field data in the result list is extracted with field identifiers in
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith _schema.resultFields_. Field identifiers are objects with the following
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith properties:
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith * `key` : <strong>(required)</strong> The path locator (String)
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith * `parser`: A function or the name of a function on `Y.Parsers` used
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith to convert the input value into a normalized type. Parser
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith functions are passed the value as input and are expected to
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith return a value.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith If no value parsing is needed, you can use path locators (strings)
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith instead of field identifiers (objects) -- see example below.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith If no processing of the result list array is needed, _schema.resultFields_
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith can be omitted; the `response.results` will point directly to the array.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith If the result list contains arrays, `response.results` will contain an
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith array of objects with key:value pairs assuming the fields in
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith _schema.resultFields_ are ordered in accordance with the data array
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith values.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith If the result list contains objects, the identified _schema.resultFields_
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith will be used to extract a value from those objects for the output result.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith To extract additional information from the JSON, include an array of
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith path locators in _schema.metaFields_. The collected values will be
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith stored in `response.meta`.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @example
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // Process array of arrays
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith var schema = {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith resultListLocator: 'produce.fruit',
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith resultFields: [ 'name', 'color' ]
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith },
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith data = {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith produce: {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith fruit: [
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith [ 'Banana', 'yellow' ],
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith [ 'Orange', 'orange' ],
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith [ 'Eggplant', 'purple' ]
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith ]
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith };
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith var response = Y.DataSchema.JSON.apply(schema, data);
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.results[0] is { name: "Banana", color: "yellow" }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // Process array of objects + some metadata
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith schema.metaFields = [ 'lastInventory' ];
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith data = {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith produce: {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith fruit: [
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith { name: 'Banana', color: 'yellow', price: '1.96' },
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith { name: 'Orange', color: 'orange', price: '2.04' },
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith { name: 'Eggplant', color: 'purple', price: '4.31' }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith ]
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith },
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith lastInventory: '2011-07-19'
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith };
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith response = Y.DataSchema.JSON.apply(schema, data);
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.results[0] is { name: "Banana", color: "yellow" }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.meta.lastInventory is '2001-07-19'
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // Use parsers
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith schema.resultFields = [
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith key: 'name',
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith parser: function (val) { return val.toUpperCase(); }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith },
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith key: 'price',
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith parser: 'number' // Uses Y.Parsers.number
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith ];
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith response = Y.DataSchema.JSON.apply(schema, data);
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // Note price was converted from a numeric string to a number
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.results[0] looks like { fruit: "BANANA", price: 1.96 }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @method apply
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @param {Object} [schema] Schema to apply. Supported configuration
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith properties are:
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @param {String} [schema.resultListLocator] Path locator for the
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith location of the array of records to flatten into `response.results`
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @param {Array} [schema.resultFields] Field identifiers to
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith locate/assign values in the response records. See above for
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith details.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @param {Array} [schema.metaFields] Path locators to extract extra
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith non-record related information from the data object.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @param {Object|Array|String} data JSON data or its string serialization.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @return {Object} An Object with properties `results` and `meta`
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith @static
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith **/
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith apply: function(schema, data) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith var data_in = data,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out = { results: [], meta: {} };
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Convert incoming JSON strings
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (!isObject(data)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith try {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_in = Y.JSON.parse(data);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith catch(e) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.error = e;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return data_out;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (isObject(data_in) && schema) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Parse results data
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out = SchemaJSON._parseResults.call(this, schema, data_in, data_out);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Parse meta data
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (schema.metaFields !== undefined) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Y.log("JSON data could not be schema-parsed: " + Y.dump(data) + " " + Y.dump(data), "error", "dataschema-json");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.error = new Error("JSON schema parse failure");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return data_out;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /**
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Schema-parsed list of results from full data
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith *
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @method _parseResults
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param schema {Object} Schema to parse against.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param json_in {Object} JSON to parse.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param data_out {Object} In-progress parsed data to update.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @return {Object} Parsed data object.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @static
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @protected
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith */
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith _parseResults: function(schema, json_in, data_out) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith var getPath = SchemaJSON.getPath,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith getValue = SchemaJSON.getLocationValue,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path = getPath(schema.resultListLocator),
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith results = path ?
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith (getValue(path, json_in) ||
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Fall back to treat resultListLocator as a simple key
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith json_in[schema.resultListLocator]) :
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Or if no resultListLocator is supplied, use the input
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith json_in;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (isArray(results)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // if no result fields are passed in, then just take
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // the results array whole-hog Sometimes you're getting
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // an array of strings, or want the whole object, so
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // resultFields don't make sense.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (isArray(schema.resultFields)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out = SchemaJSON._getFieldValues.call(this, schema.resultFields, results, data_out);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith } else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.results = results;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
002fb6dfb3c0f32b2be7b241465918b1b520a323Luke Smith } else if (schema.resultListLocator) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.results = [];
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.error = new Error("JSON results retrieval failure");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Y.log("JSON data could not be parsed: " + Y.dump(json_in), "error", "dataschema-json");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return data_out;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /**
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Get field data values out of list of full results
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith *
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @method _getFieldValues
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param fields {Array} Fields to find.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param array_in {Array} Results to parse.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param data_out {Object} In-progress parsed data to update.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @return {Object} Parsed data object.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @static
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @protected
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith */
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith _getFieldValues: function(fields, array_in, data_out) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith var results = [],
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith len = fields.length,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith i, j,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith field, key, locator, path, parser, val,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith simplePaths = [], complexPaths = [], fieldParsers = [],
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith result, record;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // First collect hashes of simple paths, complex paths, and parsers
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (i=0; i<len; i++) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith field = fields[i]; // A field can be a simple string or a hash
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith key = field.key || field; // Find the key
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith locator = field.locator || key; // Find the locator
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Validate and store locators for later
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path = SchemaJSON.getPath(locator);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (path) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (path.length === 1) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith simplePaths.push({
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith key : key,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path: path[0]
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith });
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass } else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith complexPaths.push({
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith key : key,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path : path,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith locator: locator
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith });
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith } else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Y.log("Invalid key syntax: " + key, "warn", "dataschema-json");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Validate and store parsers for later
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith //TODO: use Y.DataSchema.parse?
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith parser = (isFunction(field.parser)) ?
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith field.parser :
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Y.Parsers[field.parser + ''];
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (parser) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith fieldParsers.push({
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith key : key,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith parser: parser
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith });
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Traverse list of array_in, creating records of simple fields,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // complex fields, and applying parsers as necessary
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (i=array_in.length-1; i>=0; --i) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith record = {};
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith result = array_in[i];
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if(result) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Cycle through complexLocators
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (j=complexPaths.length - 1; j>=0; --j) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path = complexPaths[j];
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith val = SchemaJSON.getLocationValue(path.path, result);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (val === undefined) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith val = SchemaJSON.getLocationValue([path.locator], result);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Fail over keys like "foo.bar" from nested parsing
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // to single token parsing if a value is found in
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // results["foo.bar"]
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (val !== undefined) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith simplePaths.push({
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith key: path.key,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path: path.locator
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith });
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Don't try to process the path as complex
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // for further results
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith complexPaths.splice(i,1);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith continue;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith record[path.key] = Base.parse.call(this,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith (SchemaJSON.getLocationValue(path.path, result)), path);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Cycle through simpleLocators
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (j = simplePaths.length - 1; j >= 0; --j) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path = simplePaths[j];
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Bug 1777850: The result might be an array instead of object
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith record[path.key] = Base.parse.call(this,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith ((result[path.path] === undefined) ?
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith result[j] : result[path.path]), path);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Cycle through fieldParsers
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for (j=fieldParsers.length-1; j>=0; --j) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith key = fieldParsers[j].key;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith record[key] = fieldParsers[j].parser.call(this, record[key]);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Safety net
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (record[key] === undefined) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith record[key] = null;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith results[i] = record;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.results = results;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return data_out;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith },
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith /**
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Parses results data according to schema
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith *
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @method _parseMeta
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param metaFields {Object} Metafields definitions.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param json_in {Object} JSON to parse.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param data_out {Object} In-progress parsed data to update.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @return {Object} Schema-parsed meta data.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @static
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @protected
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith */
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith _parseMeta: function(metaFields, json_in, data_out) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (isObject(metaFields)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith var key, path;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith for(key in metaFields) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (metaFields.hasOwnProperty(key)) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith path = SchemaJSON.getPath(metaFields[key]);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith if (path && json_in) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.meta[key] = SchemaJSON.getLocationValue(path, json_in);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith else {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.error = new Error("JSON meta data retrieval failure");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith return data_out;
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith }
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith};
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith// TODO: Y.Object + mix() might be better here
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke SmithY.DataSchema.JSON = Y.mix(SchemaJSON, Base);
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['dataschema-base','json']});