Giter Club home page Giter Club logo

pydantic-core's Introduction

pydantic-core

CI Coverage

This package provides the core functionality for pydantic.

The package is currently a work in progress and subject to significant change.

There is, as yet, no integration with pydantic, so schemas can only be defined via dictionaries.

The plan is for pydantic to adopt pydantic-core in v2 and to generate the schema definition from type hints in pydantic, then create a SchemaValidator upon model creation.

pydantic-core will be a separate package, required by pydantic.

The public interface to pydantic shouldn't change too much as a result of this switch (though I intend to clean up quite a lot in the public API in v2 as well).

Example of usage:

from pydantic_core import SchemaValidator, ValidationError

v = SchemaValidator({
    'title': 'MyModel',
    'type': 'model',
    'fields': {
        'name': {
            'type': 'str',
        },
        'age': {
            'type': 'int-constrained',
            'ge': 18,
        },
        'is_developer': {
            'type': 'bool',
            'default': True,
        },
    },
})
print(v)
"""
SchemaValidator(title="MyModel", validator=ModelValidator ...
"""

r1 = v.validate_python({'name': 'Samuel', 'age': 35})
print(r1)
"""
(
  {'name': 'Samuel', 'age': 35, 'is_developer': True}, <- validated data
  {'age', 'name'} <- fields set
)
"""

# pydantic-core can also validate JSON directly
r2 = v.validate_json('{"name": "Samuel", "age": 35}')
assert r1 == r2

try:
    v.validate_python({'name': 'Samuel', 'age': 11})
except ValidationError as e:
    print(e)
    """
    1 validation error for MyModel
    age
      Value must be greater than or equal to 18
      [kind=int_greater_than_equal, context={ge: 18}, input_value=11, input_type=int]
    """

Pydantic-core is currently around 17x faster than pydantic standard. See benchmarks/run.py for details.

This relative performance will be less impressive for small models but could be significantly move impressive for deeply nested models.

The improvement will decrease slightly when we have to create a class instance after validation, but shouldn't change more.

The aim is to remain 10x faster than current pydantic for common use cases.

Why not JSONSchema?

Looking at the above schema passed to SchemaValidator it would seem reasonable to ask "why not use JSONSchema?".

And if we could use JSONSchema, why not use an existing rust library to do validation?

In fact, in the very early commits to pydantic-core, I did try to use JSONSchema, however I quickly realized it wouldn't work.

JSONSchema does not match the schema for pydantic that closely:

  • there are lots of extra checks which pydantic wants to do and aren't covered by JSONSchema
  • there are configurations which are possible in JSONSchema but are hard or impossible to imagine in pydantic
  • pydantic has the concept of parsing or coercion at it's core, JSONSchema doesn't - it assumes you either accept or reject the input, never change it
  • There are whole classes of problem pydantic has to deal with (like python class instance validation) which JSONSchema has no idea about since it's dedicated to JSON

Even if we could use JSONSchema, it wouldn't help much since rust JSONSchema validators expect to know the schema at compile time, pydantic-core has no knowledge of the schema until SchemaValidator is initialised.

Still, it wouldn't be that hard to implement a conversion layer (either in python or rust) to convert JSONSchema to "pydantic schema" and thereby achieve partial JSONSchema validation.

pydantic-core's People

Contributors

samuelcolvin avatar danielsanchezq avatar stranger6667 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.