Skip to content

Commit 31721f7

Browse files
author
Chris Wacek
committed
Don't validate objects on creation unless from_json
1 parent 5afc6d9 commit 31721f7

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

python_jsonschema_objects/classbuilder.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def __repr__(self):
5252
def from_json(cls, jsonmsg):
5353
import json
5454
msg = json.loads(jsonmsg)
55-
return cls(**msg)
55+
obj = cls(**msg)
56+
obj.validate()
57+
return obj
5658

5759
def __init__(this, **props):
5860
this._extended_properties = dict()
@@ -68,15 +70,15 @@ def __init__(this, **props):
6870

6971
try:
7072
logging.debug("Setting value for '{0}' to {1}"
71-
.format(prop, props[prop]))
73+
.format(prop, props[prop]))
7274
setattr(this, prop, props[prop])
7375
except validators.ValidationError as e:
7476
import sys
7577
raise type(e), type(e)(str(e) + " \nwhile setting '{0}' in {1}".format(
7678
prop, this.__class__.__name__)), sys.exc_info()[2]
7779

78-
if len(props) > 0:
79-
this.validate()
80+
#if len(props) > 0:
81+
# this.validate()
8082

8183
def __setattr__(self, name, val):
8284
if name.startswith("_"):

test/test.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ def set_status(status):
9898
str(person.lastName).should.equal("Bond")
9999

100100
it 'should not allow required attributes to be missing':
101-
self.Person.when.called_with(firstName="James").should.throw(
101+
person = self.Person(firstName="James")
102+
103+
person.validate.when.called_with().should.throw(
102104
pjs.ValidationError
103105
)
104106

0 commit comments

Comments
 (0)