Skip to content

Commit 0625bc3

Browse files
authored
Merge pull request #210 from rtibblesbot/issue-209-cf2cc6
Fix language regex to allow multi-subtag codes like zh-Hans-CN
2 parents 11b7241 + ef1c64b commit 0625bc3

7 files changed

Lines changed: 69 additions & 6 deletions

js/EmbedContentRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const SCHEMA = {
1818
"language": {
1919
"type": "string",
2020
"description": "Language code from https://github.com/learningequality/le-utils/blob/main/le_utils/resources/languagelookup.json",
21-
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)?$"
21+
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)*$"
2222
},
2323
"url": {
2424
"type": "string",

js/EmbedTopicsRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const SCHEMA = {
2626
"language": {
2727
"type": "string",
2828
"description": "Language code from https://github.com/learningequality/le-utils/blob/main/le_utils/resources/languagelookup.json",
29-
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)?$"
29+
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)*$"
3030
},
3131
"level": {
3232
"type": "integer",

le_utils/constants/embed_content_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"language": {
2525
"type": "string",
2626
"description": "Language code from https://github.com/learningequality/le-utils/blob/main/le_utils/resources/languagelookup.json",
27-
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)?$",
27+
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)*$",
2828
},
2929
"url": {
3030
"type": "string",

le_utils/constants/embed_topics_request.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"language": {
3030
"type": "string",
3131
"description": "Language code from https://github.com/learningequality/le-utils/blob/main/le_utils/resources/languagelookup.json",
32-
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)?$",
32+
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)*$",
3333
},
3434
"level": {
3535
"type": "integer",

spec/schema-embed_content_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"language": {
1414
"type": "string",
1515
"description": "Language code from https://github.com/learningequality/le-utils/blob/main/le_utils/resources/languagelookup.json",
16-
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)?$"
16+
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)*$"
1717
},
1818
"url": {
1919
"type": "string",

spec/schema-embed_topics_request.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"language": {
2222
"type": "string",
2323
"description": "Language code from https://github.com/learningequality/le-utils/blob/main/le_utils/resources/languagelookup.json",
24-
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)?$"
24+
"pattern": "^[a-z]{2,3}(?:-[a-zA-Z]+)*$"
2525
},
2626
"level": {
2727
"type": "integer",

tests/test_schemas.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from __future__ import unicode_literals
33

44
import contextlib
5+
import json
6+
import pkgutil
57
import uuid
68

79
import pytest
@@ -61,6 +63,14 @@ def _validate_completion_criteria(data):
6163
)
6264

6365

66+
def _load_language_codes():
67+
"""Return all language codes from languagelookup.json."""
68+
data = pkgutil.get_data("le_utils", "resources/languagelookup.json")
69+
codes = list(json.loads(data).keys())
70+
assert len(codes) > 0, "languagelookup.json should not be empty"
71+
return codes
72+
73+
6474
@contextlib.contextmanager
6575
def _assert_not_raises(not_expected):
6676
try:
@@ -479,6 +489,31 @@ def test_embed__topics__missing_channel_id():
479489
)
480490

481491

492+
@skip_if_jsonschema_unavailable
493+
def test_embed__topics__all_languagelookup_codes_valid():
494+
"""Every language code in languagelookup.json must be accepted by the topics schema.
495+
496+
Regression test for https://github.com/learningequality/le-utils/issues/209
497+
where codes like zh-Hans-CN were rejected.
498+
"""
499+
test_id = str(uuid.uuid4())
500+
for code in _load_language_codes():
501+
with _assert_not_raises(jsonschema.ValidationError):
502+
_validate_embed_topics_request(
503+
{
504+
"topics": [
505+
{
506+
"id": test_id,
507+
"channel_id": test_id,
508+
"title": "Test topic",
509+
"description": "Test description",
510+
"language": code,
511+
}
512+
],
513+
}
514+
)
515+
516+
482517
@skip_if_jsonschema_unavailable
483518
def test_embed__content__valid():
484519
with _assert_not_raises(jsonschema.ValidationError):
@@ -703,6 +738,34 @@ def test_embed__content__invalid_preset_files():
703738
)
704739

705740

741+
@skip_if_jsonschema_unavailable
742+
def test_embed__content__all_languagelookup_codes_valid():
743+
"""Every language code in languagelookup.json must be accepted by the content schema.
744+
745+
Regression test for https://github.com/learningequality/le-utils/issues/209
746+
where codes like zh-Hans-CN were rejected.
747+
"""
748+
test_id = str(uuid.uuid4())
749+
for code in _load_language_codes():
750+
with _assert_not_raises(jsonschema.ValidationError):
751+
_validate_embed_content_request(
752+
{
753+
"resources": [
754+
{
755+
"id": test_id,
756+
"channel_id": test_id,
757+
"title": "Resource title",
758+
"description": "Resource description",
759+
"text": "Resource text",
760+
"language": code,
761+
"content_id": test_id,
762+
"channel_version": 1,
763+
},
764+
],
765+
}
766+
)
767+
768+
706769
def _validate_learning_objectives(data):
707770
"""
708771
:param data: Dictionary of data to validate

0 commit comments

Comments
 (0)