Skip to content

Commit

Permalink
Merge pull request #2 from mystand/omit_in_schema
Browse files Browse the repository at this point in the history
add omit to schema
  • Loading branch information
kutase authored Sep 25, 2018
2 parents c55fe5e + 8c816b5 commit 89673a6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,21 @@ serialize.user(test_users)
// photo: [[Object], [Object]],
// name: 'Tester2',
// password: '12345678' }]

// If you always want to get all fields of object excluding some of them, use omit in schema
serialize.add('omitUserPassword', {
omit: ['password']
})

const plainUser = {
name: 'Tester',
email: '[email protected]',
password: '12345'
}

console.log(serialize.omitUserPassword(plainUser))
// ==>
// { name: 'Tester', email: '[email protected]' }
```

### Options
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class Serializer {
}
}

if (Array.isArray(schema.omit)) {
attributes = attributes.filter(attribute => !schema.omit.includes(attribute))
}

if (!_.isObject(options)) {
throw new WrongTypeError('options', 'object', options)
}
Expand Down
17 changes: 16 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ serialize.add('photo', {
}
})

// If you want to add all fields of object with some formatters and don't want to
// If you want to add all fields of object with some formatters and don't want to
// write all fields in attributes option, you can use extraAttributes option.
serialize.add('userThroughExtraAttributes', {
extraAttributes: ['email1', 'email2'],
Expand Down Expand Up @@ -198,3 +198,18 @@ console.log(serialize.testGetters(galleryWithGettersValues).map(el => el.gallery
// { url: '/test/path/img41337.png1337', test: [Object] }
// ]
// ]

// If you always want to get all fields of object excluding some of them, use omit in schema
serialize.add('omitUserPassword', {
omit: ['password']
})

const plainUser = {
name: 'Tester',
email: '[email protected]',
password: '12345'
}

console.log(serialize.omitUserPassword(plainUser))
// ==>
// { name: 'Tester', email: '[email protected]' }

0 comments on commit 89673a6

Please sign in to comment.