Skip to content

Commit

Permalink
Docs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinger86 committed Jan 10, 2022
1 parent 810b196 commit 9eafb05
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Main benefits:
- Type annotations can replace repetitive view validation/sanitization code.
- Simple serializers can have their fields auto-generated from annotations
- Validated serializer data can be accessed from attributes, with their types known to the IDE
- [Pydantic](https://pydantic-docs.helpmanual.io/) models and [Marshmallow](https://marshmallow.readthedocs.io) schemas are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.
- [Pydantic](https://pydantic-docs.helpmanual.io/) models are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.

**Documentation**: <a href="https://rsinger86.github.io/drf-typed/" target="_blank">https://rsinger86.github.io/drf-typed</a>

Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Main benefits:
- Type annotations can replace repetitive view validation/sanitization code.
- Simple serializers can have their fields auto-generated from annotations
- Validated serializer data can be accessed from attributes, with their types known to the IDE
- [Pydantic](https://pydantic-docs.helpmanual.io/) models and [Marshmallow](https://marshmallow.readthedocs.io) schemas are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.
- [Pydantic](https://pydantic-docs.helpmanual.io/) models are compatible types for view parameters. Annotate your POST/PUT functions with them to automatically validate incoming request bodies.

## Views Example

Expand Down
14 changes: 7 additions & 7 deletions docs/views/advanced_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,20 @@ def search_documens(request: Request, q: str = None):

## Interdependent Query Parameter Validation

Often, it's useful to validate a combination of query parameters - for instance, a `start_date` shouldn't come after an `end_date`. You can use complex schema object (Pydantic or Marshmallow) for this scenario. In the example below, `Query(source="*")` is instructing an instance of `SearchParamsSchema` to be populated/validated using all of the query parameters together: `request.query_params.dict()`.
Often, it's useful to validate a combination of query parameters - for instance, a `start_date` shouldn't come after an `end_date`. You can use complex schema object (Pydantic) for this scenario. In the example below, `Query(source="*")` is instructing an instance of `SearchParamsSchema` to be populated/validated using all of the query parameters together: `request.query_params.dict()`.

```python
from marshmallow import Schema, fields, validates_schema, ValidationError
from pydantic import BaseModel
from rest_typed import typed_api_view

class SearchParamsSchema(Schema):
class SearchParamsSchema(BaseModel):
start_date = fields.Date()
end_date = fields.Date()

@validates_schema
def validate_numbers(self, data, **kwargs):
if data["start_date"] >= data["end_date"]:
raise ValidationError("end_date must come after start_date")
@root_validator
def validate_dates(cls, values):
if values["start_date"] >= values["end_date"]:
raise ValueError("end_date must come after start_date")

@typed_api_view(["GET"])
def search_documens(search_params: SearchParamsSchema = Query(source="*")):
Expand Down
2 changes: 1 addition & 1 deletion docs/views/simple_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ For many cases, you can rely on implicit behavior for how different parts of the
The value of a view parameter will come from...

- the URL path if the path variable and the view argument have the same name, _or_:
- the request body if the view argument is annotated using a class from a supported library for complex object validation (Pydantic, MarshMallow), _or_:
- the request body if the view argument is annotated using a class from a supported library for complex object validation (Pydantic), _or_:
- a query parameter with the same name

Unless a default value is given, the parameter is **required** and a [`ValidationError`](https://www.django-rest-framework.org/api-guide/exceptions/#validationerror) will be raised if not set.
Expand Down
2 changes: 1 addition & 1 deletion test_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,4 @@
},
}

DRF_TYPED_VIEWS = {"schema_packages": ["pydantic", "marshmallow", "typesystem"]}
DRF_TYPED_VIEWS = {"schema_packages": ["pydantic"]}

0 comments on commit 9eafb05

Please sign in to comment.