Skip to content

Commit 7aa39cf

Browse files
committed
Update content keywords to only apply to string instances
1 parent e12a540 commit 7aa39cf

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

annotations/tests/content.json

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"title": "`contentMediaType` is an annotation",
3+
"title": "`contentMediaType` is an annotation for string instances",
44
"schema": {
55
"contentMediaType": "application/json"
66
},
@@ -14,11 +14,21 @@
1414
"expected": ["application/json"]
1515
}
1616
]
17+
},
18+
{
19+
"instance": 42,
20+
"assertions": [
21+
{
22+
"location": "#",
23+
"keyword": "contentMediaType",
24+
"expected": []
25+
}
26+
]
1727
}
1828
]
1929
},
2030
{
21-
"title": "`contentEncoding` is an annotation",
31+
"title": "`contentEncoding` is an annotation for string instances",
2232
"schema": {
2333
"contentEncoding": "base64"
2434
},
@@ -32,13 +42,24 @@
3242
"expected": ["base64"]
3343
}
3444
]
45+
},
46+
{
47+
"instance": 42,
48+
"assertions": [
49+
{
50+
"location": "#",
51+
"keyword": "contentEncoding",
52+
"expected": []
53+
}
54+
]
3555
}
3656
]
3757
},
3858
{
39-
"title": "`contentSchema` is an annotation",
59+
"title": "`contentSchema` is an annotation for string instances",
4060
"schema": {
4161
"$id": "https://annotations.json-schema.org/test/contentSchema-is-an-annotation",
62+
"contentMediaType": "application/json",
4263
"contentSchema": { "type": "number" }
4364
},
4465
"subjects": [
@@ -51,6 +72,35 @@
5172
"expected": ["https://annotations.json-schema.org/test/contentSchema-is-an-annotation#/contentSchema"]
5273
}
5374
]
75+
},
76+
{
77+
"instance": 42,
78+
"assertions": [
79+
{
80+
"location": "#",
81+
"keyword": "contentSchema",
82+
"expected": []
83+
}
84+
]
85+
}
86+
]
87+
},
88+
{
89+
"title": "`contentSchema` requires `contentMediaType`",
90+
"schema": {
91+
"$id": "https://annotations.json-schema.org/test/contentSchema-is-an-annotation",
92+
"contentSchema": { "type": "number" }
93+
},
94+
"subjects": [
95+
{
96+
"instance": "42",
97+
"assertions": [
98+
{
99+
"location": "#",
100+
"keyword": "contentSchema",
101+
"expected": []
102+
}
103+
]
54104
}
55105
]
56106
}

lib/keywords/contentEncoding.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import * as Browser from "@hyperjump/browser";
2+
import * as Instance from "../instance.js";
23

34

45
const id = "https://json-schema.org/keyword/contentEncoding";
56

67
const compile = (schema) => Browser.value(schema);
78
const interpret = () => true;
8-
const annotation = (contentEncoding) => contentEncoding;
9+
10+
const annotation = (contentEncoding, instance) => {
11+
if (Instance.typeOf(instance) !== "string") {
12+
return;
13+
}
14+
15+
return contentEncoding;
16+
};
917

1018
export default { id, compile, interpret, annotation };

lib/keywords/contentMediaType.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import * as Browser from "@hyperjump/browser";
2+
import * as Instance from "../instance.js";
23

34

45
const id = "https://json-schema.org/keyword/contentMediaType";
56

67
const compile = (schema) => Browser.value(schema);
78
const interpret = () => true;
8-
const annotation = (contentMediaType) => contentMediaType;
9+
10+
const annotation = (contentMediaType, instance) => {
11+
if (Instance.typeOf(instance) !== "string") {
12+
return;
13+
}
14+
15+
return contentMediaType;
16+
};
917

1018
export default { id, compile, interpret, annotation };

lib/keywords/contentSchema.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
import { canonicalUri } from "../schema.js";
2+
import * as Browser from "@hyperjump/browser";
3+
import * as Instance from "../instance.js";
4+
import { getKeywordName } from "../keywords.js";
25

36

47
const id = "https://json-schema.org/keyword/contentSchema";
58

6-
const compile = (contentSchema) => canonicalUri(contentSchema);
9+
const compile = async (contentSchema, _ast, parentSchema) => {
10+
const contentMediaTypeKeyword = getKeywordName(contentSchema.document.dialectId, "https://json-schema.org/keyword/contentMediaType");
11+
const contentMediaType = await Browser.step(contentMediaTypeKeyword, parentSchema);
12+
return Browser.value(contentMediaType) && canonicalUri(contentSchema);
13+
};
14+
715
const interpret = () => true;
8-
const annotation = (contentSchema) => contentSchema;
16+
17+
const annotation = (contentSchema, instance) => {
18+
if (Instance.typeOf(instance) !== "string") {
19+
return;
20+
}
21+
22+
return contentSchema;
23+
};
924

1025
export default { id, compile, interpret, annotation };

0 commit comments

Comments
 (0)