New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-hidden

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-hidden - npm Package Compare versions

Comparing version

to
1.6.2

.prettierrc

3

CHANGELOG.md
# Changelog
1.6.2
- added TypeScript definitions (thanks to @marshalys)
1.6.0

@@ -5,0 +8,0 @@

63

lib/mongoose-hidden.js

@@ -1,2 +0,2 @@

'use strict';
'use strict'

@@ -40,8 +40,10 @@ const mpath = require('mpath')

return schema.options[key] === true
return (
schema.options[key] === true
|| (typeof schema.options[key] === 'function' && schema.options[key](doc, transformed))
)
}
/**
* Should a property by be hidden er not
* Should a property be hidden er not
*

@@ -66,5 +68,7 @@ * @access private

// Test hide by option or schema
return testOptions(options, pathname)
return (
testOptions(options, pathname)
|| testSchema(schema, hide, doc, transformed)
|| testSchema(schema, hideTarget, doc, transformed)
)
}

@@ -83,4 +87,6 @@

function shouldCopyVirtual(schema, key, options, target) {
return schema.pathType(key) === 'virtual'
return (
schema.pathType(key) === 'virtual'
&& [hide, `hide${target}`].indexOf(options.virtuals[key]) === -1
)
}

@@ -158,3 +164,3 @@

function ensureOption(options) {
return function (option, fallback) {
return function(option, fallback) {
return option in options ? options[option] : fallback

@@ -173,3 +179,3 @@ }

function prepOptions(options, defaults) {
let _options = options = options || {}
let _options = (options = options || {})

@@ -183,3 +189,3 @@ // Set defaults from options and default

defaultHidden: Object.assign({}, ensure(defaultHidden, defaults.defaultHidden)),
virtuals: ensure(virtuals, defaults.virtuals)
virtuals: ensure(virtuals, defaults.virtuals),
}

@@ -208,3 +214,3 @@

*/
const partsToValue = function (parts, value) {
const partsToValue = function(parts, value) {
if (parts.length === 0) {

@@ -226,3 +232,3 @@ return value

*/
const setPath = function (obj, path, value) {
const setPath = function(obj, path, value) {
const parts = path.split('.')

@@ -239,12 +245,16 @@

module.exports = function (defaults) {
let _defaults = Object.assign({}, {
autoHide: true,
autoHideJSON: true,
autoHideObject: true,
defaultHidden: { '_id': true, '__v': true },
virtuals: {},
}, defaults || {})
module.exports = function(defaults) {
let _defaults = Object.assign(
{},
{
autoHide: true,
autoHideJSON: true,
autoHideObject: true,
defaultHidden: { _id: true, __v: true },
virtuals: {},
},
defaults || {}
)
return function (schema, options) {
return function(schema, options) {
options = prepOptions(options, _defaults)

@@ -256,4 +266,4 @@ log(options)

let transformer = function (target, prevTransform) {
return function (doc, transformed, opt) {
let transformer = function(target, prevTransform) {
return function(doc, transformed, opt) {
log(transformed)

@@ -271,3 +281,3 @@

log('REAL AND NESTED VALUES')
paths.forEach(function (pathname) {
paths.forEach(function(pathname) {
let schemaType = schema.path(pathname)

@@ -306,3 +316,3 @@ if (shouldHide(schemaType, options, target, doc, transformed, pathname)) {

virtuals: toJSONOptions[virtuals] || false,
transform: transformer('JSON', toJSONOptions['transform'] || null)
transform: transformer('JSON', toJSONOptions['transform'] || null),
})

@@ -315,3 +325,3 @@

virtuals: toObjectOptions[virtuals] || false,
transform: transformer('Object', toObjectOptions['transform'] || null)
transform: transformer('Object', toObjectOptions['transform'] || null),
})

@@ -322,5 +332,2 @@ }

/* for testing */
module.exports['__test__'] = {
setPath: setPath,
}
module.exports['__test__'] = { setPath: setPath }
{
"name": "mongoose-hidden",
"version": "1.6.1",
"version": "1.6.2",
"author": "Michael Bøcker-Larsen <m19n@pm.me>",

@@ -30,3 +30,3 @@ "description": "Hides certain model properties when invoking toJSON or toObject.",

"debug": "^4.0.1",
"mpath": "^0.5.1"
"mpath": "^0.6.0"
},

@@ -33,0 +33,0 @@ "devDependencies": {

@@ -7,3 +7,3 @@ # mongoose-hidden

[![Known vulnerabilities](https://snyk.io/test/github/mblarsen/mongoose-hidden/badge.svg)](https://snyk.io/test/github/mblarsen/mongoose-hidden)
[![Total downloads](https://img.shields.io/npm/dt/mongoose-hidden.svg)](https://www.npmjs.com/package/mongoose-hidden)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
[![Monthly downloads](https://img.shields.io/npm/dm/mongoose-hidden.svg)](https://www.npmjs.com/package/mongoose-hidden)

@@ -10,0 +10,0 @@ [![NPM version](http://img.shields.io/npm/v/mongoose-hidden.svg)](https://www.npmjs.com/package/mongoose-hidden)

@@ -1,2 +0,2 @@

"use strict";
'use strict'

@@ -12,21 +12,36 @@ const should = require('should')

describe("mongoose-hidden", function () {
let testUser = { name: "Joe", email: "joe@example.com", password: "secret" }
let testUserSub = { name: "Joe", email: "joe@example.com", password: "secret", spouse: { name: "Maries" } }
let testUserSub2 = { name: "Joe", email: "joe@example.com", password: "secret", spouse: { name: "Maries", age: 37 } }
let testUser2 = { name: "Marie", email: "marie@example.com", password: "secret" }
let testUser3 = { name: "Joe", email: { prefix: 'joe', suffix: 'example.com' }, password: "secret" }
let testCompany = { "_id": "5613a1c7e1095d8e71ae90da", "name": "GOGGLE", "code": "GOG" }
let testCompany2 = { "_id": "5613a1c7e1095d8e71ae90db", "name": "APPLE", "code": "APL" }
let testPassword = "secret"
let keyVersion = "__v"
let keyId = "_id"
describe('mongoose-hidden', function() {
let testUser = { name: 'Joe', email: 'joe@example.com', password: 'secret' }
let testUserSub = {
name: 'Joe',
email: 'joe@example.com',
password: 'secret',
spouse: { name: 'Maries' },
}
let testUserSub2 = {
name: 'Joe',
email: 'joe@example.com',
password: 'secret',
spouse: { name: 'Maries', age: 37 },
}
let testUser2 = { name: 'Marie', email: 'marie@example.com', password: 'secret' }
let testUser3 = {
name: 'Joe',
email: { prefix: 'joe', suffix: 'example.com' },
password: 'secret',
}
let testCompany = { _id: '5613a1c7e1095d8e71ae90da', name: 'GOGGLE', code: 'GOG' }
let testCompany2 = { _id: '5613a1c7e1095d8e71ae90db', name: 'APPLE', code: 'APL' }
let testPassword = 'secret'
let keyVersion = '__v'
let keyId = '_id'
let defineModel = function (name, schemaProperties, pluginOptions) {
let defineModel = function(name, schemaProperties, pluginOptions) {
if (typeof name === 'object') {
pluginOptions = schemaProperties
schemaProperties = name
name = "User"
name = 'User'
}
let schema = schemaProperties instanceof Schema ? schemaProperties : new Schema(schemaProperties)
let schema
= schemaProperties instanceof Schema ? schemaProperties : new Schema(schemaProperties)
schema.plugin(mongooseHidden, pluginOptions || {})

@@ -36,9 +51,7 @@ return mongoose.model(name, schema, undefined)

before(function (done) {
mongoose.connect('mongodb://localhost/mongoose-hidden', function (err) {
before(function(done) {
mongoose.connect('mongodb://localhost/mongoose-hidden', function(err) {
if (err) {
console.error('MongoDB: ' + err.message)
console.error(
'MongoDB is running? Is it accessible by this application?'
)
console.error('MongoDB is running? Is it accessible by this application?')
return done(err)

@@ -50,3 +63,3 @@ }

afterEach(function (done) {
afterEach(function(done) {
mongoose.modelSchemas = {}

@@ -57,17 +70,17 @@ mongoose.models = {}

after(function (done) {
after(function(done) {
mongoose.connection.close(done)
})
describe("A model with no hidden properties defined", function () {
it("should return all properties", function (done) {
describe('A model with no hidden properties defined', function() {
it('should return all properties', function(done) {
let User = defineModel({
name: String,
email: String,
password: String
password: String,
})
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
userJson.password.should.equal(testPassword)

@@ -78,4 +91,4 @@ done()

describe("A model with a hidden properties defined", function () {
it("shouldn't return those property", function (done) {
describe('A model with a hidden properties defined', function() {
it("shouldn't return those property", function(done) {
let User = defineModel({

@@ -86,9 +99,9 @@ name: String,

type: String,
hide: true
}
hide: true,
},
})
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson.password)

@@ -99,4 +112,4 @@ done()

describe("A model with default hidden properties defined", function () {
it("shouldn't return __v property", function (done) {
describe('A model with default hidden properties defined', function() {
it("shouldn't return __v property", function(done) {
let User = defineModel({

@@ -107,10 +120,10 @@ name: String,

type: String,
hide: true
}
hide: true,
},
})
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson.password)

@@ -123,17 +136,20 @@ should.not.exist(userJson[keyVersion])

describe("Default hiding turned off", function () {
it("shouldn't hide any properties", function (done) {
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hide: true
}
}, { hide: false, hideJSON: true, hideObject: true })
describe('Default hiding turned off', function() {
it("shouldn't hide any properties", function(done) {
let User = defineModel(
{
name: String,
email: String,
password: {
type: String,
hide: true,
},
},
{ hide: false, hideJSON: true, hideObject: true }
)
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
testPassword.should.equal(userJson.password)

@@ -147,18 +163,21 @@ should.exist(userJson[keyVersion])

describe("Default hiding turned off for JSON only", function () {
it("shouldn't hide any properties", function (done) {
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hide: true
}
}, { hideJSON: false })
describe('Default hiding turned off for JSON only', function() {
it("shouldn't hide any properties", function(done) {
let User = defineModel(
{
name: String,
email: String,
password: {
type: String,
hide: true,
},
},
{ hideJSON: false }
)
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
userJson.password.should.equal(testPassword)

@@ -169,5 +188,5 @@ should.exist(userJson[keyVersion])

let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
should.not.exist(userObject["password"])
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
should.not.exist(userObject['password'])
should.not.exist(userObject[keyVersion])

@@ -180,18 +199,21 @@ should.not.exist(userObject[keyId])

describe("Default hiding turned off for object only", function () {
it("shouldn't hide any properties", function (done) {
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hide: true
}
}, { hideObject: false })
describe('Default hiding turned off for object only', function() {
it("shouldn't hide any properties", function(done) {
let User = defineModel(
{
name: String,
email: String,
password: {
type: String,
hide: true,
},
},
{ hideObject: false }
)
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
userObject.password.should.equal(testPassword)

@@ -202,5 +224,5 @@ should.exist(userObject[keyVersion])

let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
should.not.exist(userJson["password"])
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson['password'])
should.not.exist(userJson[keyVersion])

@@ -213,4 +235,4 @@ should.not.exist(userJson[keyId])

describe("Default hiding on, JSON option property", function () {
it("shouldn't hide any properties", function (done) {
describe('Default hiding on, JSON option property', function() {
it("shouldn't hide any properties", function(done) {
let User = defineModel({

@@ -221,17 +243,17 @@ name: String,

type: String,
hideJSON: true
}
hideJSON: true,
},
})
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
userObject.password.should.equal(testPassword)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
should.not.exist(userJson["password"])
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson['password'])
done()

@@ -242,4 +264,4 @@ })

describe("Default hiding on, object option property", function () {
it("shouldn't hide any properties", function (done) {
describe('Default hiding on, object option property', function() {
it("shouldn't hide any properties", function(done) {
let User = defineModel({

@@ -250,16 +272,16 @@ name: String,

type: String,
hideObject: true
}
hideObject: true,
},
})
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
should.not.exist(userObject["password"])
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
should.not.exist(userObject['password'])
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
userJson.password.should.equal(testPassword)

@@ -271,4 +293,4 @@ done()

describe("Default hiding on, object option property off", function () {
it("shouldn't hide any properties", function (done) {
describe('Default hiding on, object option property off', function() {
it("shouldn't hide any properties", function(done) {
let User = defineModel({

@@ -279,16 +301,16 @@ name: String,

type: String,
hideObject: false
}
hideObject: false,
},
})
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
userObject.password.should.equal(testPassword)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
userJson.password.should.equal(testPassword)

@@ -300,141 +322,132 @@ done()

describe("A model with hidden properties defined using function",
function () {
it(
"shouldn't return password property for Joe for both JSON and object",
function (done) {
let testFunction = function (doc, ret) {
return doc.name === 'Joe'
}
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hide: testFunction
}
})
describe('A model with hidden properties defined using function', function() {
it("shouldn't return password property for Joe for both JSON and object", function(done) {
let testFunction = function(doc, ret) {
return doc.name === 'Joe'
}
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hide: testFunction,
},
})
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
should.not.exist(userJson.password)
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
should.not.exist(userObject.password)
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson.password)
let userObject = user.toObject()
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
should.not.exist(userObject.password)
user = new User(testUser2)
userJson = user.toJSON()
userJson.name.should.equal("Marie")
userJson.email.should.equal("marie@example.com")
userJson.password.should.equal(testPassword)
userObject = user.toJSON()
userObject.name.should.equal("Marie")
userObject.email.should.equal("marie@example.com")
userObject.password.should.equal(testPassword)
user = new User(testUser2)
userJson = user.toJSON()
userJson.name.should.equal('Marie')
userJson.email.should.equal('marie@example.com')
userJson.password.should.equal(testPassword)
userObject = user.toJSON()
userObject.name.should.equal('Marie')
userObject.email.should.equal('marie@example.com')
userObject.password.should.equal(testPassword)
done()
})
done()
})
})
describe(
"A model with a hidden properties defined using function for JSON",
function () {
it("shouldn't return password property for Joe for only for JSON",
function (done) {
let testFunction = function (doc) {
return doc.name === 'Joe'
}
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hideJSON: testFunction
}
})
describe('A model with a hidden properties defined using function for JSON', function() {
it("shouldn't return password property for Joe for only for JSON", function(done) {
let testFunction = function(doc) {
return doc.name === 'Joe'
}
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hideJSON: testFunction,
},
})
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
should.not.exist(userJson.password)
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
userObject.password.should.equal(testPassword)
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson.password)
let userObject = user.toObject()
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
userObject.password.should.equal(testPassword)
user = new User(testUser2)
userJson = user.toJSON()
userJson.name.should.equal("Marie")
userJson.email.should.equal("marie@example.com")
userJson.password.should.equal(testPassword)
userObject = user.toObject()
userObject.name.should.equal("Marie")
userObject.email.should.equal("marie@example.com")
userObject.password.should.equal(testPassword)
user = new User(testUser2)
userJson = user.toJSON()
userJson.name.should.equal('Marie')
userJson.email.should.equal('marie@example.com')
userJson.password.should.equal(testPassword)
userObject = user.toObject()
userObject.name.should.equal('Marie')
userObject.email.should.equal('marie@example.com')
userObject.password.should.equal(testPassword)
done()
})
done()
})
})
describe(
"A model with a hidden properties defined using function for object",
function () {
it("shouldn't return password property for Joe for only for object",
function (done) {
let testFunction = function (doc) {
return doc.name === 'Joe'
}
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hideObject: testFunction
}
})
describe('A model with a hidden properties defined using function for object', function() {
it("shouldn't return password property for Joe for only for object", function(done) {
let testFunction = function(doc) {
return doc.name === 'Joe'
}
let User = defineModel({
name: String,
email: String,
password: {
type: String,
hideObject: testFunction,
},
})
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.password.should.equal(testPassword)
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
should.not.exist(userObject.password)
let user = new User(testUser)
let userJson = user.toJSON()
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
userJson.password.should.equal(testPassword)
let userObject = user.toObject()
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
should.not.exist(userObject.password)
user = new User(testUser2)
userJson = user.toJSON()
userJson.name.should.equal("Marie")
userJson.email.should.equal("marie@example.com")
userJson.password.should.equal(testPassword)
userObject = user.toObject()
userObject.name.should.equal("Marie")
userObject.email.should.equal("marie@example.com")
userObject.password.should.equal(testPassword)
user = new User(testUser2)
userJson = user.toJSON()
userJson.name.should.equal('Marie')
userJson.email.should.equal('marie@example.com')
userJson.password.should.equal(testPassword)
userObject = user.toObject()
userObject.name.should.equal('Marie')
userObject.email.should.equal('marie@example.com')
userObject.password.should.equal(testPassword)
done()
})
done()
})
})
describe("A model with password set as default hidden", function () {
it("shouldn't return password, but __v", function (done) {
describe('A model with password set as default hidden', function() {
it("shouldn't return password, but __v", function(done) {
let UserSchema = new Schema({
name: String,
email: String,
password: String
password: String,
})
UserSchema.plugin(plugin({ defaultHidden: { "password": true } }))
UserSchema.plugin(plugin({ defaultHidden: { password: true } }))
let User = mongoose.model('User', UserSchema, undefined, { cache: false })
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.name.should.equal('Joe')
user[keyVersion].should.exist // eslint-disable-line
userJson.email.should.equal("joe@example.com")
should.not.exist(userJson["password"])
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson['password'])
done()

@@ -445,41 +458,39 @@ })

describe(
"A model with password and password set as default hidden overriden with option",
function () {
it("should return password", function (done) {
let UserSchema = new Schema({
name: String,
email: String,
password: String
})
UserSchema.plugin(plugin({ defaultHidden: { "password": true } }), { defaultHidden: {} })
let User = mongoose.model('User', UserSchema, undefined, { cache: false })
let user = new User(testUser)
user.save(function () {
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.password.should.equal(testPassword)
done()
})
describe('A model with password and password set as default hidden overriden with option', function() {
it('should return password', function(done) {
let UserSchema = new Schema({
name: String,
email: String,
password: String,
})
UserSchema.plugin(plugin({ defaultHidden: { password: true } }), { defaultHidden: {} })
let User = mongoose.model('User', UserSchema, undefined, { cache: false })
let user = new User(testUser)
user.save(function() {
let userJson = user.toJSON()
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
userJson.password.should.equal(testPassword)
done()
})
})
})
// Github issue https://github.com/mblarsen/mongoose-hidden/issues/11
describe("A model with password set as hidden as option", function () {
it("shouldn't return password nor __v", function (done) {
describe('A model with password set as hidden as option', function() {
it("shouldn't return password nor __v", function(done) {
let UserSchema = new Schema({
name: String,
email: String,
password: String
password: String,
})
UserSchema.plugin(plugin(), { hidden: { "password": true } })
UserSchema.plugin(plugin(), { hidden: { password: true } })
let User = mongoose.model('User', UserSchema, undefined, { cache: false })
let user = new User(testUser)
user.save(function () {
user.save(function() {
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.name.should.equal('Joe')
user[keyVersion].should.exist // eslint-disable-line
userJson.email.should.equal("joe@example.com")
should.not.exist(userJson["password"])
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson['password'])
should.not.exist(userJson[keyVersion])

@@ -491,5 +502,4 @@ done()

describe("A model with a virtuals defined", function () {
it("shouldn't return that property if option not passed", function (
done) {
describe('A model with a virtuals defined', function() {
it("shouldn't return that property if option not passed", function(done) {
let User = defineModel({

@@ -500,6 +510,6 @@ name: String,

type: String,
hide: true
}
hide: true,
},
})
User.schema.virtual('niceEmail').get(function () {
User.schema.virtual('niceEmail').get(function() {
return '"' + this.name + '" <' + this.email + '>'

@@ -509,4 +519,4 @@ })

let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.not.exist(userJson['niceEmail'])

@@ -517,3 +527,3 @@ should.not.exist(userJson.password)

it("should return that property if option is passed", function (done) {
it('should return that property if option is passed', function(done) {
let schema = new Schema({

@@ -524,11 +534,11 @@ name: String,

type: String,
hide: true
}
hide: true,
},
})
schema.set('toJSON', {
getters: true,
virtuals: true
virtuals: true,
})
schema.plugin(require('../index')())
schema.virtual('niceEmail').get(function () {
schema.virtual('niceEmail').get(function() {
return '"' + this.name + '" <' + this.email + '>'

@@ -540,4 +550,4 @@ })

let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.exist(userJson['niceEmail'])

@@ -550,7 +560,7 @@ userJson.niceEmail.should.equal('"Joe" <joe@example.com>')

// Github issue https://github.com/mblarsen/mongoose-hidden/issues/12
it("should return and hide nested virtuals", function (done) {
it('should return and hide nested virtuals', function(done) {
let schema = new Schema({
name: {
type: String,
hidden: false
hidden: false,
},

@@ -560,17 +570,17 @@ email: String,

bro: String,
baz: { aaa: String }
baz: { aaa: String },
},
password: {
type: String,
hide: true
}
hide: true,
},
})
schema.set('toJSON', {
getters: true,
virtuals: true
virtuals: true,
})
schema.virtual('fancyEmail').get(function () {
schema.virtual('fancyEmail').get(function() {
return '"' + this.name + '" <' + this.email + '>'
})
schema.virtual('nice.email').get(function () {
schema.virtual('nice.email').get(function() {
return '"' + this.name + '" <' + this.email + '>'

@@ -583,4 +593,4 @@ })

let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.exist(userJson.nice)

@@ -593,101 +603,101 @@ should.exist(userJson.nice.bro)

it("shouldn't return that property even if option is passed",
function (done) {
let schema = new Schema({
name: String,
email: String,
password: {
type: String,
hide: true
}
})
schema.set('toJSON', {
getters: true,
virtuals: true
})
schema.plugin(plugin(), {
virtuals: {
id: 'hide',
niceEmail: 'hide'
}
})
schema.virtual('niceEmail').get(function () {
return '"' + this.name + '" <' + this.email + '>'
})
let User = mongoose.model('VirtualUser2', schema)
let user = new User(testUser)
user.niceEmail.should.equal('"Joe" <joe@example.com>')
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
// should.not.exist(userJson['id'])
should.not.exist(userJson['niceEmail'])
should.not.exist(userJson.password)
done()
it("shouldn't return that property even if option is passed", function(done) {
let schema = new Schema({
name: String,
email: String,
password: {
type: String,
hide: true,
},
})
schema.set('toJSON', {
getters: true,
virtuals: true,
})
schema.plugin(plugin(), {
virtuals: {
id: 'hide',
niceEmail: 'hide',
},
})
schema.virtual('niceEmail').get(function() {
return '"' + this.name + '" <' + this.email + '>'
})
let User = mongoose.model('VirtualUser2', schema)
let user = new User(testUser)
user.niceEmail.should.equal('"Joe" <joe@example.com>')
let userJson = user.toJSON()
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
// should.not.exist(userJson['id'])
should.not.exist(userJson['niceEmail'])
should.not.exist(userJson.password)
done()
})
it(
"shouldn't return that property even if option is passed, unless object",
function (done) {
let schema = new Schema({
name: String,
email: String,
password: {
type: String,
hide: true
}
})
schema.set('toJSON', {
getters: true,
virtuals: true
})
schema.set('toObject', {
getters: true,
virtuals: true
})
schema.plugin(require('../index')(), {
virtuals: { niceEmail: 'hideObject' }
})
schema.virtual('niceEmail').get(function () {
return '"' + this.name + '" <' + this.email + '>'
})
let User = mongoose.model('VirtualUser3', schema)
let user = new User(testUser)
user.niceEmail.should.equal('"Joe" <joe@example.com>')
let userJson = user.toJSON()
userJson.name.should.equal("Joe")
userJson.email.should.equal("joe@example.com")
should.exist(userJson['niceEmail'])
userJson.niceEmail.should.equal('"Joe" <joe@example.com>')
should.not.exist(userJson.password)
it("shouldn't return that property even if option is passed, unless object", function(done) {
let schema = new Schema({
name: String,
email: String,
password: {
type: String,
hide: true,
},
})
schema.set('toJSON', {
getters: true,
virtuals: true,
})
schema.set('toObject', {
getters: true,
virtuals: true,
})
schema.plugin(require('../index')(), { virtuals: { niceEmail: 'hideObject' } })
schema.virtual('niceEmail').get(function() {
return '"' + this.name + '" <' + this.email + '>'
})
let User = mongoose.model('VirtualUser3', schema)
let user = new User(testUser)
user.niceEmail.should.equal('"Joe" <joe@example.com>')
let userJson = user.toJSON()
userJson.name.should.equal('Joe')
userJson.email.should.equal('joe@example.com')
should.exist(userJson['niceEmail'])
userJson.niceEmail.should.equal('"Joe" <joe@example.com>')
should.not.exist(userJson.password)
let userObject = user.toObject()
userObject.name.should.equal("Joe")
userObject.email.should.equal("joe@example.com")
should.not.exist(userObject['niceEmail'])
should.not.exist(userObject.password)
done()
})
let userObject = user.toObject()
userObject.name.should.equal('Joe')
userObject.email.should.equal('joe@example.com')
should.not.exist(userObject['niceEmail'])
should.not.exist(userObject.password)
done()
})
})
// Github issue https://github.com/mblarsen/mongoose-hidden/issues/1
describe("A document with nested documents when hiding", function () {
it("shouldn't remove its nested documents", function (done) {
describe('A document with nested documents when hiding', function() {
it("shouldn't remove its nested documents", function(done) {
mongoose.modelSchemas = {}
mongoose.models = {}
let Company = defineModel("Company", {
let Company = defineModel(
'Company',
{
name: String,
code: String,
},
{ hideObject: false },
{}
)
let User = defineModel('User', {
name: String,
code: String,
}, { hideObject: false }, {})
let User = defineModel("User", {
name: String,
email: String,
company: {
type: Schema.ObjectId,
ref: 'Company'
ref: 'Company',
},
password: {
type: String,
hide: true
}
hide: true,
},
})

@@ -697,16 +707,16 @@

let user = new User(testUser)
company.save(function (err, freshCompany) {
company.save(function(err, freshCompany) {
user.company = company._id
user.save(function () {
User.findOne().populate('company').exec(function (
err, freshUser) {
should.exist(freshUser.company)
freshUser.company.name.should.equal(
'GOGGLE')
let userJson = freshUser.toJSON()
should.not.exist(userJson.password)
should.exist(userJson.company)
should.equal("GOGGLE", userJson.company.name)
done()
})
user.save(function() {
User.findOne().
populate('company').
exec(function(err, freshUser) {
should.exist(freshUser.company)
freshUser.company.name.should.equal('GOGGLE')
let userJson = freshUser.toJSON()
should.not.exist(userJson.password)
should.exist(userJson.company)
should.equal('GOGGLE', userJson.company.name)
done()
})
})

@@ -717,23 +727,28 @@ })

describe("A document with a collection of nested documents", function () {
it("shouldn't remove its nested documents", function (done) {
describe('A document with a collection of nested documents', function() {
it("shouldn't remove its nested documents", function(done) {
mongoose.modelSchemas = {}
mongoose.models = {}
let Company = defineModel("Company", {
let Company = defineModel(
'Company',
{
name: String,
code: String,
},
{ hideObject: false },
{}
)
let User = defineModel('User', {
name: String,
code: String,
}, {
hideObject: false
}, {})
let User = defineModel("User", {
name: String,
email: String,
companies: [{
type: Schema.ObjectId,
ref: 'Company'
}],
companies: [
{
type: Schema.ObjectId,
ref: 'Company',
},
],
password: {
type: String,
hide: true
}
hide: true,
},
})

@@ -744,21 +759,18 @@

let user = new User(testUser)
company.save(function (err, freshCompany) {
company2.save(function (err, freshCompany2) {
company.save(function(err, freshCompany) {
company2.save(function(err, freshCompany2) {
user.companies.push(company)
user.companies.push(company2)
user.save(function () {
User.findOne().populate('companies').exec(
function (err, freshUser) {
user.save(function() {
User.findOne().
populate('companies').
exec(function(err, freshUser) {
should.exist(freshUser.companies)
freshUser.companies[0].name.should.equal(
'GOGGLE')
freshUser.companies[1].name.should.equal(
'APPLE')
freshUser.companies[0].name.should.equal('GOGGLE')
freshUser.companies[1].name.should.equal('APPLE')
let userJson = freshUser.toJSON()
should.not.exist(userJson.password)
should.exist(userJson.companies)
should.equal("GOGGLE", userJson.companies[
0].name)
should.equal("APPLE", userJson.companies[
1].name)
should.equal('GOGGLE', userJson.companies[0].name)
should.equal('APPLE', userJson.companies[1].name)
done()

@@ -772,4 +784,4 @@ })

describe("A model with a transform", function () {
it("should transform", function (done) {
describe('A model with a transform', function() {
it('should transform', function(done) {
let MrUserSchema = new Schema({

@@ -779,11 +791,11 @@ name: String,

type: String,
hide: true
}
hide: true,
},
})
MrUserSchema.set('toJSON', {
transform: function (doc, ret) {
transform: function(doc, ret) {
ret['name'] = 'Mr. ' + ret['name']
return ret
}
},
})

@@ -795,7 +807,7 @@

let userJson = user.toJSON()
userJson.name.should.equal("Mr. Joe")
userJson.name.should.equal('Mr. Joe')
userJson.password.should.equal(testPassword)
done()
})
it("should still transform after adding plugin", function (done) {
it('should still transform after adding plugin', function(done) {
let MrUserSchema = new Schema({

@@ -805,11 +817,11 @@ name: String,

type: String,
hide: true
}
hide: true,
},
})
MrUserSchema.set('toJSON', {
transform: function (doc, ret) {
transform: function(doc, ret) {
ret['name'] = 'Mr. ' + ret['name']
return ret
}
},
})

@@ -821,3 +833,3 @@

let userJson = user.toJSON()
userJson.name.should.equal("Mr. Joe")
userJson.name.should.equal('Mr. Joe')
should.not.exist(userJson.password)

@@ -829,4 +841,4 @@ done()

// Github issue https://github.com/mblarsen/mongoose-hidden/issues/3
describe("A model with other documents", function () {
it("should return the object property", function (done) {
describe('A model with other documents', function() {
it('should return the object property', function(done) {
let User = defineModel({

@@ -836,5 +848,5 @@ name: String,

prefix: String,
suffix: String
suffix: String,
},
password: String
password: String,
})

@@ -847,12 +859,15 @@ let user = new User(testUser3)

})
describe("A model with other documents partially hidden", function () {
it("should return the object property", function (done) {
let User = defineModel({
name: String,
email: {
prefix: String,
suffix: String
describe('A model with other documents partially hidden', function() {
it('should return the object property', function(done) {
let User = defineModel(
{
name: String,
email: {
prefix: String,
suffix: String,
},
password: String,
},
password: String
}, { hidden: { 'email.suffix': true } })
{ hidden: { 'email.suffix': true } }
)
let user = new User(testUser3)

@@ -868,18 +883,21 @@ let userJson = user.toObject()

// Github issue https://github.com/mblarsen/mongoose-hidden/issues/13
describe("A documents with non-schema properties set to hidden", function () {
it("should hide the properties", function (done) {
let User = defineModel({
name: String,
email: String,
password: String
}, { hidden: { email: true } })
describe('A documents with non-schema properties set to hidden', function() {
it('should hide the properties', function(done) {
let User = defineModel(
{
name: String,
email: String,
password: String,
},
{ hidden: { email: true } }
)
let user = new User(testUser)
user.save(function (err, savedUser) {
user.save(function(err, savedUser) {
User.schema.remove('email')
let User2 = mongoose.model('User', User.schema, undefined, { cache: false })
User2.findById(savedUser['_id'], function (err2, john) {
User2.findById(savedUser['_id'], function(err2, john) {
let userJson = john.toObject()
let testUserWithoutEmail = {
name: testUser.name,
password: testUser.password
password: testUser.password,
}

@@ -894,4 +912,4 @@ userJson.should.deepEqual(testUserWithoutEmail)

// Github issue https://github.com/mblarsen/mongoose-hidden/issues/12
describe("A model with nested documents", function () {
it("should return only the visible parts", function (done) {
describe('A model with nested documents', function() {
it('should return only the visible parts', function(done) {
let User = defineModel({

@@ -901,5 +919,5 @@ name: String,

prefix: { type: String },
suffix: { type: String, hide: true }
suffix: { type: String, hide: true },
},
password: String
password: String,
})

@@ -915,35 +933,33 @@ let user = new User(testUser3)

describe("Setting a path on an object", function () {
it("should set plain property", function () {
let obj = { password: "secret", }
setPath(obj, "password", "no more secrets")
obj.password.should.equal("no more secrets")
describe('Setting a path on an object', function() {
it('should set plain property', function() {
let obj = { password: 'secret' }
setPath(obj, 'password', 'no more secrets')
obj.password.should.equal('no more secrets')
})
it("should set plain property and create if it doesn't exist",
function () {
let obj = {}
setPath(obj, "password", "no more secrets")
obj.password.should.equal("no more secrets")
})
it("should set nested property", function () {
let obj = { name: { first: "Joe" } }
setPath(obj, "name.first", "Jane")
obj.name.first.should.equal("Jane")
setPath(obj, "name.last", "Doe")
obj.name.last.should.equal("Doe")
it("should set plain property and create if it doesn't exist", function() {
let obj = {}
setPath(obj, 'password', 'no more secrets')
obj.password.should.equal('no more secrets')
})
it("should set nested property and create path if it doesn't exist",
function () {
let obj = {}
setPath(obj, "name.first", "Jane")
obj.name.first.should.equal("Jane")
setPath(obj, "rights.inland.case", "A0003")
obj.rights.inland.case.should.equal("A0003")
setPath(obj, "rights.outlandish.case", "A0004")
obj.rights.outlandish.case.should.equal("A0004")
})
it('should set nested property', function() {
let obj = { name: { first: 'Joe' } }
setPath(obj, 'name.first', 'Jane')
obj.name.first.should.equal('Jane')
setPath(obj, 'name.last', 'Doe')
obj.name.last.should.equal('Doe')
})
it("should set nested property and create path if it doesn't exist", function() {
let obj = {}
setPath(obj, 'name.first', 'Jane')
obj.name.first.should.equal('Jane')
setPath(obj, 'rights.inland.case', 'A0003')
obj.rights.inland.case.should.equal('A0003')
setPath(obj, 'rights.outlandish.case', 'A0004')
obj.rights.outlandish.case.should.equal('A0004')
})
})
describe('Model with embedded schema', function () {
it('should return all properties', function (done) {
describe('Model with embedded schema', function() {
it('should return all properties', function(done) {
let User = defineModel({

@@ -953,3 +969,3 @@ name: String,

password: String,
spouse: new Schema({ name: { type: String } })
spouse: new Schema({ name: { type: String } }),
})

@@ -965,3 +981,3 @@ let user = new User(testUserSub)

it('shouldn\'t return those property', function (done) {
it("shouldn't return those property", function(done) {
let User = defineModel({

@@ -973,4 +989,4 @@ name: String,

name: { type: String, hide: true },
age: { type: Number }
})
age: { type: Number },
}),
})

@@ -977,0 +993,0 @@ let user = new User(testUserSub2)