UNPKG

fhirbuilder

Version:
22 lines (21 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AssertModelFieldsMatchAttributes = AssertModelFieldsMatchAttributes; function getNonFunctionKeys(obj) { return Object.keys(obj).filter((key) => typeof obj[key] !== 'function'); } /** * @description Asserts that the model fields match the attributes of the model * @param expectedAttributes - the expected attributes of the model * @param instance - the instance of the model * @constructor AssertModelFieldsMatchAttributes * @throws {Error} if the model fields do not match the attributes of the model */ function AssertModelFieldsMatchAttributes(expectedAttributes, instance) { const expectedFieldNames = expectedAttributes.map((attribute) => attribute.name); const actualFieldNames = getNonFunctionKeys(instance); const unmatchedFields = actualFieldNames.filter((field) => !expectedFieldNames.includes(field)); if (unmatchedFields.length > 0) { throw new Error(`The instance of ${instance.constructor.name} is missing expected fields: ${unmatchedFields.join(', ')}`); } }