Skip to content

Commit

Permalink
fix: UriTemplate Error
Browse files Browse the repository at this point in the history
  • Loading branch information
nickevansuk committed Mar 3, 2021
1 parent 83115ae commit 652c8d9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
11 changes: 1 addition & 10 deletions src/classes/field.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const UriTemplate = require('uritemplate');
const DataModelHelper = require('../helpers/data-model');
const PropertyHelper = require('../helpers/property');

Expand Down Expand Up @@ -162,15 +161,7 @@ const Field = class {
if (!isEnum) {
// Is this a URL template?
// This processes most strings... so could be a bit intensive
const template = UriTemplate.parse(data);
let isUrlTemplate = false;
for (const expression of template.expressions) {
if (expression.constructor.name === 'VariableExpression') {
isUrlTemplate = true;
break;
}
}
if (isUrlTemplate) {
if (PropertyHelper.isUrlTemplate(data)) {
returnType = 'https://schema.org/Text';
} else if (this.constructor.URL_REGEX.test(data)) {
returnType = 'https://schema.org/URL';
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/property-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const PropertyHelper = require('./property');

describe('PropertyHelper', () => {
describe('isUrlTemplate', () => {
it('should return true for template', async () => {
const result = PropertyHelper.isUrlTemplate('{test}');
expect(result).toBe(true);
});
it('should return false for string', async () => {
const result = PropertyHelper.isUrlTemplate('test');
expect(result).toBe(false);
});
it('should return false for erroneous string', async () => {
const result = PropertyHelper.isUrlTemplate('{description}{something-else}.');
expect(result).toBe(false);
});
});
});
15 changes: 15 additions & 0 deletions src/helpers/property.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const crypto = require('crypto');
const UriTemplate = require('uritemplate');
const DataModelHelper = require('./data-model');

const PropertyHelper = class {
Expand Down Expand Up @@ -183,6 +184,20 @@ const PropertyHelper = class {
return keyChecks;
}

static isUrlTemplate(data) {
try {
const template = UriTemplate.parse(data);
for (const expression of template.expressions) {
if (expression.constructor.name === 'VariableExpression') {
return true;
}
}
} catch (e) {
return false;
}
return false;
}

static clearCache() {
this.enumCache = {};
this.propertyCache = {};
Expand Down

0 comments on commit 652c8d9

Please sign in to comment.