Skip to content

Commit 1a71d68

Browse files
authored
Fix YAML date parsing and add test (#118)
1 parent cb8ca18 commit 1a71d68

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

src/check_jsonschema/loaders/instance/yaml.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
_yaml = ruamel.yaml.YAML(typ="safe")
66

7+
# ruamel.yaml parses timestamp values into datetime.datetime values which differs from
8+
# JSON which parses timestamps as strings. Turn off this feature.
9+
_yaml.constructor.yaml_constructors[
10+
"tag:yaml.org,2002:timestamp"
11+
] = _yaml.constructor.yaml_constructors["tag:yaml.org,2002:str"]
12+
713

814
def _normalize(data: t.Any) -> t.Any:
915
"""
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Comment
2+
object:
3+
string: "I'm a string."
4+
multiline_string: |
5+
I'm
6+
a multiline
7+
string
8+
9+
integer_1: 1
10+
integer_2: +1
11+
integer_3: -1
12+
13+
float_1: +1.0
14+
float_2: 3.1415
15+
float_3: -0.01
16+
float_4: 5e+22
17+
float_5: 1e06
18+
float_6: -2E-2
19+
float_7: 6.626e-34
20+
float_8: 224_617.445_991_228
21+
22+
infinite_1: .inf
23+
infinite_2: +.inf
24+
infinite_3: -.inf
25+
26+
not_a_number_1: .nan
27+
not_a_number_2: .NaN
28+
not_a_number_3: .NAN
29+
30+
hexadecimal_1: 0xDEADBEEF
31+
hexadecimal_2: 0xdeadbeef
32+
hexadecimal_3: 0xdead_beef
33+
34+
octal_1: 0o01234567
35+
octal_2: 0o755
36+
37+
binary: 0b11010110
38+
39+
null_1: null
40+
null_2: ~
41+
42+
boolean_1: true
43+
boolean_2: false
44+
boolean_3: True
45+
boolean_4: False
46+
boolean_5: TRUE
47+
boolean_6: FALSE
48+
49+
# ruamel.yaml by default converts dates to datetime objects, so check-jsonschema parses
50+
# as strings
51+
offset_datetime_1: 1979-05-27T07:32:00Z
52+
offset_datetime_2: 1979-05-27T00:32:00-07:00
53+
offset_datetime_3: 1979-05-27T00:32:00.999999-07:00
54+
offset_datetime_4: '1979-05-27T07:32:00Z'
55+
offset_datetime_5: '1979-05-27T00:32:00-07:00'
56+
offset_datetime_6: '1979-05-27T00:32:00.999999-07:00'
57+
58+
datetime_1: 1979-05-27T07:32:00Z
59+
datetime_2: 1979-05-27T00:32:00.999999z
60+
61+
local_date_1: 1979-05-27
62+
local_date_2: '1979-05-27'
63+
64+
time_1: 07:32:00Z
65+
time_2: 00:32:00.999999z
66+
67+
# YAML does not have a native duration type which translates to
68+
# datetime.timedelta under ruamel.yaml
69+
# so ISO durations can only be represented as strings
70+
duration_1: "P1D"
71+
72+
array_1: ["a", 2, true]
73+
array_2: [
74+
"b",
75+
3.1,
76+
false,
77+
]
78+
79+
nested_object_1:
80+
foo: "bar"
81+
82+
nested_object_2: { foo: "bar" }
83+
84+
array_of_objects_1:
85+
- foo: "bar"
86+
- foo: "bar"
87+
88+
nested_array_of_objects_1:
89+
- foo: "bar"
90+
- foo: "bar"
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema",
3+
"type": "object",
4+
"properties": {
5+
"object": {
6+
"type": "object",
7+
"properties": {
8+
"string": { "type": "string" },
9+
"multiline_string": { "type": "string" },
10+
"integer_1": { "type": "integer" },
11+
"integer_2": { "type": "integer" },
12+
"integer_3": { "type": "integer" },
13+
"float_1": { "type": "number" },
14+
"float_2": { "type": "number" },
15+
"float_3": { "type": "number" },
16+
"float_4": { "type": "number" },
17+
"float_5": { "type": "number" },
18+
"float_6": { "type": "number" },
19+
"float_7": { "type": "number" },
20+
"float_8": { "type": "number" },
21+
"infinite_1": { "type": "number" },
22+
"infinite_2": { "type": "number" },
23+
"infinite_3": { "type": "number" },
24+
"not_a_number_1": { "type": "number" },
25+
"not_a_number_2": { "type": "number" },
26+
"not_a_number_3": { "type": "number" },
27+
"hexadecimal_1": { "type": "number" },
28+
"hexadecimal_2": { "type": "number" },
29+
"hexadecimal_3": { "type": "number" },
30+
"octal_1": { "type": "number" },
31+
"octal_2": { "type": "number" },
32+
"binary": { "type": "number" },
33+
"null_1": { "type": "null" },
34+
"null_2": { "type": "null" },
35+
"boolean_1": { "type": "boolean" },
36+
"boolean_2": { "type": "boolean" },
37+
"boolean_3": { "type": "boolean" },
38+
"boolean_4": { "type": "boolean" },
39+
"boolean_5": { "type": "boolean" },
40+
"boolean_6": { "type": "boolean" },
41+
"offset_datetime_1": { "type": "string", "format": "date-time" },
42+
"offset_datetime_2": { "type": "string", "format": "date-time" },
43+
"offset_datetime_3": { "type": "string", "format": "date-time" },
44+
"offset_datetime_4": { "type": "string", "format": "date-time" },
45+
"offset_datetime_5": { "type": "string", "format": "date-time" },
46+
"offset_datetime_6": { "type": "string", "format": "date-time" },
47+
"datetime_1": { "type": "string", "format": "date-time" },
48+
"datetime_2": { "type": "string", "format": "date-time" },
49+
"local_date_1": { "type": "string", "format": "date" },
50+
"local_date_2": { "type": "string", "format": "date" },
51+
"time_1": { "type": "string", "format": "time" },
52+
"time_2": { "type": "string", "format": "time" },
53+
"duration_1": { "type": "string", "format": "duration" },
54+
"array_1": { "type": "array" },
55+
"array_2": { "type": "array" },
56+
"nested_object_1": {
57+
"type": "object",
58+
"properties": { "foo": { "type": "string" } }
59+
},
60+
"nested_object_2": {
61+
"type": "object",
62+
"properties": { "foo": { "type": "string" } }
63+
},
64+
"array_of_objects_1": {
65+
"type": "array",
66+
"items": {
67+
"type": "object",
68+
"properties": { "foo": { "type": "string" } }
69+
}
70+
},
71+
"nested_array_of_objects_1": {
72+
"type": "array",
73+
"items": {
74+
"type": "object",
75+
"properties": { "foo": { "type": "string" } }
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)