Debian Codegraph @

docs/index.md

raw
1 # Overview
2
3 ## apischema
4
5 JSON (de)serialization, GraphQL and JSON schema generation using Python typing.
6
7 *apischema* makes your life easier when dealing with API data.
8
9 <!--
10 ## Documentation
11
12 [https://wyfo.github.io/apischema/](https://wyfo.github.io/apischema/)
13 -->
14
15 ## Install
16 ```shell
17 pip install apischema
18 ```
19 It requires only Python 3.8+. *PyPy3* is also fully supported.
20
21 ## Why another library?
22
23 <!--
24 (If you wonder how this differs from the *pydantic* library, see the [dedicated section of the documentation](difference_with_pydantic.md) — there are many differences.)
25 -->
26
27 This library fulfills the following goals:
28
29 - stay as close as possible to the standard library (dataclasses, typing, etc.) — as a consequence we do not need plugins for editors/linters/etc.;
30 - avoid object-oriented limitations — do not require a base class — thus handle easily every type (`Foo`, `list[Bar]`, `NewType(Id, int)`, etc.) the same way.
31 - be adaptable, provide tools to support any types (ORM, etc.);
32 - avoid dynamic things like using raw strings for attributes name - play nicely with your IDE.
33
34 No known alternative achieves all of this, and apischema is also [(a lot) faster](optimizations_and_benchmark.md#benchmark) than all of them.
35
36 On top of that, because APIs are not only JSON, *apischema* is also a complete GraphQL library
37
38 !!! note
39 Actually, *apischema* is even adaptable enough to enable support of competitor libraries in a few dozens of line of code ([pydantic support example](examples/pydantic_support.md) using [conversions feature](conversions.md))
40
41 ## Example
42
43 ```python
44 {!quickstart.py!}
45 ```
46 *apischema* works out of the box with your data model.
47
48 !!! note
49 This example and further ones are using *pytest* API because they are in fact run as tests in the library CI
50
51 ### Run the documentation examples
52
53 All documentation examples are written using the last Python minor version — currently 3.10 — in order to provide up-to-date documentation. Because Python 3.10 specificities (like [PEP 585](https://www.python.org/dev/peps/pep-0604/)) are used, this version is "mandatory" to execute the examples as-is.
54
55 In addition to *pytest*, some examples use third-party libraries like *SQLAlchemy* or *attrs*. All of this dependencies can be downloaded using the `examples` extra with
56 ```shell
57 pip install apischema[examples]
58 ```
59
60 Once dependencies are installed, you can simply copy-paste examples and execute them, using the proper Python version.
61
62 ## FAQ
63
64 #### What is the difference between *apischema* and *pydantic*?
65
66 See the [dedicated section](difference_with_pydantic.md) — there are many differences.
67
68 #### I already have my data model with my *SQLAlchemy*/ORM tables, will I have to duplicate my code, making one dataclass per table?
69 No, `apischema` works with user-defined types as well as types from foreign libraries. Using the [conversion](conversions.md) feature, you can add default serialization for all your tables, or register a different serializer that you can select according to your API endpoint, or both.
70
71 #### I need more accurate validation than "ensure this is an integer and not a string ", can I do that?
72 See the [validation](validation.md) section. You can use standard JSON schema validation (`maxItems`, `pattern`, etc.) that will be embedded in your schema or add custom Python validators for each class/fields/`NewType` you want.