@@ -5,72 +5,50 @@ The simplest way to use a reference, is with a schema class as a the target.
55``` python
66import typesystem
77
8- class Artist (typesystem .Schema ):
9- name = typesystem.String(max_length = 100 )
10-
11- class Album (typesystem .Schema ):
12- title = typesystem.String(max_length = 100 )
13- release_date = typesystem.Date()
14- artist = typesystem.Reference(to = Artist)
15- ```
16-
17- Using a schema class directly might not always be possible. If you need to
18- to support back-references or cyclical references, you can use a string-literal
19- reference and provide a ` SchemaDefinitions ` instance, which is a dictionary-like
20- object providing an index of reference lookups.
21-
22- ``` python
23- import typesystem
24-
25- definitions = typesystem.SchemaDefinitions()
26-
27- class Artist (typesystem .Schema ):
28- name = typesystem.String(max_length = 100 )
29-
30- class Person (typesystem .Schema ):
31- name = typesystem.String(max_length = 100 )
32- release_date = typesystem.Date()
33- artist = typesystem.Reference(to = ' Artist' , definitions = definitions)
34-
35- definitions[' Artist' ] = Artist
36- definitions[' Person' ] = Person
8+ artist_schema = typesystem.Schema(
9+ fields = {
10+ " name" : typesystem.String(max_length = 100 )
11+ }
12+ )
13+
14+ definitions = typesystem.Definitions()
15+ definitions[" Artist" ] = artist_schema
16+
17+ album_schema = typesystem.Schema(
18+ fields = {
19+ " title" : typesystem.String(max_length = 100 ),
20+ " release_date" : typesystem.Date(),
21+ " artist" : typesystem.Reference(to = " Artist" , definitions = definitions),
22+ }
23+ )
3724```
3825
39- A shorthand for including a schema class in the definitions index, and for
40- setting the ` definitions ` on any Reference fields, is to declare schema
41- classes with the ` definitions ` keyword argument, like so:
42-
43- ``` python
44- import typesystem
45-
46- definitions = typesystem.SchemaDefinitions()
47-
48- class Artist (typesystem .Schema , definitions = definitions ):
49- name = typesystem.String(max_length = 100 )
50-
51- class Person (typesystem .Schema , definitions = definitions ):
52- name = typesystem.String(max_length = 100 )
53- release_date = typesystem.Date()
54- artist = typesystem.Reference(to = ' Artist' )
55- ```
56-
57- Registering schema classes against a ` SchemaDefinitions ` instance is particularly
26+ Registering schema instances against a ` Definitions ` instance is particularly
5827useful if you're using JSON schema to document the input and output types of
5928a Web API, since you can easily dump all the type definitions:
6029
6130``` python
6231import json
6332import typesystem
6433
65- definitions = typesystem.SchemaDefinitions ()
34+ definitions = typesystem.Definitions ()
6635
67- class Artist (typesystem .Schema , definitions = definitions ):
68- name = typesystem.String(max_length = 100 )
36+ artist_schema = typesystem.Schema(
37+ fields = {
38+ " name" : typesystem.String(max_length = 100 )
39+ }
40+ )
6941
70- class Person (typesystem .Schema , definitions = definitions ):
71- name = typesystem.String(max_length = 100 )
72- release_date = typesystem.Date()
73- artist = typesystem.Reference(to = ' Artist' )
42+ album_schema = typesystem.Schema(
43+ fields = {
44+ " title" : typesystem.String(max_length = 100 ),
45+ " release_date" : typesystem.Date(),
46+ " artist" : typesystem.Reference(to = " Artist" , definitions = definitions),
47+ }
48+ )
49+
50+ definitions[" Artist" ] = artist_schema
51+ definitions[" Album" ] = album_schema
7452
7553document = typesystem.to_json_schema(definitions)
7654print (json.dumps(document, indent = 4 ))
@@ -89,10 +67,10 @@ print(json.dumps(document, indent=4))
8967# "name"
9068# ]
9169# },
92- # "Person ": {
70+ # "Album ": {
9371# "type": "object",
9472# "properties": {
95- # "name ": {
73+ # "title ": {
9674# "type": "string",
9775# "minLength": 1,
9876# "maxLength": 100
@@ -107,7 +85,7 @@ print(json.dumps(document, indent=4))
10785# }
10886# },
10987# "required": [
110- # "name ",
88+ # "title ",
11189# "release_date",
11290# "artist"
11391# ]
0 commit comments