Skip to content

Commit

Permalink
Add improvements to the tutorial section
Browse files Browse the repository at this point in the history
  • Loading branch information
izxxr committed Mar 1, 2024
1 parent 2659d31 commit 17347ad
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
19 changes: 13 additions & 6 deletions docs/source/tutorial/basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ Quickstart
This page introduces to the basics of Oblate.

Oblate's main components is the :class:`oblate.Schema` class which is used to define schemas
that can be used for validating data. A schema is essentially a data class. Start by defining
your own schema::
that can be used for validating data. Start by defining your own schema::

from oblate import fields
import oblate
Expand All @@ -18,8 +17,8 @@ your own schema::
username = fields.String()
is_employee = fields.Boolean(default=False)

Each schema has fields which are essentially just the attributes of a schema. ``oblate.fields`` provides
various pre-defined fields that support standard validation rules.
Each schema has fields which are essentially just the attributes of a schema. ``oblate.fields``
provides various pre-defined commonly used fields that support standard validation rules.

Now we can use raw data to initialize our schema::

Expand All @@ -30,5 +29,13 @@ Now we can use raw data to initialize our schema::
print(emily.username, "is employee?", emily.is_employee) # Emily is employee? True

Each field of the schema is accessed exactly the same way as normal attributes of a class are
accessed. Note how the ``default`` argument was used for defaulting ``is_employee`` value. There
are many other similar options that allow easy customization of each field.
accessed.

Note how the ``default`` argument was used for assigning default ``is_employee`` value
in case it is not provided in the data. There are many other similar options that
allow easy customization of each field. Some of the commonly used ones are:

- :ref:`guide-fields-optional-fields-and-defaults`
- :ref:`guide-fields-noneable-nullable-fields`
- :ref:`Strict/Non-Strict Fields <guide-fields-strict-mode>`
- :ref:`guide-fields-frozen-fields`
2 changes: 1 addition & 1 deletion docs/source/tutorial/errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ Here's the result::

.. TODO: Reference error customization tutorial here
This "raw format" is fully customizable to suit any use case.
This "raw format" is fully :ref:`customizable <guide-errors-raw-error-formatting>` to suit any use case.
24 changes: 24 additions & 0 deletions docs/source/tutorial/validators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ validation features, also provides the ability of writing custom validators.

This is done using the :func:`validate.field` function::

import oblate
from oblate import validate, fields

class User(oblate.Schema):
id = fields.Integer()
username = fields.String()
Expand All @@ -34,3 +37,24 @@ causes the following error::
└── In field username:
└── This field is required.

Pre-built Validators
--------------------

Some validators that are commonly used in various cases are preshipped by Oblate so you
don't have to implement such validations yourself. These validators are provided by the
``oblate.validate`` module.

For example, the range validator that we implemented above can be replaced with the one
already provided by the library::

class User(oblate.Schema):
id = fields.Integer(validators=[validate.Range(1000, 9999)])
username = fields.String()
is_employee = fields.Boolean(default=False)

The ``validators`` parameter takes a sequence of :class:`validate.Validator` objects.

It is recommended to use the pre-defined validators whenever possible as they are
tailored to be robust and flexible to suit most use cases.

These validators are documented in :ref:`Validators API Reference <api-validators>`.

0 comments on commit 17347ad

Please sign in to comment.