File tree Expand file tree Collapse file tree 5 files changed +48
-30
lines changed Expand file tree Collapse file tree 5 files changed +48
-30
lines changed Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ Installation
2020
2121.. md-tab-set ::
2222
23- .. md-tab-item :: Pip + PyPI (recommented )
23+ .. md-tab-item :: Pip + PyPI (recommended )
2424
2525 .. code-block :: console
2626
Original file line number Diff line number Diff line change 1- from copy import deepcopy
21from typing import Any
3- from typing import Dict
42from typing import Hashable
5- from typing import ItemsView
63from typing import Iterator
74from typing import List
85from typing import Mapping
@@ -112,7 +109,7 @@ def type(
112109 # * nullable: true is only meaningful in combination with a type
113110 # assertion specified in the same Schema Object.
114111 # * nullable: true operates within a single Schema Object
115- if "nullable" in schema and schema ["nullable" ] == True :
112+ if "nullable" in schema and schema ["nullable" ] is True :
116113 return
117114 yield ValidationError ("None for not nullable" )
118115
Original file line number Diff line number Diff line change 1- from typing import Any
2- from typing import Hashable
3- from typing import Mapping
4- from typing import Type
1+ from typing import Any , Hashable , Mapping , Type
52
63from jsonschema .exceptions import best_match
74from jsonschema .protocols import Validator
@@ -16,8 +13,24 @@ def validate(
1613 * args : Any ,
1714 ** kwargs : Any
1815) -> None :
16+ """
17+ Validate an instance against a given schema using the specified validator class
18+
19+ Args:
20+ instance: The instance to validate.
21+ schema: The schema to validate against
22+ cls: The validator class to use (defaults to OAS31Validator)
23+ *args: Additional positional arguments to pass to the validator
24+ **kwargs: Additional keyword arguments to pass to the validator
25+
26+ Raises:
27+ jsonschema.exceptions.ValidationError: If the instance is invalid according to the schema
28+ """
1929 cls .check_schema (schema )
2030 validator = cls (schema , * args , ** kwargs )
21- error = best_match (validator .evolve (schema = schema ).iter_errors (instance ))
22- if error is not None :
31+ errors = list (validator .evolve (schema = schema ).iter_errors (instance ))
32+
33+ if errors :
34+ error = best_match (errors )
35+ error .message = f"Validation failed: { error .message } "
2336 raise error
Original file line number Diff line number Diff line change 1- import warnings
2- from typing import Any
3- from typing import Type
4-
51from jsonschema import _keywords
62from jsonschema import _legacy_keywords
73from jsonschema .validators import Draft202012Validator
Original file line number Diff line number Diff line change 1- from unittest import TestCase
2-
1+ import pytest
32from openapi_schema_validator import validate
43
54
6- class ValidateTest (TestCase ):
7- def test_validate_does_not_mutate_schema_adding_nullable_key (self ):
8- schema = {
9- "type" : "object" ,
10- "properties" : {
11- "email" : {"type" : "string" },
12- "enabled" : {
13- "type" : "boolean" ,
14- },
5+ @pytest .fixture (scope = "function" )
6+ def schema ():
7+ return {
8+ "type" : "object" ,
9+ "properties" : {
10+ "email" : {"type" : "string" },
11+ "enabled" : {
12+ "type" : "boolean" ,
1513 },
16- "example" : {"enabled" : False , "email" : "foo@bar.com" },
17- }
14+ },
15+ "example" : {"enabled" : False , "email" : "foo@bar.com" },
16+ }
17+
18+
19+ def test_validate_does_not_add_nullable_to_schema (schema ):
20+ """
21+ Verify that calling validate does not add the 'nullable' key to the schema
22+ """
23+ validate ({"email" : "foo@bar.com" }, schema )
24+ assert "nullable" not in schema ["properties" ]["email" ].keys ()
1825
19- validate ({"email" : "foo@bar.com" }, schema )
2026
21- self .assertTrue ("nullable" not in schema ["properties" ]["email" ].keys ())
27+ def test_validate_does_not_mutate_schema (schema ):
28+ """
29+ Verify that calling validate does not mutate the schema
30+ """
31+ original_schema = schema .copy ()
32+ validate ({"email" : "foo@bar.com" }, schema )
33+ assert schema == original_schema
You can’t perform that action at this time.
0 commit comments