Skip to content
This repository has been archived by the owner on Oct 17, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8 from standardhealth/flux_b
Browse files Browse the repository at this point in the history
Eliminate namespaces from properties from JSON Schema
  • Loading branch information
cmoesel authored May 11, 2018
2 parents ed88b92 + af5aa50 commit 273ffd0
Show file tree
Hide file tree
Showing 29 changed files with 235 additions and 209 deletions.
57 changes: 41 additions & 16 deletions lib/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,29 @@ function namespaceToSchema(ns, dataElementsSpecs, baseSchemaURL, baseTypeURL) {
}
}
if (def.fields.length) {
const fieldNameMap = {};
const clashingNames = {};
for (const field of def.fields) {
if (!(field instanceof TBD) && !isValidField(field) || (field.inheritance === INHERITED)) {
continue;
if (!(field instanceof TBD)) {
if (!isValidField(field)) {
continue;
} else if (field.inheritance === INHERITED) {
if (fieldNameMap[field.identifier.name]) {
logger.error(`ERROR: clashing property names: %s and %s ERROR_CODE: 12038`, fieldNameMap[field.identifier.name].fqn, field.identifier.fqn);
clashingNames[field.identifier.name] = true;
} else {
fieldNameMap[field.identifier.name] = field.identifier;
}
continue;
}

if (fieldNameMap[field.identifier.name]) {
logger.error(`ERROR: clashing property names: %s and %s ERROR_CODE: 12038`, fieldNameMap[field.identifier.name].fqn, field.identifier.fqn);
clashingNames[field.identifier.name] = true;
continue;
} else {
fieldNameMap[field.identifier.name] = field.identifier;
}
}
const card = field.effectiveCard;
if (card && card.isZeroedOut) {
Expand All @@ -149,11 +169,16 @@ function namespaceToSchema(ns, dataElementsSpecs, baseSchemaURL, baseTypeURL) {
needsEntryType = false;
}

schemaDef.properties[field.identifier.fqn] = value;
schemaDef.properties[field.identifier.name] = value;
if (required) {
requiredProperties.push(field.identifier.fqn);
requiredProperties.push(field.identifier.name);
}
}

for (const clashingName in clashingNames) {
delete schemaDef.properties[clashingName];
}
requiredProperties = requiredProperties.filter(propName => !(propName in clashingNames));
} else if (!def.value) {
schemaDef.type = 'object';
schemaDef.description = 'Empty DataElement?';
Expand All @@ -177,9 +202,9 @@ function namespaceToSchema(ns, dataElementsSpecs, baseSchemaURL, baseTypeURL) {
wholeDef.description = descriptionList.join('\n');
}
if (needsEntryType) {
schemaDef.properties['shr.base.EntryType'] = nonEntryEntryTypeField;
schemaDef.properties['EntryType'] = nonEntryEntryTypeField;
if (def.identifier.fqn !== 'shr.base.EntryType') {
requiredProperties.push('shr.base.EntryType');
requiredProperties.push('EntryType');
}
}

Expand Down Expand Up @@ -265,10 +290,10 @@ function flatNamespaceToSchema(ns, dataElementsSpecs, baseSchemaURL, baseTypeURL
continue;
}

const qualifiedName = field.identifier.fqn;
schemaDef.properties[qualifiedName] = value;
if (required && (requiredProperties.indexOf(qualifiedName) === -1)) {
requiredProperties.push(qualifiedName);
const fieldName = field.identifier.name;
schemaDef.properties[fieldName] = value;
if (required && (requiredProperties.indexOf(fieldName) === -1)) {
requiredProperties.push(fieldName);
}
}
if (def.isEntry) {
Expand Down Expand Up @@ -753,11 +778,11 @@ function makeRef(id, enclosingNamespace, baseSchemaURL) {
function makeShrRefObject(refs, baseTypeURL, target = {}) {
target.type = 'object';
target.properties = {
ShrId: { type: 'string' },
EntryId: { type: 'string' },
EntryType: { type: 'string' }
_ShrId: { type: 'string' },
_EntryId: { type: 'string' },
_EntryType: { type: 'string' }
};
target.required = ['ShrId', 'EntryType', 'EntryId'];
target.required = ['_ShrId', '_EntryType', '_EntryId'];
target.refType = refs.map((ref) => `${baseTypeURL}${namespaceToURLPathSegment(ref.identifier.namespace)}/${ref.identifier.name}`);
return target;
}
Expand Down Expand Up @@ -903,7 +928,7 @@ function extractConstraintPath(constraint, valueDef, dataElementSpecs) {
}
return {};
}
normalizedPath.push(pathId.fqn);
normalizedPath.push(pathId.name);
}
}
currentDef = newDef;
Expand Down Expand Up @@ -961,7 +986,7 @@ function extractUnnormalizedConstraintPath(constraint, valueDef, dataElementSpec
}
return {};
}
normalizedPath.push(pathId.fqn);
normalizedPath.push(pathId.name);
}
}
currentDef = newDef;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shr-json-schema-export",
"version": "5.2.3",
"version": "5.3.0",
"description": "Exports SHR data elements from SHR models to JSON Schema",
"author": "",
"license": "Apache-2.0",
Expand Down
22 changes: 11 additions & 11 deletions test/export_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ const stubSchema = JSON.parse(`{
{
"type": "object",
"properties": {
"shr.base.ShrId": {
"ShrId": {
"$ref": "#/definitions/RequiredString"
},
"shr.base.EntryId": {
"EntryId": {
"$ref": "#/definitions/RequiredString"
},
"shr.core.CreationTime": {
"CreationTime": {
"type": "object",
"properties": {
"Value": {
Expand All @@ -125,15 +125,15 @@ const stubSchema = JSON.parse(`{
},
"required": [ "Value" ]
},
"shr.base.LastUpdated": {
"LastUpdated": {
"$ref": "#/definitions/RequiredString"
}
},
"required": [
"shr.base.ShrId",
"shr.base.EntryId",
"shr.core.CreationTime",
"shr.base.LastUpdated"
"ShrId",
"EntryId",
"CreationTime",
"LastUpdated"
]
}
]
Expand All @@ -154,16 +154,16 @@ const stubSchema = JSON.parse(`{
"type": "object",
"properties": {
"Value": { "type": "string", "format": "uri" },
"shr.base.EntryType": { "$ref": "#/definitions/EntryType" }
"EntryType": { "$ref": "#/definitions/EntryType" }
},
"required": [ "Value" ]
},
"Any": {
"type": "object",
"properties": {
"shr.base.EntryType": { "$ref": "#/definitions/EntryType" }
"EntryType": { "$ref": "#/definitions/EntryType" }
},
"required": [ "shr.base.EntryType" ]
"required": [ "EntryType" ]
}
}
}`);
10 changes: 5 additions & 5 deletions test/fixtures/AbstractAndPlainGroup.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
{
"type": "object",
"properties": {
"shr.test.Simple": { "$ref": "#/definitions/Simple" },
"shr.test.Plain": { "$ref": "#/definitions/Plain" }
"Simple": { "$ref": "#/definitions/Simple" },
"Plain": { "$ref": "#/definitions/Plain" }
},
"required": ["shr.test.Simple", "shr.test.Plain"]
"required": ["Simple", "Plain"]
}
]
},
Expand All @@ -44,12 +44,12 @@
"concepts": [ { "code": "bar", "codeSystem": "http://foo.org", "displayText": "Foobar"}],
"type": "object",
"properties": {
"shr.base.EntryType": { "$ref": "https://standardhealthrecord.org/test/shr/base#/definitions/EntryType" },
"EntryType": { "$ref": "https://standardhealthrecord.org/test/shr/base#/definitions/EntryType" },
"Value": {
"type": "string"
}
},
"required": ["Value", "shr.base.EntryType"]
"required": ["Value", "EntryType"]
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions test/fixtures/BooleanAndCodeConstraints.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{ "enum": [ true ] }
]
},
"shr.test.Bool": {
"Bool": {
"allOf": [
{
"properties": {
Expand All @@ -36,7 +36,7 @@
{ "$ref": "#/definitions/Bool" }
]
},
"shr.test.Coded": {
"Coded": {
"properties": {
"Value": {
"code": { "code": "bar", "codeSystem": "http://foo.org", "displayText": "Foobar"}
Expand All @@ -52,12 +52,12 @@
"type": "object",
"description": "A boolean element.",
"properties": {
"shr.base.EntryType": {"$ref": "https://standardhealthrecord.org/test/shr/base#/definitions/EntryType" },
"EntryType": {"$ref": "https://standardhealthrecord.org/test/shr/base#/definitions/EntryType" },
"Value": {
"type": "boolean"
}
},
"required": ["shr.base.EntryType"]
"required": ["EntryType"]
},
"Group": {
"description": "It is a group of elements",
Expand All @@ -70,17 +70,17 @@
{
"type": "object",
"properties": {
"shr.test.Simple": { "$ref": "#/definitions/Simple" },
"shr.test.Coded": { "$ref": "#/definitions/Coded" },
"shr.test.ElementValue": {
"Simple": { "$ref": "#/definitions/Simple" },
"Coded": { "$ref": "#/definitions/Coded" },
"ElementValue": {
"type": "array",
"minItems": 0,
"items": {
"$ref": "#/definitions/ElementValue"
}
}
},
"required": ["shr.test.Simple"]
"required": ["Simple"]
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/ChoiceValueSetConstraint.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
"type": "object",
"properties": {
"shr.test.CodedChoice": {
"CodedChoice": {
"allOf": [
{
"properties": {
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/Group.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
{
"type": "object",
"properties": {
"shr.test.Simple": { "$ref": "#/definitions/Simple" },
"shr.test.Coded": { "$ref": "#/definitions/Coded" },
"shr.test.ElementValue": {
"Simple": { "$ref": "#/definitions/Simple" },
"Coded": { "$ref": "#/definitions/Coded" },
"ElementValue": {
"type": "array",
"minItems": 0,
"items": {
"$ref": "#/definitions/ElementValue"
}
}
},
"required": ["shr.test.Simple"]
"required": ["Simple"]
}
]
},
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/GroupDerivative.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@
{
"type": "object",
"properties": {
"shr.test.Simple": { "$ref": "#/definitions/Simple" },
"shr.test.Coded": { "$ref": "#/definitions/Coded" },
"shr.test.ElementValue": {
"Simple": { "$ref": "#/definitions/Simple" },
"Coded": { "$ref": "#/definitions/Coded" },
"ElementValue": {
"type": "array",
"minItems": 0,
"items": {
"$ref": "#/definitions/ElementValue"
}
}
},
"required": ["shr.test.Simple"]
"required": ["Simple"]
}
]
},
Expand Down
8 changes: 1 addition & 7 deletions test/fixtures/GroupPathClash.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@
{ "$ref": "https://standardhealthrecord.org/test/shr/base#/definitions/Entry" },
{
"type": "object",
"properties": {
"shr.test.Simple": { "$ref": "#/definitions/Simple" },
"shr.other.test.Simple": {
"$ref": "https://standardhealthrecord.org/test/shr/other/test#/definitions/Simple"
}
},
"required": ["shr.test.Simple"]
"properties": {}
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/GroupPathClash_errors.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"shrId": "shr.test.GroupPathClash",
"level": 50,
"msg": "ERROR: clashing property names: shr.test.Simple and shr.other.test.Simple ERROR_CODE: 12038"
}
]
6 changes: 3 additions & 3 deletions test/fixtures/GroupWithChoiceOfChoice.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
]
}
},
"shr.test.Simple": { "$ref": "#/definitions/Simple" },
"shr.test.Coded": { "$ref": "#/definitions/Coded" }
"Simple": { "$ref": "#/definitions/Simple" },
"Coded": { "$ref": "#/definitions/Coded" }
},
"required": [ "shr.test.Simple" ]
"required": [ "Simple" ]
}
]
},
Expand Down
Loading

0 comments on commit 273ffd0

Please sign in to comment.