Skip to content

Commit

Permalink
Add possibility to create empty subsets
Browse files Browse the repository at this point in the history
This feature is necessary because it might be possible to create schemas that have only readable fields. In this case, schema.writableFields() should just work without throwing an error.
  • Loading branch information
jhnns committed Jun 16, 2016
1 parent a8d8874 commit f6a4a02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
8 changes: 0 additions & 8 deletions lib/Schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ Schema.prototype.only = function (key1, key2, key3) {
fields = slice.call(arguments);
}

if (fields.length === 0) {
throw new Error("Cannot create a subset of the schema with no keys");
}

subset.fields = fields;

return subset;
Expand All @@ -79,10 +75,6 @@ Schema.prototype.except = function (key1, key2, key3) {
fields = slice.call(arguments);
}

if (fields.length === this.fields.length) {
throw new Error("Cannot create a subset of the schema with no keys");
}

subset.fields = this.fields.filter(function (value) {
return fields.indexOf(value) === -1;
});
Expand Down
28 changes: 12 additions & 16 deletions test/Schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,9 @@ describe("Schema", function () {
expect(subset.fields).to.eql(["name", "age"]);
});

it("should throw an error if no keys are given", function () {
expect(function () {
schema.only();
}).to.throw("Cannot create a subset of the schema with no keys");
it("should be possible to create schemas with no fields", function () {
subset = schema.only();
expect(subset.fields).to.eql([]);
});

});
Expand All @@ -156,10 +155,9 @@ describe("Schema", function () {
expect(subset.fields).to.eql(["name", "age"]);
});

it("should throw an error if no keys are given", function () {
expect(function () {
schema.only([]);
}).to.throw("Cannot create a subset of the schema with no keys");
it("should be possible to create schemas with no fields", function () {
subset = schema.only([]);
expect(subset.fields).to.eql([]);
});

});
Expand Down Expand Up @@ -192,10 +190,9 @@ describe("Schema", function () {
expect(subset.fields).to.eql(["name"]);
});

it("should throw an error if no keys are given", function () {
expect(function () {
schema.except.apply(schema, schema.fields);
}).to.throw("Cannot create a subset of the schema with no keys");
it("should be possible to exclude all fields", function () {
subset = schema.except.apply(schema, schema.fields);
expect(subset.fields).to.eql([]);
});

});
Expand All @@ -208,10 +205,9 @@ describe("Schema", function () {
expect(subset.fields).to.eql(["name", "age"]);
});

it("should throw an error if no keys are given", function () {
expect(function () {
schema.except(schema.fields);
}).to.throw("Cannot create a subset of the schema with no keys");
it("should be possible to exclude all fields", function () {
subset = schema.except(schema.fields);
expect(subset.fields).to.eql([]);
});

});
Expand Down

0 comments on commit f6a4a02

Please sign in to comment.