(appId, collectionName, document, accessList, isMasterKey, encryption_key)
| 350 | } |
| 351 | |
| 352 | async function _isSchemaValid(appId, collectionName, document, accessList, isMasterKey, encryption_key) { |
| 353 | const mainPromise = q.defer(); |
| 354 | let columnNotFound = false; |
| 355 | |
| 356 | try { |
| 357 | const promises = []; |
| 358 | if (!document) { |
| 359 | await mainPromise.reject('Document is undefined'); |
| 360 | } |
| 361 | const modifiedDocuments = document._modifiedColumns; |
| 362 | const table = await tableService.getSchema(appId, collectionName); |
| 363 | const columns = table.columns; |
| 364 | // check for required. |
| 365 | if (!document._tableName || !document._type) { |
| 366 | await mainPromise.reject('Not a type of table'); |
| 367 | } |
| 368 | for (let i = 0; i < columns.length; i++) { |
| 369 | if (columns[i].name === 'id') continue; // ignore. |
| 370 | |
| 371 | if (document[columns[i].name] === undefined) { |
| 372 | // TODO : check type for defaultValue , convert to date of type is DateTime , quick patch , fix properly later |
| 373 | if (columns[i].dataType === 'DateTime') { |
| 374 | try { |
| 375 | columns[i].defaultValue = new Date(columns[i].defaultValue); |
| 376 | } catch (e) { |
| 377 | columns[i].defaultValue = null; |
| 378 | } |
| 379 | } |
| 380 | document[columns[i].name] = columns[i].defaultValue; |
| 381 | } |
| 382 | |
| 383 | if (columns[i].dataType === 'File' && document[columns[i].name] && document[columns[i].name]._type && document[columns[i].name]._type === 'file' && !document[columns[i].name]._id) { // if url of the file is null, which means file is not saved. Remove the whole object. |
| 384 | document[columns[i].name] = null; |
| 385 | } |
| 386 | |
| 387 | // if column datatype is bool, and data is null, change data to false by default. |
| 388 | if (columns[i].dataType === 'Boolean' && !document[columns[i].name]) { |
| 389 | document[columns[i].name] = false; |
| 390 | } |
| 391 | |
| 392 | if (columns[i].required) { |
| 393 | if (document[columns[i].name] === null || document[columns[i].name] === undefined) { |
| 394 | await mainPromise.reject(`${columns[i].name} is required`); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | // Is Editable only by master key is true? |
| 400 | if (columns[i].editableByMasterKey && modifiedDocuments.indexOf(columns[i].name) > -1) { |
| 401 | if (!isMasterKey) { |
| 402 | mainPromise.reject(`${columns[i].name} is only editable by Master Key.`); |
| 403 | return mainPromise.promise; |
| 404 | } |
| 405 | } |
| 406 | // This code encrypts the password in the documents. It shouldn't be here in validateSchema - Let's have it here for temp. |
| 407 | if (columns[i].dataType === 'EncryptedText') { |
| 408 | if (document[columns[i].name] && typeof document[columns[i].name] === 'string' && document._modifiedColumns.indexOf(columns[i].name) !== -1) { |
| 409 | document[columns[i].name] = _encrypt(document[columns[i].name], encryption_key); |
no test coverage detected