5
5
from dirty_equals import FunctionCheck , HasAttributes , IsInstance
6
6
7
7
from pydantic_core import CoreConfig , SchemaValidator , ValidationError
8
+ from pydantic_core import core_schema as cs
8
9
9
10
from .conftest import Err , plain_repr
10
11
11
12
12
13
def test_on_field ():
13
- v = SchemaValidator ({ 'type' : 'str' , ' min_length' : 2 , ' max_length' : 5 } )
14
+ v = SchemaValidator (cs . str_schema ( min_length = 2 , max_length = 5 ) )
14
15
r = plain_repr (v )
15
16
assert 'min_length:Some(2)' in r
16
17
assert 'max_length:Some(5)' in r
@@ -19,14 +20,14 @@ def test_on_field():
19
20
20
21
21
22
def test_on_config ():
22
- v = SchemaValidator ({ 'type' : 'str' }, { ' str_max_length' : 5 } )
23
+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( str_max_length = 5 ) )
23
24
assert 'max_length:Some(5)' in plain_repr (v )
24
25
assert v .isinstance_python ('test' ) is True
25
26
assert v .isinstance_python ('test long' ) is False
26
27
27
28
28
29
def test_field_priority_arg ():
29
- v = SchemaValidator ({ 'type' : 'str' , ' max_length' : 5 }, { ' str_max_length' : 10 } )
30
+ v = SchemaValidator (cs . str_schema ( max_length = 5 ), config = CoreConfig ( str_max_length = 10 ) )
30
31
assert 'max_length:Some(5)' in plain_repr (v )
31
32
assert v .isinstance_python ('test' ) is True
32
33
assert v .isinstance_python ('test long' ) is False
@@ -39,12 +40,11 @@ class MyModel:
39
40
40
41
def test_on_model_class ():
41
42
v = SchemaValidator (
42
- {
43
- 'type' : 'model' ,
44
- 'cls' : MyModel ,
45
- 'config' : {'str_max_length' : 5 },
46
- 'schema' : {'type' : 'model-fields' , 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}}},
47
- }
43
+ cs .model_schema (
44
+ cls = MyModel ,
45
+ config = CoreConfig (str_max_length = 5 ),
46
+ schema = cs .model_fields_schema (fields = {'f' : cs .model_field (schema = cs .str_schema ())}),
47
+ )
48
48
)
49
49
assert 'max_length:Some(5)' in plain_repr (v )
50
50
assert v .isinstance_python ({'f' : 'test' }) is True
@@ -53,15 +53,11 @@ def test_on_model_class():
53
53
54
54
def test_field_priority_model ():
55
55
v = SchemaValidator (
56
- {
57
- 'type' : 'model' ,
58
- 'cls' : MyModel ,
59
- 'config' : {'str_max_length' : 10 },
60
- 'schema' : {
61
- 'type' : 'model-fields' ,
62
- 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' , 'max_length' : 5 }}},
63
- },
64
- }
56
+ cs .model_schema (
57
+ cls = MyModel ,
58
+ config = CoreConfig (str_max_length = 10 ),
59
+ schema = cs .model_fields_schema (fields = {'f' : cs .model_field (schema = cs .str_schema (max_length = 5 ))}),
60
+ )
65
61
)
66
62
assert 'max_length:Some(5)' in plain_repr (v )
67
63
assert v .isinstance_python ({'f' : 'test' }) is True
@@ -71,29 +67,34 @@ def test_field_priority_model():
71
67
@pytest .mark .parametrize (
72
68
'config,float_field_schema,input_value,expected' ,
73
69
[
74
- ({}, {'type' : 'float' }, {'x' : 'nan' }, IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan ))),
75
70
(
76
- { 'allow_inf_nan' : True } ,
77
- { 'type' : 'float' } ,
71
+ CoreConfig () ,
72
+ cs . float_schema () ,
78
73
{'x' : 'nan' },
79
74
IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan )),
80
75
),
81
76
(
82
- {'allow_inf_nan' : False },
83
- {'type' : 'float' },
77
+ CoreConfig (allow_inf_nan = True ),
78
+ cs .float_schema (),
79
+ {'x' : 'nan' },
80
+ IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan )),
81
+ ),
82
+ (
83
+ CoreConfig (allow_inf_nan = False ),
84
+ cs .float_schema (),
84
85
{'x' : 'nan' },
85
86
Err ('Input should be a finite number [type=finite_number,' ),
86
87
),
87
88
# field `allow_inf_nan` (if set) should have priority over global config
88
89
(
89
- { ' allow_inf_nan' : True } ,
90
- { 'type' : 'float' , ' allow_inf_nan' : False } ,
90
+ CoreConfig ( allow_inf_nan = True ) ,
91
+ cs . float_schema ( allow_inf_nan = False ) ,
91
92
{'x' : 'nan' },
92
93
Err ('Input should be a finite number [type=finite_number,' ),
93
94
),
94
95
(
95
- { ' allow_inf_nan' : False } ,
96
- { 'type' : 'float' , ' allow_inf_nan' : True } ,
96
+ CoreConfig ( allow_inf_nan = False ) ,
97
+ cs . float_schema ( allow_inf_nan = True ) ,
97
98
{'x' : 'nan' },
98
99
IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan )),
99
100
),
@@ -102,12 +103,11 @@ def test_field_priority_model():
102
103
)
103
104
def test_allow_inf_nan (config : CoreConfig , float_field_schema , input_value , expected ):
104
105
v = SchemaValidator (
105
- {
106
- 'type' : 'model' ,
107
- 'cls' : MyModel ,
108
- 'schema' : {'type' : 'model-fields' , 'fields' : {'x' : {'type' : 'model-field' , 'schema' : float_field_schema }}},
109
- 'config' : config ,
110
- }
106
+ cs .model_schema (
107
+ cls = MyModel ,
108
+ schema = cs .model_fields_schema (fields = {'x' : cs .model_field (schema = float_field_schema )}),
109
+ config = config ,
110
+ )
111
111
)
112
112
if isinstance (expected , Err ):
113
113
with pytest .raises (ValidationError , match = re .escape (expected .message )):
@@ -120,34 +120,32 @@ def test_allow_inf_nan(config: CoreConfig, float_field_schema, input_value, expe
120
120
@pytest .mark .parametrize (
121
121
'config,input_str' ,
122
122
(
123
- ({} , 'type=string_type, input_value=123, input_type=int' ),
124
- ({ ' hide_input_in_errors' : False } , 'type=string_type, input_value=123, input_type=int' ),
125
- ({ ' hide_input_in_errors' : True } , 'type=string_type' ),
123
+ (CoreConfig () , 'type=string_type, input_value=123, input_type=int' ),
124
+ (CoreConfig ( hide_input_in_errors = False ) , 'type=string_type, input_value=123, input_type=int' ),
125
+ (CoreConfig ( hide_input_in_errors = True ) , 'type=string_type' ),
126
126
),
127
127
)
128
128
def test_hide_input_in_errors (config , input_str ):
129
129
v = SchemaValidator (
130
- {
131
- 'type' : 'model' ,
132
- 'cls' : MyModel ,
133
- 'schema' : {'type' : 'model-fields' , 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}}},
134
- },
135
- config ,
130
+ cs .model_schema (
131
+ cls = MyModel , schema = cs .model_fields_schema (fields = {'f' : cs .model_field (schema = cs .str_schema ())})
132
+ ),
133
+ config = config ,
136
134
)
137
135
138
136
with pytest .raises (ValidationError , match = re .escape (f'Input should be a valid string [{ input_str } ]' )):
139
137
assert v .validate_python ({'f' : 123 })
140
138
141
139
142
140
def test_cache_strings ():
143
- v = SchemaValidator ({ 'type' : 'str' } )
141
+ v = SchemaValidator (cs . str_schema () )
144
142
assert 'cache_strings=True' in plain_repr (v )
145
143
146
- v = SchemaValidator ({ 'type' : 'str' }, { ' cache_strings' : True } )
144
+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( cache_strings = True ) )
147
145
assert 'cache_strings=True' in plain_repr (v )
148
146
149
- v = SchemaValidator ({ 'type' : 'str' }, { ' cache_strings' : False } )
147
+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( cache_strings = False ) )
150
148
assert 'cache_strings=False' in plain_repr (v )
151
149
152
- v = SchemaValidator ({ 'type' : 'str' }, { ' cache_strings' : ' keys'} )
150
+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( cache_strings = ' keys') )
153
151
assert "cache_strings='keys'" in plain_repr (v )
0 commit comments