From bbdea8217f9cfe0401d888ce4159790022095cc1 Mon Sep 17 00:00:00 2001 From: PercheEtienne Date: Fri, 8 Jul 2022 15:59:40 +0200 Subject: [PATCH 1/7] use the right default context --- README.md | 8 ++-- .../example_with_full_possibility.js | 2 +- .../example_with_full_possibility_result.json | 2 +- test/data/EPCISDocument-AggregationEvent.json | 2 +- test/data/EPCISDocument-AssociationEvent.json | 2 +- test/data/EPCISDocument-ObjectEvent.json | 2 +- test/data/EPCISDocument-TransactionEvent.json | 2 +- .../EPCISDocument-TransformationEvent.json | 2 +- test/data/EPCISQueryDocument.json | 2 +- test/data/eventExample.js | 2 +- .../epcisDocHavingEventWithComment.json | 2 +- .../epcisDocWithCustomSchemaInContext.json | 2 +- .../epcisDocWithDefaultSchemaInContext.json | 2 +- .../data/hashing/epcisDocWithSingleEvent.json | 2 +- .../event-hash-reference-document3.json | 2 +- test/events/extendedEvent.spec.js | 2 +- test/events/objectEvent.spec.js | 14 +++---- test/validator.spec.js | 38 +++++++++---------- 18 files changed, 45 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index e81702a1..eae7324a 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ const { EPCISDocument } = require('epcis2.js'); // json object based on which we want to create an EPCIS 2.0 document const object = { - "@context": ["https://gs1.github.io/EPCIS/epcis-context.jsonld",{"example": "http://ns.example.com/epcis/"}], + "@context": ["https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld",{"example": "http://ns.example.com/epcis/"}], "id": "https://id.example.org/document1", "type": "EPCISDocument", "schemaVersion": "2.0", @@ -115,7 +115,7 @@ Running this script should result in the following EPCIS 2.0 document (printed a ```json { "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example": "http://ns.example.com/epcis/" } @@ -227,7 +227,7 @@ This example would output (as a string): ```json { "type": "EPCISDocument", - "@context": "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "@context": "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", "schemaVersion": "2.0", "creationDate": "2022-03-15T13:08:27.309Z", "epcisBody": { @@ -364,7 +364,7 @@ You can override the default values of EPCISDocument fields by providing them to You can configure the following fields: - `EPCISDocumentContext` - the '@context' property of an EPCISDocument. By default, the value is - `https://gs1.github.io/EPCIS/epcis-context.jsonld` + `https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld` - `EPCISDocumentSchemaVersion` - the 'schemaVersion' property of an EPCISDocument. By default, the value is "2.0". ```js diff --git a/example/node_example/example_with_full_possibility.js b/example/node_example/example_with_full_possibility.js index 16ce1b1d..74133c72 100644 --- a/example/node_example/example_with_full_possibility.js +++ b/example/node_example/example_with_full_possibility.js @@ -484,7 +484,7 @@ const sendACaptureRequestExample = async () => { try { const epcisDocument = new EPCISDocument(); // epcisDocument - epcisDocument.setContext(['https://gs1.github.io/EPCIS/epcis-context.jsonld',{ + epcisDocument.setContext(['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld',{ example: 'http://ns.example.com/epcis/', ext1: 'http://example.com/ext1/' }]) diff --git a/example/node_example/example_with_full_possibility_result.json b/example/node_example/example_with_full_possibility_result.json index 3f3be795..4c088ecb 100644 --- a/example/node_example/example_with_full_possibility_result.json +++ b/example/node_example/example_with_full_possibility_result.json @@ -1,7 +1,7 @@ { "type":"EPCISDocument", "@context":[ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example":"http://ns.example.com/epcis/", "ext1":"http://example.com/ext1/" diff --git a/test/data/EPCISDocument-AggregationEvent.json b/test/data/EPCISDocument-AggregationEvent.json index 989806fa..f359210c 100644 --- a/test/data/EPCISDocument-AggregationEvent.json +++ b/test/data/EPCISDocument-AggregationEvent.json @@ -201,7 +201,7 @@ ] }, "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "ext3": "http://example.com/ext3/" }, diff --git a/test/data/EPCISDocument-AssociationEvent.json b/test/data/EPCISDocument-AssociationEvent.json index 6fba230d..2060a319 100644 --- a/test/data/EPCISDocument-AssociationEvent.json +++ b/test/data/EPCISDocument-AssociationEvent.json @@ -198,7 +198,7 @@ ] }, "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "ext3": "http://example.com/ext3/" }, diff --git a/test/data/EPCISDocument-ObjectEvent.json b/test/data/EPCISDocument-ObjectEvent.json index 30723704..4dda6659 100644 --- a/test/data/EPCISDocument-ObjectEvent.json +++ b/test/data/EPCISDocument-ObjectEvent.json @@ -248,7 +248,7 @@ ] }, "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "ext3": "http://example.com/ext3/" }, diff --git a/test/data/EPCISDocument-TransactionEvent.json b/test/data/EPCISDocument-TransactionEvent.json index 580181d0..3a9b2e66 100644 --- a/test/data/EPCISDocument-TransactionEvent.json +++ b/test/data/EPCISDocument-TransactionEvent.json @@ -200,7 +200,7 @@ ] }, "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "ext3": "http://example.com/ext3/" }, diff --git a/test/data/EPCISDocument-TransformationEvent.json b/test/data/EPCISDocument-TransformationEvent.json index 16719323..452487bb 100644 --- a/test/data/EPCISDocument-TransformationEvent.json +++ b/test/data/EPCISDocument-TransformationEvent.json @@ -273,7 +273,7 @@ ] }, "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "ext3": "http://example.com/ext3/" }, diff --git a/test/data/EPCISQueryDocument.json b/test/data/EPCISQueryDocument.json index f4ad89bc..6250e8a9 100644 --- a/test/data/EPCISQueryDocument.json +++ b/test/data/EPCISQueryDocument.json @@ -1,6 +1,6 @@ { "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example": "http://ns.example.com/epcis/" } diff --git a/test/data/eventExample.js b/test/data/eventExample.js index 016e25c8..902ccdd5 100644 --- a/test/data/eventExample.js +++ b/test/data/eventExample.js @@ -551,7 +551,7 @@ export const exampleEPCISDocumentWithEPCISHeader = { ], }, '@context': [ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { ext3: 'http://example.com/ext3/', }, diff --git a/test/data/hashing/epcisDocHavingEventWithComment.json b/test/data/hashing/epcisDocHavingEventWithComment.json index ee5a542f..efc558ea 100644 --- a/test/data/hashing/epcisDocHavingEventWithComment.json +++ b/test/data/hashing/epcisDocHavingEventWithComment.json @@ -1,6 +1,6 @@ { "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example": "http://ns.example.com/epcis/" } ], diff --git a/test/data/hashing/epcisDocWithCustomSchemaInContext.json b/test/data/hashing/epcisDocWithCustomSchemaInContext.json index eb9445b4..f1d87368 100644 --- a/test/data/hashing/epcisDocWithCustomSchemaInContext.json +++ b/test/data/hashing/epcisDocWithCustomSchemaInContext.json @@ -1,6 +1,6 @@ { "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example": "http://ns.example.com/epcis/" } ], diff --git a/test/data/hashing/epcisDocWithDefaultSchemaInContext.json b/test/data/hashing/epcisDocWithDefaultSchemaInContext.json index 91d94d19..b4b51518 100644 --- a/test/data/hashing/epcisDocWithDefaultSchemaInContext.json +++ b/test/data/hashing/epcisDocWithDefaultSchemaInContext.json @@ -1,5 +1,5 @@ { - "@context": "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "@context": "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", "id": "_:document1", "type": "EPCISDocument", diff --git a/test/data/hashing/epcisDocWithSingleEvent.json b/test/data/hashing/epcisDocWithSingleEvent.json index 7d37d396..e441b84c 100644 --- a/test/data/hashing/epcisDocWithSingleEvent.json +++ b/test/data/hashing/epcisDocWithSingleEvent.json @@ -1,6 +1,6 @@ { "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example": "http://ns.example.com/epcis/" } diff --git a/test/data/hashing/event-hash-reference-document3.json b/test/data/hashing/event-hash-reference-document3.json index 54ac52fc..fbf0cdeb 100644 --- a/test/data/hashing/event-hash-reference-document3.json +++ b/test/data/hashing/event-hash-reference-document3.json @@ -1,6 +1,6 @@ { "@context": [ - "https://gs1.github.io/EPCIS/epcis-context.jsonld", + "https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld", { "example": "https://ns.example.com/epcis" } ], "type": "EPCISDocument", diff --git a/test/events/extendedEvent.spec.js b/test/events/extendedEvent.spec.js index 6770f097..cafce5af 100644 --- a/test/events/extendedEvent.spec.js +++ b/test/events/extendedEvent.spec.js @@ -40,7 +40,7 @@ describe('unit tests for the ExtendedEvent class', () => { it('should create a valid ExtendedEvent from json EPCISDocument', async () => { const doc = { type: 'EPCISDocument', - '@context': 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + '@context': 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', schemaVersion: '2', creationDate: '2022-02-21T16:28:08.813Z', epcisBody: { diff --git a/test/events/objectEvent.spec.js b/test/events/objectEvent.spec.js index 09ea213b..14840b43 100644 --- a/test/events/objectEvent.spec.js +++ b/test/events/objectEvent.spec.js @@ -70,7 +70,7 @@ describe('unit tests for the ObjectEvent class', () => { .setPersistentDisposition(new PersistentDisposition(exampleObjectEvent.persistentDisposition)) .setReadPoint(exampleObjectEvent.readPoint.id) .setBizLocation(exampleObjectEvent.bizLocation.id) - .setContext('https://gs1.github.io/EPCIS/epcis-context.jsonld') + .setContext('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld') .setIlmd(new Ilmd(exampleObjectEvent.ilmd)); expect(obj.getEPCList().toString()).to.be.equal(exampleObjectEvent.epcList.toString()); @@ -78,7 +78,7 @@ describe('unit tests for the ObjectEvent class', () => { expect(obj.getEventTime()).to.be.equal(exampleObjectEvent.eventTime); expect(obj.getEventTimeZoneOffset()).to.be.equal(exampleObjectEvent.eventTimeZoneOffset); expect(obj.getRecordTime()).to.be.equal(exampleObjectEvent.recordTime); - expect(obj.getContext()).to.be.equal('https://gs1.github.io/EPCIS/epcis-context.jsonld'); + expect(obj.getContext()).to.be.equal('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'); expect(obj['example:myField']).to.be.equal(exampleObjectEvent['example:myField']); expect(obj.getErrorDeclaration().getDeclarationTime()).to.be.equal( exampleObjectEvent.errorDeclaration.declarationTime, @@ -136,7 +136,7 @@ describe('unit tests for the ObjectEvent class', () => { const oe = new ObjectEvent(); oe.setAction(cbv.actionTypes.delete) .addEPCList(exampleObjectEvent.epcList); - assert.doesNotThrow(() => oe.generateHashID('https://gs1.github.io/EPCIS/epcis-context.jsonld')); + assert.doesNotThrow(() => oe.generateHashID('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld')); expect(oe.getEventID().startsWith('ni:///')).to.be.equal(true); expect(oe.isValid()).to.be.equal(true); }); @@ -147,9 +147,9 @@ describe('unit tests for the ObjectEvent class', () => { .addEPCList(exampleObjectEvent.epcList); assert.doesNotThrow(() => { oe.generateHashID([ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', - 'https://gs1.github.io/EPCIS/epcis-context.jsonld1', - 'https://gs1.github.io/EPCIS/epcis-context.jsonld2', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld1', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld2', ]); }); expect(oe.getEventID().startsWith('ni:///')).to.be.equal(true); @@ -190,7 +190,7 @@ describe('unit tests for the ObjectEvent class', () => { const oe = new ObjectEvent(exampleObjectEvent); assert.doesNotThrow(() => { oe.generateHashID([ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { ext3: 'http://example.com/ext3/', }, diff --git a/test/validator.spec.js b/test/validator.spec.js index 6be0711c..ff3b2ab5 100644 --- a/test/validator.spec.js +++ b/test/validator.spec.js @@ -208,7 +208,7 @@ describe('Unit test: validator.js', () => { }, ], }, - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], }; const res = validateEpcisDocument(epcisD); assert.equal(res.success, true); @@ -217,7 +217,7 @@ describe('Unit test: validator.js', () => { it('should reject incorrect EPCISDocument', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: 2, creationDate: '2005-07-11T11:30:47.0Z', @@ -249,7 +249,7 @@ describe('Unit test: validator.js', () => { it('should reject correct EPCISDocument with invalid event', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -281,8 +281,8 @@ describe('Unit test: validator.js', () => { it('should accept correct event with event extensions', () => { const epcisDocument = { '@context': { - default: 'https://gs1.github.io/EPCIS/epcis-context.jsonld', - evt: 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + default: 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', + evt: 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', }, type: 'EPCISDocument', schemaVersion: '2.0', @@ -317,7 +317,7 @@ describe('Unit test: validator.js', () => { it('should reject incorrect event with invalid event extensions', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -350,7 +350,7 @@ describe('Unit test: validator.js', () => { it('should reject incorrect event with with eventId instead of eventID', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -382,7 +382,7 @@ describe('Unit test: validator.js', () => { it('should accept correct sensorElementList with no extensions', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -445,7 +445,7 @@ describe('Unit test: validator.js', () => { it('should accept sensorElementList with extensions', () => { const epcisDocument = { '@context': [ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { example: 'https://example.com/', evt: 'https://example.com/evt/' }, ], type: 'EPCISDocument', @@ -522,7 +522,7 @@ describe('Unit test: validator.js', () => { it('should reject correct sensorElementList with invalid extensions', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -592,7 +592,7 @@ describe('Unit test: validator.js', () => { it('should accept AggregationEvent with no extensions', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -649,7 +649,7 @@ describe('Unit test: validator.js', () => { it('should reject AggregationEvent with invalid extensions', () => { const epcisDocument = { - '@context': ['https://gs1.github.io/EPCIS/epcis-context.jsonld'], + '@context': ['https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'], type: 'EPCISDocument', schemaVersion: '2.0', creationDate: '2005-07-11T11:30:47.0Z', @@ -953,7 +953,7 @@ describe('Unit test: validator.js', () => { ], }, '@context': [ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { ext3: 'http://example.com/ext3/', }, @@ -1220,7 +1220,7 @@ describe('Unit test: validator.js', () => { setup({ checkExtensions: true }); const epcisDocument = { '@context': [ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { evt: 'https://example.com/evt/' }, ], type: 'EPCISDocument', @@ -1300,7 +1300,7 @@ describe('Unit test: validator.js', () => { setup({ checkExtensions: true }); const epcisDocument = { '@context': [ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { evt: 'https://example.com/evt/', example: 'https://example.com/example/' }, ], type: 'EPCISDocument', @@ -1381,7 +1381,7 @@ describe('Unit test: validator.js', () => { setup({ checkExtensions: false }); const epcisDocument = { '@context': [ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { evt: 'https://example.com/evt/' }, ], type: 'EPCISDocument', @@ -1459,7 +1459,7 @@ describe('Unit test: validator.js', () => { it('should return the list of authorized extensions', () => { const doc = new EPCISDocument(); doc.setContext([ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { ext3: 'http://example.com/ext3/', }, @@ -1479,7 +1479,7 @@ describe('Unit test: validator.js', () => { it('should return true if all extensions are defined in the context', () => { const doc = new EPCISDocument(); doc.setContext([ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { ext3: 'http://example.com/ext3/', }, @@ -1502,7 +1502,7 @@ describe('Unit test: validator.js', () => { it('should return false if some extensions are not defined in the context', () => { const doc = new EPCISDocument(); doc.setContext([ - 'https://gs1.github.io/EPCIS/epcis-context.jsonld', + 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld', { ext3: 'http://example.com/ext3/', }, From c6f082cdda3c773f1338d1cbe61c17f1de5eb96d Mon Sep 17 00:00:00 2001 From: PercheEtienne Date: Fri, 8 Jul 2022 16:00:38 +0200 Subject: [PATCH 2/7] ensure that the default context is always present, Otherwise we'll add it --- src/entity/epcis/EPCISDocument.js | 24 +++++++++++++++++-- src/utils/utils.js | 14 ++++++++++++ test/epcis/EPCISDocument.spec.js | 38 ++++++++++++++++++++++++++++--- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/entity/epcis/EPCISDocument.js b/src/entity/epcis/EPCISDocument.js index eee49a5a..ecbee699 100644 --- a/src/entity/epcis/EPCISDocument.js +++ b/src/entity/epcis/EPCISDocument.js @@ -11,6 +11,7 @@ import objectToEvent from '../../utils/entityUtils'; import { validateEpcisDocument } from '../../schema/validator'; import Event from '../events/Event'; +import { isJsonObject } from '../../utils/utils'; export default class EPCISDocument extends Entity { /** @@ -39,6 +40,10 @@ export default class EPCISDocument extends Entity { return; } + if (isJsonObject(epcisDocument)) { + this.setContext(epcisDocument['@context']); + } + // Create classes for sub-objects that are provided Object.entries(epcisDocument).forEach(([key, value]) => { switch (key) { @@ -76,12 +81,27 @@ export default class EPCISDocument extends Entity { } /** - * Set the context property + * Set the context property, and add the default context if it is not provided * @param {string|Object|Array|Array} context * @return {EPCISDocument} - the epcisDocument instance */ setContext(context) { - this['@context'] = context; + let newContext = context; + + if (typeof context === 'string' && context !== settings.EPCISDocumentContext) { + newContext = [...context, settings.EPCISDocumentContext]; + } + + if (typeof context === 'object') { + if (Array.isArray(context)) { + if (!context.includes(settings.EPCISDocumentContext)) { + newContext.push(settings.EPCISDocumentContext); + } + } else if (isJsonObject(context)) { + newContext = [context, settings.EPCISDocumentContext]; + } + } + this['@context'] = newContext; return this; } diff --git a/src/utils/utils.js b/src/utils/utils.js index e12509d8..3b1051fa 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -224,3 +224,17 @@ export const throwIfThereIsAnUnexpectedExtension = (object) => { } }); }; + +/** + * return true if the specified object is a JSON object, false otherwise. + * @param {*} data + * @returns + */ +export function isJsonObject(data) { + try { + JSON.parse(JSON.stringify(data)); + } catch (e) { + return false; + } + return true; +} diff --git a/test/epcis/EPCISDocument.spec.js b/test/epcis/EPCISDocument.spec.js index 5ed68847..28ca7811 100644 --- a/test/epcis/EPCISDocument.spec.js +++ b/test/epcis/EPCISDocument.spec.js @@ -398,7 +398,7 @@ describe('unit tests for the EPCISDocument class', () => { describe('Context can have different types', () => { it('context can be a string', async () => { - const context = 'context'; + const context = 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'; let e = new EPCISDocument({ '@context': context }); expect(e.toObject()['@context']).to.deep.equal(context); e = new EPCISDocument().setContext(context); @@ -408,9 +408,9 @@ describe('unit tests for the EPCISDocument class', () => { it('context can be an object', async () => { const context = { key: 'value', key2: 'value2' }; let e = new EPCISDocument({ '@context': context }); - expect(e.toObject()['@context']).to.deep.equal(context); + expect(e.toObject()['@context']).to.deep.equal([context, 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld']); e = new EPCISDocument().setContext(context); - expect(e.toObject()['@context']).to.deep.equal(context); + expect(e.toObject()['@context']).to.deep.equal([context, 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld']); }); it('context can be an array of string', async () => { @@ -509,4 +509,36 @@ describe('unit tests for the EPCISDocument class', () => { assert.throws(() => epcisMasterData.addVocabularyList([new Ilmd(), new Ilmd(), 2])); }); }); + + describe('should add the default context if it is not provided', () => { + it('without any context', () => { + const epcisDocument = new EPCISDocument(); + expect(epcisDocument.toObject()['@context']).to.deep.equal(settings.EPCISDocumentContext); + }); + it('while using setContext with an object', () => { + const epcisDocument = new EPCISDocument(); + epcisDocument.setContext({ test: 'url:of:the:test' }); + expect(epcisDocument.toObject()['@context']).to.deep.equal([{ test: 'url:of:the:test' }, settings.EPCISDocumentContext]); + }); + it('while using setContext with an array of object', () => { + const epcisDocument = new EPCISDocument(); + epcisDocument.setContext([{ test: 'url:of:the:test' }, { test2: 'url:of:the:test2' }]); + expect(epcisDocument.toObject()['@context']).to.deep.equal([{ test: 'url:of:the:test' }, { test2: 'url:of:the:test2' }, settings.EPCISDocumentContext]); + }); + it('should not duplicate the default context if it is already present in the array', () => { + const epcisDocument = new EPCISDocument(); + epcisDocument.setContext([{ test: 'url:of:the:test' }, settings.EPCISDocumentContext]); + console.log(epcisDocument) + expect(epcisDocument.toObject()['@context']).to.deep.equal([{ test: 'url:of:the:test' }, settings.EPCISDocumentContext]); + }); + it('should not duplicate the default context if it is already present as a string', () => { + const epcisDocument = new EPCISDocument(); + epcisDocument.setContext('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'); + expect(epcisDocument.toObject()['@context']).to.deep.equal('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'); + }); + it('should also add it if we provide an object in EPCISDocument constructor', () => { + const epcisDocument = new EPCISDocument({'@context': 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'}); + expect(epcisDocument.toObject()['@context']).to.deep.equal('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'); + }); + }); }); From f6519d93e109eee7c7a7dcf74b852037b4af75a3 Mon Sep 17 00:00:00 2001 From: PercheEtienne Date: Fri, 8 Jul 2022 16:03:28 +0200 Subject: [PATCH 3/7] linter --- test/epcis/EPCISDocument.spec.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/epcis/EPCISDocument.spec.js b/test/epcis/EPCISDocument.spec.js index 28ca7811..39a93f52 100644 --- a/test/epcis/EPCISDocument.spec.js +++ b/test/epcis/EPCISDocument.spec.js @@ -528,7 +528,6 @@ describe('unit tests for the EPCISDocument class', () => { it('should not duplicate the default context if it is already present in the array', () => { const epcisDocument = new EPCISDocument(); epcisDocument.setContext([{ test: 'url:of:the:test' }, settings.EPCISDocumentContext]); - console.log(epcisDocument) expect(epcisDocument.toObject()['@context']).to.deep.equal([{ test: 'url:of:the:test' }, settings.EPCISDocumentContext]); }); it('should not duplicate the default context if it is already present as a string', () => { @@ -537,7 +536,7 @@ describe('unit tests for the EPCISDocument class', () => { expect(epcisDocument.toObject()['@context']).to.deep.equal('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'); }); it('should also add it if we provide an object in EPCISDocument constructor', () => { - const epcisDocument = new EPCISDocument({'@context': 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'}); + const epcisDocument = new EPCISDocument({ '@context': 'https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld' }); expect(epcisDocument.toObject()['@context']).to.deep.equal('https://ref.gs1.org/standards/epcis/2.0.0/epcis-context.jsonld'); }); }); From ed4b4c1690d65b662ccc4f1e6ab1a1a63e4feaf5 Mon Sep 17 00:00:00 2001 From: PercheEtienne Date: Fri, 8 Jul 2022 16:03:45 +0200 Subject: [PATCH 4/7] 2.4.0 --- README.md | 2 +- example/web-example/index.html | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index eae7324a..acc573a3 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ import * as epcis from 'epcis2.js'; Or use a simple script tag to load it from the CDN: ```html - + ``` Then use in a browser `script` tag using the `epcis2` global variable: diff --git a/example/web-example/index.html b/example/web-example/index.html index 4a814a54..89b08539 100644 --- a/example/web-example/index.html +++ b/example/web-example/index.html @@ -3,7 +3,7 @@ EPCIS2.js demo - +