Commit Graph

73 Commits

Author SHA1 Message Date
Eric Jolibois ededd3ebf8 refactor(schema): support properly Literal in generated JSON schema (#2348)
* test: improve example

* refactor: use enum for Literal in JSON schema

* test: update test with new schema generation

* chore: add change file
2021-02-25 19:42:17 +00:00
Samuel Colvin b7a8ef25c6 fix coverage and make typing-extensions a required dependency (#2368)
* fixing coverage by simplifying Annotated import logic, fix #2367

* avoid checking against annotated if it's None

* make typing-extensions required WIP

* more making typing-extensions required

* fix docs and get_origin for python 3.6

* fix mypy test

* fix docs

* update docs, cleanup and add change

* clean docs/examples/schema_annotated.py

* move AnnotatedTypeNames
2021-02-17 19:17:30 +00:00
Scott Black 13928e5b98 introduce allow_mutation Field constraint (#2196)
* introduce read_only Field constraint

* add changes markdown for read_only constraint

* add readOnly property to json schema generation

* Revert "add readOnly property to json schema generation"

This reverts commit dad3d3e23a7336aa978dcdedc157559ea102fa54.

* change read_only field constraint to allow_mutation

* Update change notes for allow_mutation

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* allow field constraints defaults to be not None

* remove unnecessary test after constraint refactor

* push used constraints check back to schema functions

* use tuple item name descriptions instead of indexes

* move get_constraints function to method on FieldInfo

* address code review comments for minor changes

* Apply suggestions from code review

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* fix merge conflict

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2021-02-13 17:22:32 +00:00
maximberg c3870b611e Added schema generation for Generic fields (#2262)
* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* Added schema generation for Generic fields with tests.
Fixed test_assert_raises_validation_error.

* tested on python 3.6, 3.7, 3.8, 3.9

* tested on python 3.6, 3.7, 3.8, 3.9

* restore formatting

* fix mistakes

* formatting

* formatting

* formatting

* fixed coverage

* changes file

* changes file

* remove redundant len

* Update pydantic/schema.py

Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>

Co-authored-by: Maxim Berg <berg@petrostyle.com>
Co-authored-by: Maz Jaleel <mazjaleel@gmail.com>
2021-02-13 17:04:49 +00:00
Justin Sexton 688107ec8e Fixed KeyError when creating schema from models with same names declared within different classes (#2170)
* Fixed issue causing KeyError to be raised when building schema from multiple `BaseModel` with the same names declared in separate classes (#1912)

* Update test

* Removed redundant f-string

* Fixed formatting

* Fixed lint
2021-02-11 17:39:59 +00:00
Eric Jolibois d0baf0f156 fix(fields): handle properly default value for type Callable (#2094)
* fix(fields): handle properly default value for type `Callable`

closes #1596

* chore: move test in test_dataclasses

* chore: add comments

* test: rewrite test

* chore: remove useless variable

* fix(schema): add support when callable field has default value

closes #2086
2021-02-11 11:39:09 +00:00
Eric Jolibois 13a5c7d676 fix(schema): fix schema generation with multiple Enums having the same name (#2226)
closes #1857
2021-01-02 09:33:36 +00:00
Ben Martineau 0b7a570afd Fixes a regression where Enum fields would not propagate keyword arguments to the schema (#2109)
fix #2108

* Fix schema extra not being included when field type is Enum

* Code format

* More code format

* Add changes file

Co-authored-by: Ben Martineau <b.martineau@iwoca.co.uk>
2020-11-29 23:57:42 +00:00
Matthew Davis 9cb7ca744b add NonPositive/NonNegative Int/Float (#1987)
fix #1975

* add NonPositive/NonNegative Int/Float

* delete data.json file
2020-11-29 16:07:51 +00:00
Gabriel d14731f16c schema ref_template (#1480)
* ignore Pipfile, .lock files

* add ref_template option

use a string.Template instead of a ref_prefix to allow for more varied`$ref`s to be created.
Template string is expected to have $model_name `identifier `

* formatting / linting

* add changes

* typo

* use string.format instead of string.Template

* remove ref_prefix default

if no `ref_prefix` provided, use the `template_default`

* use ref_template in field_singleton_schema

* fix test_schema_with_ref_template

* add parameters for `test_schema_with_refs`

test name change 
test for key error

* provide ref_template default argument

* fix linting

* fix mypy and coverage

* docs and correct model_schema usage

* fastAPI tests actualy caught an error

* linting

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-10-25 16:24:37 +00:00
PrettyWood 3aacec4e17 feat(schema): support custom title, description and default for enums (#1749)
* refactor(schema): put schema data from fieldinfo in dedicated function

* feat(schema): support custom title, description and default for enums

closes #1748

* refactor: replace $ref by allOf + $ref to be supported by doc generation tools

* fix: do not set title by default for enums

* refactor: make code more explicit

* fix: run linter
2020-10-18 20:23:06 +01:00
Patrick Wang dca98559d5 Add conset (#1627)
* Add conset

* Add changes doc

* Fix too-long lines

* bump

* Update conset change doc formatting

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>

* Robustify test_conset

* Move conlist and conset tests to test_types

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-07-03 20:51:23 +01:00
John Carter ff20477521 Apply __modify_schema__ on enum schema rather than fields that use it (#1581)
* Apply __modify_schema__ on enum schema rather than fields that use it

Resolves #1576

* tweak test

* correct linting :-(

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-06-29 13:22:27 +01:00
PrettyWood 7ac9faf031 (fix) use right type check when modifying schema (#1562)
fix #1552
2020-06-27 14:12:36 +01:00
wangpeibao 5e82689c79 Change schema frozenset (#1560)
* add frozenset to field_class_to_schema

* add change.md

* add my test

* format my test

* remove print()

* just test

* return to original code

Co-authored-by: wangpeibao <wangpeibao@troila.com>
Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-06-02 11:03:12 +01:00
Calvin Young 5195e55c10 Enums as separate models (#1432)
* Updates schema() to generate enums as separate models

* Fixes mypy annotations

* Adds changes file

* Fixes comment

* Removes unused import

* Fixes test case

* Fixes missing partial branch in test coverage

* Resolves PR comments

* 🐛 Include enums in flat model schema handling

as they now have independent schemas, they kinda behave like top-level models, and should be taken into account for top level definitions

*  Add test for coverage

* 🐛 Use Type[Enum] as type for consistency

Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
2020-05-23 19:11:34 +01:00
Samuel Colvin 5067508eca fix tests and coverage for #1209 2020-05-01 10:32:39 +01:00
Mostapha Sadeghipour Roudsari a4adf892dc Remove extra allOf keys in schema (#1438)
resolves #1209
2020-04-30 18:56:11 +01:00
Atheuz 9238e3d98f include format with secret types (#1424)
* include format with secret types

* fix: fix pr comments.

Co-authored-by: Lasse Gravesen <lasse.gravesen@falcon.io>
2020-04-30 13:09:45 +01:00
Jesse VanderWees 833d33dc0f ConstrainedFloat schema: differences between IEEE floats and json (#1422)
* ConstrainedFloat schema: account for differences between IEEE floats and json

* add changes

* Update changes/1417-vdwees.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update changes/1417-vdwees.md

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* fixup

* Update pydantic/types.py

Difference of styles :)

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* merge _schema_ieee_compatibility_transform into parent method

* capitalize

* use type_, not outer_type_

Co-authored-by: Jesse VanderWees <jesse.vanderwees@kisters-bv.nl>
Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-04-30 13:08:23 +01:00
laevilgenius 7bebf2bdc3 Allow schema_extra to be a classmethod (#1306)
* Remove FunctionType check

* Fix mypy

* Add classmethod test

* Add instance method test

* Fix linting

* model_class

* Remove __call__ implementation

* Fix linting for py3.8

* noqa tweak

* undo mistake sorry, :-/

Co-authored-by: Samuel Colvin <s@muelcolvin.com>
2020-04-15 22:06:25 +01:00
PrettyWood aaec3c9c15 Remove typing-extension dependency on python 3.8 (#1342)
* Remove typing-extension dependency on python 3.8

* retrigger CI

* retrigger CI bis
2020-03-26 15:44:11 +00:00
Samuel Colvin d495710303 Cleanup ci & coverage (#1329)
* fix badges and deploy check

* fix coverage and correct check_tag.py
2020-03-21 18:14:17 +00:00
Samuel Colvin 943a8a06e5 change alias priority logic (#1178)
* fix alias priority so alias_generators don't take priority

* improve test names

* remove debugs

* Apply suggestions from code review

* more tests and allow custom alias_priority on fields

* precedence tests and docs

* tweaks and add change

* suggestions
2020-01-24 10:31:16 +00:00
Sebastián Ramírez 496551c854 Implement support for declaring infinite generators (#1152)
*  Implement support for infinite generators with Iterable

*  Add tests for infinite generators

* 🎨 Fix format

* 📝 Add docs for infinite generators

* 📝 Add changes file

*  Store sub_field with original type parameter to allow custom validation

* 📝 Add example for validating first value in infinite generators

* 🔥 Remove unused import in example

*  Add test for infinite generator with first-value validation

* ♻️ Update fields with code review

* 📝 Update example from code review

* 📝 Update docs and format from code review
2020-01-13 11:50:16 +00:00
John Carter cd8b504568 Pass model_class to schema_extra staticmethod (#1125)
* Pass model_class to schema_extra staticmethod

Resolves #1122

* Add changelog

* Apply suggestions from code review

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Fix import after rebase

* Fix test bug

* Use TypeError instead of assert as per review

* Rename var so declaration fits one one line

* tiny tweaks

Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
2020-01-06 12:01:03 +00:00
Samuel Colvin 31cac4783b improve pydantic import time (#1132)
* improve pydantic import time, fix #1127, fix #1039

* more tweaks

* tweak utils.py

* defering inspect
2020-01-02 14:35:03 +00:00
Samuel Colvin 065ae2e4c4 fix modify schema (#1103)
* modify schema, fix #1102

* remove DSN from schema table

* fix tests

* add outer_type_ to ModelField, fix coverage

* simplify ModelField._type_display

* remove debugs

* revert change to _type_display

* test fix
2019-12-21 15:46:11 +00:00
Samuel Colvin e1ae058afb Consistent checks for sequence like objects (#1111) 2019-12-20 15:08:45 +00:00
Samuel Colvin bd72e80e94 Prevent type attributes being added to schema unintentionally (#1065)
* Prevent type attributes being added to schema unintentionally, fix #1064

* simpler boolean check

* change to __modify_schema__

* better docs

* Update docs/usage/schema.md

Co-Authored-By: dmontagu <35119617+dmontagu@users.noreply.github.com>
2019-12-11 12:01:41 +00:00
Selim Belhaouane bdc3973d90 Allow schema_extra to be a callable (#1054)
* fixes for Windows development

* Allow Config.schema_extra to be a callable
2019-12-09 11:01:17 +00:00
Aviram Hassan 1b9f08bead Dataclass is now compatible with schema methods (#1045) 2019-12-02 12:44:21 +00:00
Samuel Colvin 1d3f7824ec Fix broken Any and TypeVar behaviour (#962)
* tests for broken Dict behaviour

* fix Any, support TypeVar

* change type of field.type_

* add docs and example for TypeVar
2019-11-07 12:31:26 +00:00
Samuel Colvin afa0bd4104 set FieldInfo on subfields (#969)
* set FieldInfo on subfields

* fix coverage
2019-11-07 10:34:49 +00:00
Samuel Colvin f5cde39e75 ConstrainedList improvements, fix #913 (#917) 2019-10-21 12:15:54 +01:00
Samuel Colvin 7db098fa49 get item type in get_annotation_from_field_info (#909)
* get item type in get_annotation_from_field_info, fix #779

* check constraints are enforced

* add change

* tests for phony constraints

* rewrite get_annotation_from_field_info

* fix tests

* add constaint enforcement to docs

* fix get_annotation_from_field_info coverage

* fix linting

* update docs as per @tiangolo suggestions
2019-10-18 17:41:10 +01:00
Samuel Colvin c3098a30cf Consistent __repr__ and __str__ methods for all types (#884)
* Consistent __repr__ and __str__ methods for all types

* add change description

* devtools integration and feedback on repr methods

* fix Color repr

* tests for truncate

* add devtools section to docs

* tests for devtools

* ValidationError inheriting from Representation

* fix imports

* tweaks

* tweak docs

* exec_examples.py integration with __repr__ changes
2019-10-14 17:37:04 +01:00
Samuel Colvin 8f29837703 small bits of cleanup (#851)
* small tweaks to main.py

* add changes

* correct ModelMetaclass name

* fix password and user on AnyUrl
2019-10-01 17:08:30 +01:00
Samuel Colvin 12f4e0d082 rename Schema to Field (#824)
* rename Schema to Field

* add Schema function with deprecation warning

* mypy tests and other tweaks

* tweaks and cleanup

* Update pydantic/fields.py

Co-Authored-By: Sebastián Ramírez <tiangolo@gmail.com>
2019-09-30 17:49:59 +01:00
Samuel Colvin 16263bafea None behaviour (#803)
* tweaks to None behaviour

* prevent sub_fields for Optional fields by default

* rewrite None validation

* rename whole > each_item on validators

* cleanup processing of the Json type

* fix schema coverage and cleanup

* tweak validate_model

* change and docs

* fix validators on optional fields

* coverage

* remove is_none_validator

* minor performance improvements to ErrorWrapper

* fix coverage

* fix PaymentCardNumber

* undo schema changes, fix

* tweak validators
2019-09-18 11:38:21 +01:00
Oliver Bristow 830278b17b Set additionalProperties to False on Extra.forbid models (#796) 2019-09-09 10:18:12 +01:00
Samuel Colvin 79017111aa new URL parsing (#755)
* new URL parsing, fix #603, fix #541

* AnyUrl parts and more tests

* more coverage and db DSNs

* remove DSN methods

* tests for urlstr

* remove debug

* make AnyStr a subtype of str

* fix with cython

* rearranging networking code

* allowing international domains, cleanup

* support international domains

* better URL builder

* allow underscores in subdomains and domains

* tests for json and schema, max length

* urlstr > stricturl

* updating docs

* tweak docs examples

* tweak docs
2019-09-02 11:37:33 +01:00
Scott P 5015a7e48b Improve auto-generated title in JSON schemas (#772)
* Improve auto-generated title in JSON schemas

Underscore is converted to space.

* Added changes .rst file.
2019-08-21 16:49:05 +01:00
Samuel Colvin 5f634067da moving typing related objects into typing.py (#761)
* moving typing related objects into typing.py, fix #713

* cleanup
2019-08-21 12:49:19 +01:00
Sebastián Ramírez d4d9e9e7d7 Implement Config attribute schema_extra, to allow extending a generated schema 2019-08-05 09:45:37 -05:00
euri10 1e83ac5137 Color schema (#666)
* First attempt at adding Color in schema() generation

* Lint

* Add history

* Make docs pass

* Added format color

* Format color

* Making history...:)
2019-07-24 10:52:05 +01:00
dmontagu 18d4b2bb2a Make schema work for Literal and NewType (#649)
* Make schema work for Literal and NewType, fix #646

* Fix test when typing_extensions not installed

* Make literal values fetching 3.6 compatible

* Rework to get around issues with Literal in python 3.6

* Make cython-compatible

* Use union for multiple allowed literal values

* Minor simplification of values-gathering

* Updated history
2019-07-15 20:06:35 +01:00
Koudai Aono e4b285a0cd support custom root types (#628)
* support custom root types

* update HISTORY.rst for costom root types

* fix invalid arguments case

* remove the keyword argument of "__root__" in __init__
add a keyword argument of "__root__" in parse_obj
fix documents
create a method for cheking valid field name

* fix parse_obj

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Apply suggestions from code review

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* add type cheking for custom root type

* Update docs/index.rst

Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>

* Update index.rst
2019-07-06 09:52:42 +01:00
dmontagu 8aac71af0f Fix unparameterized generic type schema generation (#625)
* Fix #623

* Update history

* Add List test

* Augment test
2019-07-02 10:44:24 +01:00
Sebastián Ramírez 3cdbbaee95 Fix circular schema generation, remove None checking hack (#621)
* Fix circular schema generation, remove None checking hack

* Include test written by @wongpat to make it run in CI

* Update History

* Update nested_models in-place

Co-Authored-By: Patrick Wong <wongpat@users.noreply.github.com>

* Update nested_models in-place

Co-Authored-By: Patrick Wong <wongpat@users.noreply.github.com>

* Update nested_models in place everywhere for consistency
2019-06-28 10:49:26 +01:00