Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added Schema.prototype.omit() function #12939

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,48 @@ Schema.prototype.pick = function(paths, options) {
return newSchema;
};

/**
* Returns a new schema that has the `paths` from the original schema, minus the omitted ones.
*
* This method is analagous to [Lodash's `omit()` function](https://lodash.com/docs/#omit) for Mongoose schemas.
*
* #### Example:
*
* const schema = Schema({ name: String, age: Number });
* // Creates a new schema omitting the `age` path
* const newSchema = schema.omit(['age']);
*
* newSchema.path('name'); // SchemaString { ... }
* newSchema.path('age'); // undefined
*
* @param {String[]} paths List of Paths to omit for the new Schema
* @param {Object} [options] Options to pass to the new Schema Constructor (same as `new Schema(.., Options)`). Defaults to `this.options` if not set.
* @return {Schema}
* @api public
*/

Schema.prototype.omit = function(paths, options) {
const newSchema = new Schema(this, options || this.options);
if (!Array.isArray(paths)) {
throw new MongooseError(
'Schema#omit() only accepts an array argument, ' +
'got "' +
typeof paths +
'"'
);
}

newSchema.remove(paths);

for (const nested in newSchema.singleNestedPaths) {
if (paths.includes(nested)) {
delete newSchema.singleNestedPaths[nested];
}
}

return newSchema;
};

/**
* Returns default options for this schema, merged with `options`.
*
Expand Down
75 changes: 75 additions & 0 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,81 @@ describe('schema', function() {
});
});

describe('omit() (gh-12931)', function() {
it('works with nested paths', function() {
const schema = Schema({
name: {
first: {
type: String,
required: true
},
last: {
type: String,
required: true
}
},
age: {
type: Number,
index: true
}
});
assert.ok(schema.path('name.first'));
assert.ok(schema.path('name.last'));

let newSchema = schema.omit(['name.first']);
assert.ok(!newSchema.path('name.first'));
assert.ok(newSchema.path('age'));
assert.ok(newSchema.path('age').index);

newSchema = schema.omit(['age']);
assert.ok(newSchema.path('name.first'));
assert.ok(newSchema.path('name.first').required);
assert.ok(newSchema.path('name.last'));
assert.ok(newSchema.path('name.last').required);
assert.ok(!newSchema.path('age'));

newSchema = schema.omit(['name.last', 'age']);
assert.ok(newSchema.path('name.first'));
assert.ok(newSchema.path('name.first').required);
assert.ok(!newSchema.path('name.last'));
assert.ok(!newSchema.path('age'));
});

it('with single nested paths', function() {
const schema = Schema({
name: Schema({
first: {
type: String,
required: true
},
last: {
type: String,
required: true
}
}),
age: {
type: Number,
index: true
}
});
assert.ok(schema.path('name.first'));
assert.ok(schema.path('name.last'));

let newSchema = schema.omit(['age']);
assert.ok(newSchema.path('name.first'));
assert.ok(newSchema.path('name.first').required);
assert.ok(newSchema.path('name.last'));
assert.ok(newSchema.path('name.last').required);
assert.ok(!newSchema.path('age'));

newSchema = schema.omit(['name.last', 'age']);
assert.ok(newSchema.path('name.first'));
assert.ok(newSchema.path('name.first').required);
assert.ok(!newSchema.path('name.last'));
assert.ok(!newSchema.path('age'));
});
});

describe('path-level custom cast (gh-8300)', function() {
it('with numbers', function() {
const schema = Schema({
Expand Down