|
1 | 1 | import {
|
2 | 2 | mergeDeepRight,
|
3 | 3 | clone,
|
4 |
| - path, |
5 | 4 | reduce,
|
6 | 5 | isEmpty,
|
7 | 6 | equals
|
@@ -112,44 +111,36 @@ const traverse = (data, schema, func) => {
|
112 | 111 | let output = {}
|
113 | 112 | if (data instanceof Array) {
|
114 | 113 | output = data.map(item => {
|
115 |
| - if (schema.items.type) { |
116 |
| - if ([Object, Array].includes(schema.items.type)) { |
117 |
| - item = traverse(item, schema.items, func) |
118 |
| - } else { |
119 |
| - item = func(item, schema.items) |
120 |
| - } |
| 114 | + if ([Object, Array].includes(schema.items.type)) { |
| 115 | + item = traverse(item, schema.items, func) |
| 116 | + } else { |
| 117 | + item = func(item, schema.items) |
121 | 118 | }
|
122 | 119 | return item
|
123 | 120 | })
|
124 | 121 | } else if (data instanceof Object) {
|
125 | 122 | for (let key in data) {
|
126 |
| - validateSchema(schema, key) |
127 |
| - if (schema[key] && schema[key].items) { |
128 |
| - output[key] = traverse(data[key], schema[key], func) |
129 |
| - } else if (schema[key] && schema[key].properties) { |
130 |
| - output[key] = traverse(data[key], schema[key].properties, func) |
131 |
| - } else if (schema && schema.properties) { |
| 123 | + if (key in schema) { |
| 124 | + if (schema[key].items) { |
| 125 | + output[key] = traverse(data[key], schema[key], func) |
| 126 | + } else if (schema[key].properties) { |
| 127 | + output[key] = traverse(data[key], schema[key].properties, func) |
| 128 | + } else { |
| 129 | + output[key] = func(data[key], schema[key]) |
| 130 | + } |
| 131 | + } else if (schema.properties) { |
132 | 132 | output = traverse(data, schema.properties, func)
|
133 |
| - // output[key] = func(data[key], schema.properties[key]) |
134 |
| - } else if (key in schema) { |
135 |
| - output[key] = func(data[key], schema[key]) |
136 | 133 | } else {
|
| 134 | + console.warn('Key missing from schema: ' + key) |
137 | 135 | output[key] = data[key]
|
138 | 136 | }
|
139 | 137 | }
|
140 | 138 | } else {
|
141 |
| - // found a normal key: value prop |
142 | 139 | output = func(data, schema)
|
143 | 140 | }
|
144 | 141 | return output
|
145 | 142 | }
|
146 | 143 |
|
147 |
| -const validateSchema = (schema, key) => { |
148 |
| - if ((schema[key] === undefined) && (path(['properties', key], schema) === undefined)) { |
149 |
| - console.warn(`Key missing from schema: ${key}`) |
150 |
| - } |
151 |
| -} |
152 |
| - |
153 | 144 | // encode a model property
|
154 | 145 |
|
155 | 146 | export const encodeProperty = (data, schema = {}) => {
|
|
0 commit comments