55from dirty_equals import FunctionCheck , HasAttributes , IsInstance
66
77from pydantic_core import CoreConfig , SchemaValidator , ValidationError
8+ from pydantic_core import core_schema as cs
89
910from .conftest import Err , plain_repr
1011
1112
1213def 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 ) )
1415 r = plain_repr (v )
1516 assert 'min_length:Some(2)' in r
1617 assert 'max_length:Some(5)' in r
@@ -19,14 +20,14 @@ def test_on_field():
1920
2021
2122def test_on_config ():
22- v = SchemaValidator ({ 'type' : 'str' }, { ' str_max_length' : 5 } )
23+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( str_max_length = 5 ) )
2324 assert 'max_length:Some(5)' in plain_repr (v )
2425 assert v .isinstance_python ('test' ) is True
2526 assert v .isinstance_python ('test long' ) is False
2627
2728
2829def 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 ) )
3031 assert 'max_length:Some(5)' in plain_repr (v )
3132 assert v .isinstance_python ('test' ) is True
3233 assert v .isinstance_python ('test long' ) is False
@@ -39,12 +40,11 @@ class MyModel:
3940
4041def test_on_model_class ():
4142 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+ )
4848 )
4949 assert 'max_length:Some(5)' in plain_repr (v )
5050 assert v .isinstance_python ({'f' : 'test' }) is True
@@ -53,15 +53,11 @@ def test_on_model_class():
5353
5454def test_field_priority_model ():
5555 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+ )
6561 )
6662 assert 'max_length:Some(5)' in plain_repr (v )
6763 assert v .isinstance_python ({'f' : 'test' }) is True
@@ -71,29 +67,34 @@ def test_field_priority_model():
7167@pytest .mark .parametrize (
7268 'config,float_field_schema,input_value,expected' ,
7369 [
74- ({}, {'type' : 'float' }, {'x' : 'nan' }, IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan ))),
7570 (
76- { 'allow_inf_nan' : True } ,
77- { 'type' : 'float' } ,
71+ CoreConfig () ,
72+ cs . float_schema () ,
7873 {'x' : 'nan' },
7974 IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan )),
8075 ),
8176 (
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 (),
8485 {'x' : 'nan' },
8586 Err ('Input should be a finite number [type=finite_number,' ),
8687 ),
8788 # field `allow_inf_nan` (if set) should have priority over global config
8889 (
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 ) ,
9192 {'x' : 'nan' },
9293 Err ('Input should be a finite number [type=finite_number,' ),
9394 ),
9495 (
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 ) ,
9798 {'x' : 'nan' },
9899 IsInstance (MyModel ) & HasAttributes (x = FunctionCheck (math .isnan )),
99100 ),
@@ -102,12 +103,11 @@ def test_field_priority_model():
102103)
103104def test_allow_inf_nan (config : CoreConfig , float_field_schema , input_value , expected ):
104105 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+ )
111111 )
112112 if isinstance (expected , Err ):
113113 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
120120@pytest .mark .parametrize (
121121 'config,input_str' ,
122122 (
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' ),
126126 ),
127127)
128128def test_hide_input_in_errors (config , input_str ):
129129 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 ,
136134 )
137135
138136 with pytest .raises (ValidationError , match = re .escape (f'Input should be a valid string [{ input_str } ]' )):
139137 assert v .validate_python ({'f' : 123 })
140138
141139
142140def test_cache_strings ():
143- v = SchemaValidator ({ 'type' : 'str' } )
141+ v = SchemaValidator (cs . str_schema () )
144142 assert 'cache_strings=True' in plain_repr (v )
145143
146- v = SchemaValidator ({ 'type' : 'str' }, { ' cache_strings' : True } )
144+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( cache_strings = True ) )
147145 assert 'cache_strings=True' in plain_repr (v )
148146
149- v = SchemaValidator ({ 'type' : 'str' }, { ' cache_strings' : False } )
147+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( cache_strings = False ) )
150148 assert 'cache_strings=False' in plain_repr (v )
151149
152- v = SchemaValidator ({ 'type' : 'str' }, { ' cache_strings' : ' keys'} )
150+ v = SchemaValidator (cs . str_schema (), config = CoreConfig ( cache_strings = ' keys') )
153151 assert "cache_strings='keys'" in plain_repr (v )
0 commit comments