All files / module/helpers validateResourceObject.js

0% Statements 0/6
0% Branches 0/11
0% Functions 0/1
0% Lines 0/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                                                     
import { hasOwn } from '../../shared/utils'
 
/**
 * Do a crude check for common json:api resource properties.
 * This is (probably) not a long-term solution!
 *
 * Assumptions made:
 *
 * - Resource Objects always have a type and a data section
 * - newly created resources must not have an id (this is slightly
 *   stricter than the specification allows for and may be changed
 *   in the future)
 * - every other resource object should have an id
 *
 * @param {Object} resourceObject
 * @param {Boolean} isCreateResource
 */
export function validateResourceObject (resourceObject, isCreateResource) {
  const conformsToBasicStructure = typeof resourceObject === 'object' &&
    hasOwn(resourceObject, 'type') &&
    hasOwn(resourceObject, 'data')
 
  if (isCreateResource) {
    return conformsToBasicStructure &&
      !hasOwn(resourceObject, 'id')
  }
 
  if (!isCreateResource) {
    return conformsToBasicStructure &&
      hasOwn(resourceObject, 'id')
  }
 
  throw new Error('Detected potentially invalid json:api resource object')
}