f805ad34c19740fa0c9729ce35fe59d191912f32Luke SmithProvides a DataSchema implementation which can be used to work with JSON data.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@module dataschema
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@submodule dataschema-json
f805ad34c19740fa0c9729ce35fe59d191912f32Luke SmithProvides a DataSchema implementation which can be used to work with JSON data.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke SmithSee the `apply` method for usage.
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith@extends DataSchema.Base
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/////////////////////////////////////////////////////////////////////////////
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith// DataSchema.JSON static methods
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith/////////////////////////////////////////////////////////////////////////////
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Utility function converts JSON locator strings into walkable paths
10d8bafc5c24f3a4285cf6060a1935ba5cfc4b85Luke Smith * @method getPath
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @param locator {String} JSON value locator.
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * @return {String[]} Walkable path to data value.
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 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 // 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 Y.log("Invalid locator: " + locator, "error", "dataschema-json");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Utility function to walk a path and return the value located there.
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 for (;i<len;i++) {
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 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 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 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 * `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 If no value parsing is needed, you can use path locators (strings)
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith instead of field identifiers (objects) -- see example below.
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 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 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 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 // Process array of arrays
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith var schema = {
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith resultListLocator: 'produce.fruit',
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith resultFields: [ 'name', 'color' ]
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith [ 'Banana', 'yellow' ],
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith [ 'Orange', 'orange' ],
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith [ 'Eggplant', 'purple' ]
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith var response = Y.DataSchema.JSON.apply(schema, data);
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.results[0] is { name: "Banana", color: "yellow" }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // Process array of objects + some metadata
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith schema.metaFields = [ 'lastInventory' ];
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 lastInventory: '2011-07-19'
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith response = Y.DataSchema.JSON.apply(schema, data);
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.results[0] is { name: "Banana", color: "yellow" }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // response.meta.lastInventory is '2001-07-19'
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith // Use parsers
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith schema.resultFields = [
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith key: 'name',
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith parser: function (val) { return val.toUpperCase(); }
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith key: 'price',
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith parser: 'number' // Uses Y.Parsers.number
f805ad34c19740fa0c9729ce35fe59d191912f32Luke Smith response = Y.DataSchema.JSON.apply(schema, data);
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 @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 @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`
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Convert incoming JSON strings
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Parse results data
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out = SchemaJSON._parseResults.call(this, schema, data_in, data_out);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Parse meta data
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out = SchemaJSON._parseMeta(schema.metaFields, data_in, data_out);
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 * Schema-parsed list of results from full data
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 * @protected
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith _parseResults: function(schema, json_in, data_out) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Fall back to treat resultListLocator as a simple key
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Or if no resultListLocator is supplied, use the input
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 data_out = SchemaJSON._getFieldValues.call(this, schema.resultFields, results, data_out);
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 * Get field data values out of list of full results
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 * @protected
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith _getFieldValues: function(fields, array_in, data_out) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith simplePaths = [], complexPaths = [], fieldParsers = [],
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // First collect hashes of simple paths, complex paths, and parsers
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith field = fields[i]; // A field can be a simple string or a hash
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith locator = field.locator || key; // Find the locator
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Validate and store locators for later
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith Y.log("Invalid key syntax: " + key, "warn", "dataschema-json");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Validate and store parsers for later
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith //TODO: use Y.DataSchema.parse?
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Traverse list of array_in, creating records of simple fields,
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // complex fields, and applying parsers as necessary
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Cycle through complexLocators
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith val = SchemaJSON.getLocationValue(path.path, result);
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 // Don't try to process the path as complex
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // for further results
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith (SchemaJSON.getLocationValue(path.path, result)), path);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Cycle through simpleLocators
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Bug 1777850: The result might be an array instead of object
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Cycle through fieldParsers
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith record[key] = fieldParsers[j].parser.call(this, record[key]);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith // Safety net
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith * Parses results data according to schema
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 * @protected
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith _parseMeta: function(metaFields, json_in, data_out) {
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.meta[key] = SchemaJSON.getLocationValue(path, json_in);
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith data_out.error = new Error("JSON meta data retrieval failure");
df5496ef3926fd0ea425cd4aa5eb278364c98d5cLuke Smith// TODO: Y.Object + mix() might be better here
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass}, '@VERSION@' ,{requires:['dataschema-base','json']});