Skip to content

Commit 4a20b6f

Browse files
committed
Check default @language BCP47 format.
- Emit event when invalid. - Add tests.
1 parent d2ef05d commit 4a20b6f

File tree

4 files changed

+98
-1
lines changed

4 files changed

+98
-1
lines changed

lib/context.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const {
2424
} = require('./events');
2525

2626
const {
27+
REGEX_BCP47,
2728
asArray: _asArray,
2829
compareShortestLeast: _compareShortestLeast
2930
} = require('./util');
@@ -243,6 +244,22 @@ api.process = async ({
243244
'jsonld.SyntaxError',
244245
{code: 'invalid default language', context: ctx});
245246
} else {
247+
if(!value.match(REGEX_BCP47)) {
248+
if(options.eventHandler) {
249+
_handleEvent({
250+
event: {
251+
type: ['JsonLdEvent'],
252+
code: 'invalid @language value',
253+
level: 'warning',
254+
message: '@language value must be valid BCP47.',
255+
details: {
256+
language: value
257+
}
258+
},
259+
options
260+
});
261+
}
262+
}
246263
rval['@language'] = value.toLowerCase();
247264
}
248265
defined.set('@language', true);

lib/expand.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const {
3333
} = require('./url');
3434

3535
const {
36+
REGEX_BCP47,
3637
addValue: _addValue,
3738
asArray: _asArray,
3839
getValues: _getValues,
@@ -45,7 +46,6 @@ const {
4546

4647
const api = {};
4748
module.exports = api;
48-
const REGEX_BCP47 = /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/;
4949

5050
/**
5151
* Recursively expands an element using the given context. Any context in

lib/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const IdentifierIssuer = require('rdf-canonize').IdentifierIssuer;
1010
const JsonLdError = require('./JsonLdError');
1111

1212
// constants
13+
const REGEX_BCP47 = /^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$/;
1314
const REGEX_LINK_HEADERS = /(?:<[^>]*?>|"[^"]*?"|[^,])+/g;
1415
const REGEX_LINK_HEADER = /\s*<([^>]*?)>\s*(?:;\s*(.*))?/;
1516
const REGEX_LINK_HEADER_PARAMS =
@@ -24,6 +25,7 @@ const DEFAULTS = {
2425
const api = {};
2526
module.exports = api;
2627
api.IdentifierIssuer = IdentifierIssuer;
28+
api.REGEX_BCP47 = REGEX_BCP47;
2729

2830
/**
2931
* Clones an object, array, Map, Set, or string/number. If a typed JavaScript

tests/misc.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,6 +1358,84 @@ describe('events', () => {
13581358
testNotStrict: true
13591359
});
13601360
});
1361+
1362+
it('should emit for invalid @language value', async () => {
1363+
const input =
1364+
{
1365+
"urn:property": {
1366+
"@language": "en_us",
1367+
"@value": "test"
1368+
}
1369+
}
1370+
;
1371+
const expected =
1372+
[
1373+
{
1374+
"urn:property": [
1375+
{
1376+
"@language": "en_us",
1377+
"@value": "test"
1378+
}
1379+
]
1380+
}
1381+
]
1382+
;
1383+
1384+
console.error('FIXME');
1385+
await _test({
1386+
type: 'expand',
1387+
input,
1388+
expected,
1389+
mapCounts: {},
1390+
eventCounts: {
1391+
codes: {
1392+
'invalid @language value': 1
1393+
},
1394+
events: 1
1395+
},
1396+
testNotSafe: true,
1397+
testNotStrict: true
1398+
});
1399+
});
1400+
1401+
it('should emit for invalid default @language value', async () => {
1402+
const input =
1403+
{
1404+
"@context": {
1405+
"@language": "en_us"
1406+
},
1407+
"urn:property": "value"
1408+
}
1409+
;
1410+
const expected =
1411+
[
1412+
{
1413+
"urn:property": [
1414+
{
1415+
"@language": "en_us",
1416+
"@value": "value"
1417+
}
1418+
]
1419+
}
1420+
]
1421+
;
1422+
1423+
console.error('FIXME');
1424+
await _test({
1425+
type: 'expand',
1426+
input,
1427+
expected,
1428+
mapCounts: {},
1429+
eventCounts: {
1430+
codes: {
1431+
'invalid @language value': 1
1432+
},
1433+
events: 1
1434+
},
1435+
testNotSafe: true,
1436+
testNotStrict: true
1437+
});
1438+
});
13611439
});
13621440

13631441
describe('unmappedProperty', () => {

0 commit comments

Comments
 (0)