|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | + |
| 13 | +var Lib = require('../lib'); |
| 14 | +var Plots = require('../plots/plots'); |
| 15 | +var PlotSchema = require('./plot_schema'); |
| 16 | + |
| 17 | +var isPlainObject = Lib.isPlainObject; |
| 18 | + |
| 19 | +// validation error codes |
| 20 | +var code2msgFunc = { |
| 21 | + invisible: function(path) { |
| 22 | + return 'trace ' + path + ' got defaulted to be not visible'; |
| 23 | + }, |
| 24 | + schema: function(path) { |
| 25 | + return 'key ' + path.join('.') + ' is not part of the schema'; |
| 26 | + }, |
| 27 | + unused: function(path, valIn) { |
| 28 | + var prefix = isPlainObject(valIn) ? 'container' : 'key'; |
| 29 | + |
| 30 | + return prefix + ' ' + path.join('.') + ' did not get coerced'; |
| 31 | + }, |
| 32 | + value: function(path, valIn) { |
| 33 | + return 'key ' + path.join('.') + ' is set to an invalid value (' + valIn + ')'; |
| 34 | + } |
| 35 | +}; |
| 36 | + |
| 37 | +module.exports = function valiate(data, layout) { |
| 38 | + if(!Array.isArray(data)) { |
| 39 | + throw new Error('data must be an array'); |
| 40 | + } |
| 41 | + |
| 42 | + if(!isPlainObject(layout)) { |
| 43 | + throw new Error('layout must be an object'); |
| 44 | + } |
| 45 | + |
| 46 | + var gd = { |
| 47 | + data: Lib.extendDeep([], data), |
| 48 | + layout: Lib.extendDeep({}, layout) |
| 49 | + }; |
| 50 | + Plots.supplyDefaults(gd); |
| 51 | + |
| 52 | + var schema = PlotSchema.get(); |
| 53 | + |
| 54 | + var dataOut = gd._fullData, |
| 55 | + len = data.length, |
| 56 | + dataList = new Array(len); |
| 57 | + |
| 58 | + for(var i = 0; i < len; i++) { |
| 59 | + var traceIn = data[i]; |
| 60 | + var traceList = dataList[i] = []; |
| 61 | + |
| 62 | + if(!isPlainObject(traceIn)) { |
| 63 | + throw new Error('each data trace must be an object'); |
| 64 | + } |
| 65 | + |
| 66 | + var traceOut = dataOut[i], |
| 67 | + traceType = traceOut.type, |
| 68 | + traceSchema = schema.traces[traceType].attributes; |
| 69 | + |
| 70 | + // PlotSchema does something fancy with trace 'type', reset it here |
| 71 | + // to make the trace schema compatible with Lib.validate. |
| 72 | + traceSchema.type = { |
| 73 | + valType: 'enumerated', |
| 74 | + values: [traceType] |
| 75 | + }; |
| 76 | + |
| 77 | + if(traceOut.visible === false && traceIn.visible !== false) { |
| 78 | + traceList.push(format('invisible', i)); |
| 79 | + } |
| 80 | + |
| 81 | + crawl(traceIn, traceOut, traceSchema, traceList); |
| 82 | + } |
| 83 | + |
| 84 | + var layoutOut = gd._fullLayout, |
| 85 | + layoutSchema = fillLayoutSchema(schema, dataOut), |
| 86 | + layoutList = []; |
| 87 | + |
| 88 | + crawl(layout, layoutOut, layoutSchema, layoutList); |
| 89 | + |
| 90 | + return { |
| 91 | + data: dataList, |
| 92 | + layout: layoutList |
| 93 | + }; |
| 94 | +}; |
| 95 | + |
| 96 | +function crawl(objIn, objOut, schema, list, path) { |
| 97 | + path = path || []; |
| 98 | + |
| 99 | + var keys = Object.keys(objIn); |
| 100 | + |
| 101 | + for(var i = 0; i < keys.length; i++) { |
| 102 | + var k = keys[i]; |
| 103 | + |
| 104 | + var p = path.slice(); |
| 105 | + p.push(k); |
| 106 | + |
| 107 | + var valIn = objIn[k], |
| 108 | + valOut = objOut[k]; |
| 109 | + |
| 110 | + if(isPlainObject(valIn) && isPlainObject(valOut)) { |
| 111 | + crawl(valIn, valOut, schema[k], list, p); |
| 112 | + } |
| 113 | + else if(!(k in schema)) { |
| 114 | + list.push(format('schema', p)); |
| 115 | + } |
| 116 | + else if(schema[k].items && Array.isArray(valIn)) { |
| 117 | + var itemName = k.substr(0, k.length - 1); |
| 118 | + |
| 119 | + for(var j = 0; j < valIn.length; j++) { |
| 120 | + p[p.length - 1] = k + '[' + j + ']'; |
| 121 | + |
| 122 | + crawl(valIn[j], valOut[j], schema[k].items[itemName], list, p); |
| 123 | + } |
| 124 | + } |
| 125 | + else if(!(k in objOut)) { |
| 126 | + list.push(format('unused', p, valIn)); |
| 127 | + } |
| 128 | + else if(!Lib.validate(valIn, schema[k])) { |
| 129 | + list.push(format('value', p, valIn)); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + return list; |
| 134 | +} |
| 135 | + |
| 136 | +// the 'full' layout schema depends on the traces types presents |
| 137 | +function fillLayoutSchema(schema, dataOut) { |
| 138 | + for(var i = 0; i < dataOut.length; i++) { |
| 139 | + var traceType = dataOut[i].type, |
| 140 | + traceLayoutAttr = schema.traces[traceType].layoutAttributes; |
| 141 | + |
| 142 | + if(traceLayoutAttr) { |
| 143 | + Lib.extendFlat(schema.layout.layoutAttributes, traceLayoutAttr); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return schema.layout.layoutAttributes; |
| 148 | +} |
| 149 | + |
| 150 | +function format(code, path, valIn) { |
| 151 | + return { |
| 152 | + code: code, |
| 153 | + path: path, |
| 154 | + msg: code2msgFunc[code](path, valIn) |
| 155 | + }; |
| 156 | +} |
0 commit comments