Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #8 from IamSmith/master

Fix to issue 4 stripUnknownProperties subschema
  • Loading branch information...
commit 4ae55290984b14b32ffbe5a256a734106026136d 2 parents 77406ed + 1e04d7c
@serby authored
Showing with 33 additions and 6 deletions.
  1. +5 −3 lib/schemata.js
  2. +28 −3 test/schemata.test.js
View
8 lib/schemata.js 100644 → 100755
@@ -129,7 +129,7 @@ module.exports = function(schema) {
* @return {Object} The stripped down object
* @api public
*/
- function stripUnknownProperties(entityObject, tag) {
+ function stripUnknownProperties(entityObject, tag, ignoreTagForSubSchemas) {
var newEntity = {}
;
@@ -138,15 +138,17 @@ module.exports = function(schema) {
if (typeof schema[key].type !== 'undefined') {
// This infers that there is a sub-schema
if (typeof schema[key].type.stripUnknownProperties === 'function') {
+ var subSchemaTag = ignoreTagForSubSchemas ? undefined : tag
return newEntity[key] = schema[key].type.stripUnknownProperties(
- entityObject[key], tag);
+ entityObject[key], subSchemaTag, ignoreTagForSubSchemas);
} else if ((typeof schema[key].type.arraySchema !== 'undefined')
&& (typeof schema[key].type.arraySchema.stripUnknownProperties === 'function')
&& (Array.isArray(entityObject[key]))) {
// Here the property must be an array schema
+ var subSchemaTag = ignoreTagForSubSchemas ? undefined : tag
entityObject[key].forEach(function(arrayItem, index) {
entityObject[key][index] =
- schema[key].type.arraySchema.stripUnknownProperties(arrayItem, tag);
+ schema[key].type.arraySchema.stripUnknownProperties(arrayItem, subSchemaTag, ignoreTagForSubSchemas);
});
}
}
View
31 test/schemata.test.js 100644 → 100755
@@ -28,7 +28,9 @@ function createContactSchema() {
function createCommentSchema() {
return schemata({
email: {},
- comment: {}
+ comment: {
+ tag: ['auto']
+ }
})
}
@@ -36,14 +38,17 @@ function createBlogSchema() {
var blogSchema = schemata({
title: {
+ tag: ['auto']
},
body: {
+ tag: ['auto']
},
author: {
type: createContactSchema()
},
- comments: {
- type: schemata.Array(createCommentSchema())
+ comments:
+ { type: schemata.Array(createCommentSchema())
+ , tag: ['auto']
}
})
return blogSchema
@@ -270,6 +275,26 @@ describe('schemata', function() {
.should.eql({ author: { name: 'Paul' }, comments: [{ email: null, comment: null } ] })
})
+ it('strips out properties for parent but ignores sub-schemas when "ignoreSubSchemas" is true', function() {
+ var schema = createBlogSchema()
+ , comment = createCommentSchema().makeBlank()
+
+ comment.comment = 'Do not strip out my comment'
+ comment.extra = 'This will be striped as its not in the schema at all'
+ schema.stripUnknownProperties({ title: 'My Blog', author: { name: 'Paul' }, comments: [comment]}, 'auto', true)
+ .should.eql({ title: 'My Blog', comments: [{ email: null, comment: 'Do not strip out my comment' } ] })
+ })
+
+ it('strips out properties for parent and sub-schemas when "ignoreSubSchemas" is false', function() {
+ var schema = createBlogSchema()
+ , comment = createCommentSchema().makeBlank()
+
+ comment.comment = 'Do not strip out my comment'
+ comment.extra = 'Hello'
+ schema.stripUnknownProperties({ title: 'My Blog', author: { name: 'Paul' }, comments: [comment]}, 'auto')
+ .should.eql({ title: 'My Blog', comments: [{ comment: 'Do not strip out my comment' } ] })
+ })
+
})
describe('#cast()', function() {

0 comments on commit 4ae5529

Please sign in to comment.
Something went wrong with that request. Please try again.