Files
OpenBB/website/content/platform/developer_guide/validators.mdx
Danglewood 3a8a71dd15 [Docs] Refactor Docs Pages For Diataxis (#6315)
* This change creates a PR

* start moving stuff around

* data_models/_category_.json

* reference/_category_.json

* moving some stuff around

* Introduction

* Documentation updates

* few broken links, clean up sidebar titles for how-to

* codespell

* quickstart - new provider extension

* add a comment

* map provider interface

* quickstart for new router extension

* [Docs] diataxis refactor refactor (#6425)

* Start refactor

* Edits

* [DOCS] Reorganize and edit. Fix sidebar navigation (#6445)

* Reorganize and edit. Fix sidebar navigation

* Fix broken linkages

* Edit

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>

* Fix links

---------

Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>

* Fix broken links

* Reword CLA part in the Licensing FAQ

* change titles and order suggestion - cc @IgorWounds @piiq

---------

Co-authored-by: Igor Radovanovic <74266147+IgorWounds@users.noreply.github.com>
Co-authored-by: James Maslek <jmaslek11@gmail.com>
Co-authored-by: Theodore Aptekarev <aptekarev@gmail.com>
Co-authored-by: hjoaquim <henriquecjoaquim@gmail.com>
2024-05-22 13:50:43 +00:00

120 lines
3.5 KiB
Plaintext
Vendored

---
title: Validators
sidebar_position: 9
description: This guide provides detailed instructions on how and where validators should be used.
keywords:
- OpenBB Platform
- Data point addition
- Provider creation
- Query parameters
- Data output models
- Fetcher class
- validator
- field
- param
- Fast API
---
import HeadTitle from '@site/src/components/General/HeadTitle.tsx';
<HeadTitle title="Validators - Developer Guidelines - Development | OpenBB Platform Docs" />
Both QueryParams and Data models can benefit from the tactical use of Pydantic validators.
This page will outline some of the key scenarios where they are deployed.
Overall, they assist with enforcing Fast API compliance for both inputs and outputs,
and they work in the final stage of transformation immediately before output.
## Why Use Validators?
Some situations where they are used include:
- Transform, conform, or otherwise alter the entered query parameter or returned data value.
- A query parameter or data field is a date.
- A query parameter requires a dynamic default state.
- Normalizing percent values.
- Cleaning NaN values.
## Examples
The items to import from the Pydantic library are:
```python
from pydantic import field_validator, model_validator
```
<details>
<summary mdxType="summary">Parsing Dates</summary>
Providers will format dates in a number of ways. OpenBB uses YYYY-MM-DD as the standard convention, for both inputs and outputs.
Outputs are a `datetime` object or JSON serlialized string. Validators are used to parse the date from the specific format.
This example is used within a provider's `Data` model.
```python
@field_validator("last_trade_timestamp", mode="before", check_fields=False)
@classmethod
def parse_timestamp(cls, v):
"""Parse a Unix timestamp."""
return datetime.fromtimestamp(v)
```
</details>
<details>
<summary mdxType="summary">Normalize Percent Values</summary>
At the provider level, we want to standardize the way values representing a percent are returned.
It is our intention to ensure those values are ready-to-consume by formulas without conversion.
This example would be used within a provider's `Data` model.
```python
@field_validator("change_percent", mode="before", check_fields=False)
@classmethod
def normalize_percent(cls, v):
"""Normalize the percent."""
return v / 100 if v else None
```
</details>
<details>
<summary mdxType="summary">Dynamic Default Date</summary>
It might be desirable to have a default date parameter that is not static. To allow this, we must set the default parameter value as `None`, and use the `model_validator`. This example is for the `QueryParams`.
```python
@model_validator(mode="before")
@classmethod
def validate_dates(cls, values) -> dict:
"""Validate the query parameters."""
if values.get("start_date") is None:
values["start_date"] = (datetime.now() - timedelta(days=90)).date()
if values.get("end_date") is None:
values["end_date"] = datetime.now().date()
return values
```
</details>
<details>
<summary mdxType="summary">Replace 0s With None</summary>
Sometimes values are returned as a `0` when they should really be a `null`.
This example looks at the entire `Data` model, but could easily be adapted to use on individual fields.
```python
@model_validator(mode="before")
@classmethod
def replace_zero(cls, values):
"""Check for zero values and replace with None."""
return (
{k: None if v == 0 else v for k, v in values.items()}
if isinstance(values, dict)
else values
)
```
</details>