JSON assertions¶
JSONPath navigation and JSON Schema validation (requires the json extra).
JSON path navigation and schema validation mixin.
at_json_path ¶
Navigate to a JSON path and return a new builder with the matched value.
Uses JSONPath syntax (e.g. $.users[0].name). If multiple matches are found,
the value is a list of all matches. If exactly one match is found, the value is
unwrapped from the list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
JSONPath expression. |
required |
Examples:
Usage:
data = {"users": [{"name": "Alice"}, {"name": "Bob"}]}
assert_that(data).at_json_path("$.users[0].name").is_equal_to("Alice")
assert_that(data).at_json_path("$.users[*].name").is_equal_to(["Alice", "Bob"])
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
a new instance with the extracted value |
Raises:
| Type | Description |
|---|---|
ValueError
|
if no match is found at the given path |
Source code in assertpy/json_mixin.py
has_json_path ¶
Assert that the given JSON path exists in val.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
JSONPath expression. |
required |
Examples:
Usage:
data = {"meta": {"total": 5}}
assert_that(data).has_json_path("$.meta.total")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the path does not exist |
Source code in assertpy/json_mixin.py
does_not_have_json_path ¶
Assert that the given JSON path does not exist in val.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
JSONPath expression. |
required |
Examples:
Usage:
data = {"status": "ok"}
assert_that(data).does_not_have_json_path("$.error")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if the path exists |
Source code in assertpy/json_mixin.py
matches_json_schema ¶
Assert that val conforms to the given JSON Schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
a JSON Schema as a dict. |
required |
Examples:
Usage:
schema = {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}
assert_that({"name": "Alice"}).matches_json_schema(schema)
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not conform to the schema |
Source code in assertpy/json_mixin.py
matches_json_schema_from_file ¶
Assert that val conforms to a JSON Schema loaded from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
path to a JSON file containing the schema. |
required |
Examples:
Usage:
assert_that(data).matches_json_schema_from_file("schemas/order.json")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not conform to the schema |
Source code in assertpy/json_mixin.py
conforms_to_openapi ¶
conforms_to_openapi(
spec: dict[str, Any],
path: str,
method: str,
*,
status: str | int | None = None,
content_type: str = "application/json",
strict_nullable: bool = False,
) -> Self
Assert that val conforms to an OpenAPI operation's response-body schema.
val is validated against the schema declared for the application/json response of the
method/path operation in spec. This checks only the response body of that one
operation - not request bodies, parameters, headers, or the spec as a whole.
OpenAPI 3.0 (its nullable keyword is honoured), 3.1, and Swagger 2.0 (schema declared directly
on the response, its x-nullable extension honoured) are all supported. $ref,
oneOf/allOf/anyOf and enum all validate with full JSON-Schema semantics, and every
violation is reported with its JSON path.
Under OpenAPI 3.0, nullable: true adds null to the type beside it and leaves an enum
as written, so null passes an enum only where the enum lists it. A property marked
writeOnly: true is not asked for by required, since val is a response.
A multipleOf divides the numbers as they are written, so 19.99 is a multiple of 0.01,
which it is not as two floats divided. A float is read as the decimal it prints as: digits a float
does not keep are gone before val is handed in, and a decimal.Decimal keeps them.
A format is checked where there is a check for it: date, time, date-time, email,
ipv4, ipv6, uuid, regex, uri, hostname, duration, and OpenAPI's int32
and int64. A time is RFC 3339's, with its offset: 10:00:00Z, and not 10:00:00. A
hostname under OpenAPI 3.1 has its xn-- labels held to IDNA by the idna package. An
email is the mailbox of RFC 5321, and a duration has no fraction: PT0.5S is none. The
others jsonschema checks only beside a package of their own, uri-reference,
iri, json-pointer and uri-template among them, are checked where that package is
installed (jsonschema[format-nongpl] brings them all), and pass unchecked where it is not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spec
|
dict[str, Any]
|
a parsed OpenAPI document (dict); loading YAML/JSON is the caller's job. |
required |
path
|
str
|
the operation's path template, e.g. |
required |
method
|
str
|
the HTTP method, e.g. |
required |
status
|
str | int | None
|
response status to validate against; defaults to |
None
|
content_type
|
str
|
response content type; defaults to |
'application/json'
|
strict_nullable
|
bool
|
hold OpenAPI 3.0's |
False
|
Examples:
Usage:
spec = {...} # your parsed OpenAPI document
assert_that(response.json()).conforms_to_openapi(spec, "/orders/{id}", "get")
Returns:
| Name | Type | Description |
|---|---|---|
AssertionBuilder |
Self
|
returns this instance to chain to the next assertion |
Raises:
| Type | Description |
|---|---|
AssertionError
|
if val does not conform to the response schema |
ValueError
|
if the operation, status, or content type is not found in the spec, a schema of
OpenAPI 3.0 or Swagger 2.0 holds a keyword of later JSON Schema, which would be passed over,
or a |
Source code in assertpy/json_mixin.py
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 | |