Skip to content
This repository has been archived by the owner on Mar 28, 2019. It is now read-only.

Commit

Permalink
Add preserve_unknown to the Schema options
Browse files Browse the repository at this point in the history
  • Loading branch information
almet committed Feb 26, 2015
1 parent 1709963 commit e6a855d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cliquet/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ class Options:
* ``readonly_fields``: Fields that cannot be updated
* ``unique_fields``: Fields that must have unique values for the user
collection.
* ``preserve_unknown``: Define if unknown fields should be preserved
or not (default False).
"""
readonly_fields = ('id', 'last_modified')
unique_fields = ('id', 'last_modified')
preserve_unknown = False

def is_readonly(self, field):
"""Return True if specified field name is read-only.
Expand All @@ -62,3 +65,10 @@ def is_readonly(self, field):
:rtype: boolean
"""
return field in self.Options.readonly_fields

def schema_type(self, **kw):
if self.Options.preserve_unknown is True:
unknown = 'preserve'
else:
unknown = 'ignore'
return colander.Mapping(unknown=unknown)
27 changes: 27 additions & 0 deletions cliquet/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,30 @@ def test_raises_invalid_if_no_scheme(self):
self.assertRaises(colander.Invalid,
schema.URL().deserialize,
url)


class ResourceSchemaTest(unittest.TestCase):

def test_preserves_unknown_fields_when_specified(self):
class PreserveSchema(schema.ResourceSchema):
class Options:
preserve_unknown = True

schema_instance = PreserveSchema()
deserialized = schema_instance.deserialize({'foo': 'bar'})
self.assertIn('foo', deserialized)
self.assertEquals(deserialized['foo'], 'bar')

def test_ignore_unknwon_fields_when_specified(self):
class PreserveSchema(schema.ResourceSchema):
class Options:
preserve_unknown = False

schema_instance = PreserveSchema()
deserialized = schema_instance.deserialize({'foo': 'bar'})
self.assertNotIn('foo', deserialized)

def test_ignore_unknwon_fields_by_default(self):
schema_instance = schema.ResourceSchema()
deserialized = schema_instance.deserialize({'foo': 'bar'})
self.assertNotIn('foo', deserialized)

0 comments on commit e6a855d

Please sign in to comment.