Skip to content

Commit c227d64

Browse files
committed
Document slightly more clearly how this library interacts with non-JSON.
Closes: #788
1 parent d74efba commit c227d64

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

docs/faq.rst

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Frequently Asked Questions
33
==========================
44

5-
65
My schema specifies format validation. Why do invalid instances seem valid?
76
---------------------------------------------------------------------------
87

@@ -74,6 +73,32 @@ implemented here, across any other implementation they encounter.
7473
the object which implements format validation
7574

7675

76+
Can `jsonschema` be used to validate YAML, TOML, etc.?
77+
------------------------------------------------------
78+
79+
Like most JSON Schema implementations, `jsonschema` doesn't actually deal directly with JSON at all (other than in relation to the :kw:`$ref` keyword, elaborated on below).
80+
81+
In other words as far as this library is concerned, schemas and instances are simply runtime Python objects.
82+
The JSON object ``{}`` is simply the Python `dict` ``{}``, and a JSON Schema like ``{"type": "object", {"properties": {}}}`` is really an assertion about particular Python objects and their keys.
83+
84+
.. note::
85+
86+
The :kw:`$ref` keyword is a single notable exception.
87+
88+
Specifically, in the case where `jsonschema` is asked to `resolve a remote reference <RefResolver>`, it has no choice but to assume that the remote reference is serialized as JSON, and to deserialize it using the `json` module.
89+
90+
One cannot today therefore reference some remote piece of YAML and have it deserialized into Python objects by this library without doing some additional work.
91+
92+
In practice what this means for JSON-like formats like YAML and TOML is that indeed one can generally schematize and then validate them exactly as if they were JSON by simply first deserializing them using libraries like ``PyYAML`` or the like, and passing the resulting Python objects into functions within this library.
93+
94+
Beware however that there are cases where the behavior of the JSON Schema specification itself is only well-defined within the data model of JSON itself, and therefore only for Python objects that could have "in theory" come from JSON.
95+
As an example, JSON supports only string-valued keys, whereas YAML supports additional types.
96+
The JSON Schema specification does not deal with how to apply the :kw:`patternProperties` keyword to non-string properties.
97+
The behavior of this library is therefore similarly not defined when presented with Python objects of this form, which could never have come from JSON.
98+
In such cases one is recommended to first pre-process the data such that the resulting behavior is well-defined.
99+
In the previous example, if the desired behavior is to transparently coerce numeric properties to strings, as Javascript might, then do the conversion explicitly before passing data to this library.
100+
101+
77102
How do I configure a base URI for ``$ref`` resolution using local files?
78103
------------------------------------------------------------------------
79104

@@ -239,8 +264,8 @@ defaults.
239264
assert obj2 == {} # whoops
240265

241266

242-
How do jsonschema version numbers work?
243-
---------------------------------------
267+
How do `jsonschema` version numbers work?
268+
-----------------------------------------
244269

245270
``jsonschema`` tries to follow the `Semantic Versioning
246271
<https://semver.org/>`_ specification.

docs/spelling-wordlist.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ th
99
callables
1010
deque
1111
dereferences
12+
deserialize
13+
deserialized
14+
deserializing
1215
filesystem
1316
hostname
1417
implementers
@@ -17,6 +20,7 @@ indices
1720
ipv
1821
iterable
1922
iteratively
23+
Javascript
2024
jsonschema
2125
majorly
2226
metaschema
@@ -27,6 +31,7 @@ programmatically
2731
recurses
2832
regex
2933
repr
34+
runtime
3035
sensical
3136
subschema
3237
subschemas

0 commit comments

Comments
 (0)