|
20 | 20 | const addComma = `
|
21 | 21 | if (addComma) {
|
22 | 22 | json += ','
|
| 23 | + } else { |
| 24 | + addComma = true |
23 | 25 | }
|
24 |
| - addComma = true |
25 | 26 | `
|
26 | 27 |
|
27 | 28 | function isValidSchema (schema, name) {
|
@@ -1247,39 +1248,52 @@ function nested (laterCode, name, key, location, subKey, isArray) {
|
1247 | 1248 | const nullIndex = type.indexOf('null')
|
1248 | 1249 | const sortedTypes = nullIndex !== -1 ? [type[nullIndex]].concat(type.slice(0, nullIndex)).concat(type.slice(nullIndex + 1)) : type
|
1249 | 1250 | sortedTypes.forEach((type, index) => {
|
| 1251 | + const statement = index === 0 ? 'if' : 'else if' |
1250 | 1252 | const tempSchema = Object.assign({}, schema, { type })
|
1251 | 1253 | const nestedResult = nested(laterCode, name, key, mergeLocation(location, { schema: tempSchema }), subKey, isArray)
|
1252 |
| - |
1253 |
| - if (type === 'string') { |
1254 |
| - code += ` |
1255 |
| - ${index === 0 ? 'if' : 'else if'}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp || (typeof obj${accessor} === "object" && Object.hasOwnProperty.call(obj${accessor}, "toString"))) |
1256 |
| - ${nestedResult.code} |
1257 |
| - ` |
1258 |
| - } else if (type === 'null') { |
1259 |
| - code += ` |
1260 |
| - ${index === 0 ? 'if' : 'else if'}(obj${accessor} == null) |
1261 |
| - ${nestedResult.code} |
1262 |
| - ` |
1263 |
| - } else if (type === 'array') { |
1264 |
| - code += ` |
1265 |
| - ${index === 0 ? 'if' : 'else if'}(Array.isArray(obj${accessor})) |
1266 |
| - ${nestedResult.code} |
1267 |
| - ` |
1268 |
| - } else if (type === 'integer') { |
1269 |
| - code += ` |
1270 |
| - ${index === 0 ? 'if' : 'else if'}(Number.isInteger(obj${accessor}) || obj${accessor} === null) |
1271 |
| - ${nestedResult.code} |
1272 |
| - ` |
1273 |
| - } else if (type === 'number') { |
1274 |
| - code += ` |
1275 |
| - ${index === 0 ? 'if' : 'else if'}(isNaN(obj${accessor}) === false) |
1276 |
| - ${nestedResult.code} |
1277 |
| - ` |
1278 |
| - } else { |
1279 |
| - code += ` |
1280 |
| - ${index === 0 ? 'if' : 'else if'}(typeof obj${accessor} === "${type}") |
1281 |
| - ${nestedResult.code} |
1282 |
| - ` |
| 1254 | + switch (type) { |
| 1255 | + case 'string': { |
| 1256 | + code += ` |
| 1257 | + ${statement}(obj${accessor} === null || typeof obj${accessor} === "${type}" || obj${accessor} instanceof Date || typeof obj${accessor}.toISOString === "function" || obj${accessor} instanceof RegExp || (typeof obj${accessor} === "object" && Object.hasOwnProperty.call(obj${accessor}, "toString"))) |
| 1258 | + ${nestedResult.code} |
| 1259 | + ` |
| 1260 | + break |
| 1261 | + } |
| 1262 | + case 'null': { |
| 1263 | + code += ` |
| 1264 | + ${statement}(obj${accessor} == null) |
| 1265 | + ${nestedResult.code} |
| 1266 | + ` |
| 1267 | + break |
| 1268 | + } |
| 1269 | + case 'array': { |
| 1270 | + code += ` |
| 1271 | + ${statement}(Array.isArray(obj${accessor})) |
| 1272 | + ${nestedResult.code} |
| 1273 | + ` |
| 1274 | + break |
| 1275 | + } |
| 1276 | + case 'integer': { |
| 1277 | + code += ` |
| 1278 | + ${statement}(Number.isInteger(obj${accessor}) || obj${accessor} === null) |
| 1279 | + ${nestedResult.code} |
| 1280 | + ` |
| 1281 | + break |
| 1282 | + } |
| 1283 | + case 'number': { |
| 1284 | + code += ` |
| 1285 | + ${statement}(isNaN(obj${accessor}) === false) |
| 1286 | + ${nestedResult.code} |
| 1287 | + ` |
| 1288 | + break |
| 1289 | + } |
| 1290 | + default: { |
| 1291 | + code += ` |
| 1292 | + ${statement}(typeof obj${accessor} === "${type}") |
| 1293 | + ${nestedResult.code} |
| 1294 | + ` |
| 1295 | + break |
| 1296 | + } |
1283 | 1297 | }
|
1284 | 1298 | laterCode = nestedResult.laterCode
|
1285 | 1299 | })
|
|
0 commit comments