Skip to content

Commit 4fe2a8b

Browse files
committed
Refactor ValidationError.to_schema_error to _Error.create_from
1 parent 0e49379 commit 4fe2a8b

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

jsonschema.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ def __init__(self, message, cause=None, context=()):
7070
self.instance = None
7171
self.schema = None
7272

73+
@classmethod
74+
def create_from(cls, other):
75+
new_error = cls(other.message, other.cause, other.context)
76+
new_error.path = other.path
77+
new_error.schema_path = other.schema_path
78+
new_error._details_set = other._details_set
79+
new_error.validator_keyword = other.validator_keyword
80+
new_error.validator_value = other.validator_value
81+
new_error.instance = other.instance
82+
new_error.schema = other.schema
83+
return new_error
84+
7385
@property
7486
def validator(self):
7587
warnings.warn(
@@ -112,22 +124,8 @@ def __unicode__(self):
112124
__str__ = __unicode__
113125

114126

115-
class ValidationError(_Error):
116-
def to_schema_error(self):
117-
schema_error = SchemaError(self.message)
118-
schema_error.path = self.path
119-
schema_error.schema_path = self.schema_path
120-
schema_error.context = self.context
121-
schema_error.cause = self.cause
122-
schema_error._details_set = self._details_set
123-
schema_error.validator_keyword = self.validator_keyword
124-
schema_error.validator_value = self.validator_value
125-
schema_error.instance = self.instance
126-
schema_error.schema = self.schema
127-
return schema_error
128-
129-
130127
class SchemaError(_Error): pass
128+
class ValidationError(_Error): pass
131129
class RefResolutionError(Exception): pass
132130
class UnknownType(Exception): pass
133131

@@ -236,7 +234,7 @@ def is_valid(self, instance, _schema=None):
236234
@classmethod
237235
def check_schema(cls, schema):
238236
for error in cls(cls.META_SCHEMA).iter_errors(schema):
239-
raise error.to_schema_error()
237+
raise SchemaError.create_from(error)
240238

241239
def iter_errors(self, instance, _schema=None):
242240
if _schema is None:

0 commit comments

Comments
 (0)