File: validate.py

package info (click to toggle)
python-apischema 0.18.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,608 kB
  • sloc: python: 15,266; sh: 7; makefile: 7
file content (25 lines) | stat: -rw-r--r-- 596 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from dataclasses import dataclass, field

import pytest

from apischema import ValidationError, schema, validator
from apischema.validation import validate


@dataclass
class Foo:
    bar: int = field(metadata=schema(min=0, max=10))
    baz: int

    @validator
    def not_equal(self):
        if self.bar == self.baz:
            yield "bar cannot be equal to baz"


# validate don't validate constraints, but only validators
validate(Foo(-1, 0))

with pytest.raises(ValidationError) as err:
    validate(Foo(2, 2))
assert err.value.errors == [{"loc": [], "err": "bar cannot be equal to baz"}]