|
|
@@ -19,11 +19,11 @@ describe('projectData', function () {
|
|
|
throwable(undefined)();
|
|
|
});
|
|
|
|
|
|
- it('should require the "strict" option to be a boolean', function () {
|
|
|
- const throwable = v => () => projectData(10, {}, {strict: v});
|
|
|
+ it('should require the "keepUnknown" option to be a boolean', function () {
|
|
|
+ const throwable = v => () => projectData(10, {}, {keepUnknown: v});
|
|
|
const error = s =>
|
|
|
format(
|
|
|
- 'Projection option "strict" must be a Boolean, but %s was given.',
|
|
|
+ 'Projection option "keepUnknown" must be a Boolean, but %s was given.',
|
|
|
s,
|
|
|
);
|
|
|
expect(throwable('str')).to.throw(error('"str"'));
|
|
|
@@ -350,57 +350,118 @@ describe('projectData', function () {
|
|
|
expect(projectData(null, {})).to.be.eq(null);
|
|
|
});
|
|
|
|
|
|
- it('should project an array items', function () {
|
|
|
- const list = [{foo: 10, bar: 20, baz: 30}, {qux: 30}];
|
|
|
- const expectedList = [{foo: 10, baz: 30}, {qux: 30}];
|
|
|
- const res = projectData(list, {foo: true, bar: false});
|
|
|
- expect(res).to.be.eql(expectedList);
|
|
|
+ it('should include a property defined with an empty object', function () {
|
|
|
+ const schema = {foo: {}, bar: {}};
|
|
|
+ const data = {foo: 10, baz: 30};
|
|
|
+ const res = projectData(data, schema);
|
|
|
+ expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should project an array items in the strict mode', function () {
|
|
|
- const list = [{foo: 10, bar: 20, baz: 30}, {qux: 30}];
|
|
|
- const expectedList = [{foo: 10}, {}];
|
|
|
- const res = projectData(list, {foo: true, bar: false}, {strict: true});
|
|
|
- expect(res).to.be.eql(expectedList);
|
|
|
+ it('should exclude a property when the logical rule is false', function () {
|
|
|
+ const schema = {foo: false, bar: false};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema);
|
|
|
+ expect(res).to.be.eql({});
|
|
|
});
|
|
|
|
|
|
- it('should exclude unknown properties when the strict mode is enabled', function () {
|
|
|
- const res = projectData(
|
|
|
- {foo: 10, bar: 20, baz: 30},
|
|
|
- {foo: true, bar: false},
|
|
|
- {strict: true},
|
|
|
- );
|
|
|
- expect(res).to.be.eql({foo: 10});
|
|
|
+ it('should include a property when the logical rule is true', function () {
|
|
|
+ const schema = {foo: true, bar: true};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema);
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
});
|
|
|
|
|
|
- it('should include a property defined with an empty object in the strict mode', function () {
|
|
|
- const schema = {foo: {}, bar: {}};
|
|
|
- const data = {foo: 10, baz: 30};
|
|
|
- const res = projectData(data, schema, {strict: true});
|
|
|
+ it('should exclude a property when the "select" option is false', function () {
|
|
|
+ const schema = {foo: true, bar: {select: false}};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema);
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should include a property with a nested schema in the strict mode', function () {
|
|
|
+ it('should include a property when the "select" option is true', function () {
|
|
|
+ const schema = {foo: true, bar: {select: true}};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema);
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should include a property with a nested schema', function () {
|
|
|
const schema = {user: {schema: {id: true, name: false}}};
|
|
|
- const data = {user: {id: 1, name: 'John Doe'}, timestamp: 12345};
|
|
|
- const res = projectData(data, schema, {strict: true});
|
|
|
+ const data = {user: {id: 1, name: 'John Doe'}};
|
|
|
+ const res = projectData(data, schema);
|
|
|
expect(res).to.be.eql({user: {id: 1}});
|
|
|
});
|
|
|
|
|
|
- it('should exclude a property when the "select" option is false in the strict mode', function () {
|
|
|
- const schema = {foo: {}, bar: {select: false}};
|
|
|
+ it('should exclude properties not defined in a given schema', function () {
|
|
|
+ const res = projectData({foo: 10, bar: 20}, {});
|
|
|
+ expect(res).to.be.eql({});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should project an array items by a given schema', function () {
|
|
|
+ const list = [
|
|
|
+ {foo: 10, bar: 20, baz: 30},
|
|
|
+ {bar: 20, qux: 30},
|
|
|
+ ];
|
|
|
+ const expectedList = [{foo: 10}, {}];
|
|
|
+ const res = projectData(list, {foo: true, bar: false});
|
|
|
+ expect(res).to.be.eql(expectedList);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should include a property defined with an empty object in the keep unknown mode', function () {
|
|
|
+ const schema = {foo: {}, bar: {}};
|
|
|
const data = {foo: 10, bar: 20};
|
|
|
- const res = projectData(data, schema, {strict: true});
|
|
|
+ const res = projectData(data, schema, {keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should exclude a property when the logical rule is false in the keep unknown mode', function () {
|
|
|
+ const schema = {foo: false, bar: false};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema, {keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should include a property when the logical rule is true in the keep unknown mode', function () {
|
|
|
+ const schema = {foo: true, bar: true};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema, {keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should exclude a property when the "select" option is false in the keep unknown mode', function () {
|
|
|
+ const schema = {foo: true, bar: {select: false}};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema, {keepUnknown: true});
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should include a property when the "select" option is true in the strict mode', function () {
|
|
|
- const schema = {foo: {}, bar: {select: true}};
|
|
|
+ it('should include a property when the "select" option is true in the keep unknown mode', function () {
|
|
|
+ const schema = {foo: true, bar: {select: true}};
|
|
|
+ const data = {foo: 10, bar: 20};
|
|
|
+ const res = projectData(data, schema, {keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should include a property with a nested schema in the keep unknown mode', function () {
|
|
|
+ const schema = {user: {schema: {id: true, name: false}}};
|
|
|
+ const data = {user: {id: 1, name: 'John Doe'}};
|
|
|
+ const res = projectData(data, schema, {keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({user: {id: 1}});
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should include unknown properties in the keep unknown mode', function () {
|
|
|
const data = {foo: 10, bar: 20};
|
|
|
- const res = projectData(data, schema, {strict: true});
|
|
|
+ const res = projectData(data, {}, {keepUnknown: true});
|
|
|
expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
});
|
|
|
|
|
|
+ it('should project an array items in the keep unknown mode', function () {
|
|
|
+ const list = [{foo: 10, bar: 20, baz: 30}, {qux: 30}];
|
|
|
+ const expectedList = [{foo: 10, baz: 30}, {qux: 30}];
|
|
|
+ const res = projectData(list, {foo: true, bar: false}, {keepUnknown: true});
|
|
|
+ expect(res).to.be.eql(expectedList);
|
|
|
+ });
|
|
|
+
|
|
|
it('should ignore prototype properties', function () {
|
|
|
const data = Object.create({baz: 30});
|
|
|
data.foo = 10;
|
|
|
@@ -410,19 +471,6 @@ describe('projectData', function () {
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should project the property by a boolean rule', function () {
|
|
|
- const res = projectData({foo: 10, bar: 20}, {foo: true, bar: false});
|
|
|
- expect(res).to.be.eql({foo: 10});
|
|
|
- });
|
|
|
-
|
|
|
- it('should project the property by the select option', function () {
|
|
|
- const res = projectData(
|
|
|
- {foo: 10, bar: 20},
|
|
|
- {foo: {select: true}, bar: {select: false}},
|
|
|
- );
|
|
|
- expect(res).to.be.eql({foo: 10});
|
|
|
- });
|
|
|
-
|
|
|
it('should ignore scope options when no active scope is provided', function () {
|
|
|
const schema = {
|
|
|
foo: {select: true, scopes: {input: false}},
|
|
|
@@ -432,7 +480,7 @@ describe('projectData', function () {
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should project the active scope by the boolean rule', function () {
|
|
|
+ it('should project the active scope by a boolean rule', function () {
|
|
|
const schema = {
|
|
|
foo: {scopes: {input: true}},
|
|
|
bar: {scopes: {input: false}},
|
|
|
@@ -450,7 +498,7 @@ describe('projectData', function () {
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should prioritize the scope rule over the general options', function () {
|
|
|
+ it('should prioritize the scope rule over common rules', function () {
|
|
|
const schema = {
|
|
|
foo: {select: false, scopes: {input: true}},
|
|
|
bar: {select: true, scopes: {input: false}},
|
|
|
@@ -468,34 +516,31 @@ describe('projectData', function () {
|
|
|
expect(res).to.be.eql({foo: 10});
|
|
|
});
|
|
|
|
|
|
- it('should include a property in the strict mode if no rule for the active scope is specified', function () {
|
|
|
+ it('should include a property in the keep unknown mode if no rule for the active scope is specified', function () {
|
|
|
const schema = {
|
|
|
foo: {scopes: {input: true}},
|
|
|
bar: {scopes: {input: false}},
|
|
|
baz: {scopes: {output: true}},
|
|
|
};
|
|
|
const data = {foo: 10, bar: 20, baz: 30, qux: 40};
|
|
|
- const res = projectData(data, schema, {strict: true, scope: 'input'});
|
|
|
- expect(res).to.be.eql({foo: 10, baz: 30});
|
|
|
+ const res = projectData(data, schema, {scope: 'input', keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({foo: 10, baz: 30, qux: 40});
|
|
|
});
|
|
|
|
|
|
- it('should prioritize the scope options over the general options in the strict mode', function () {
|
|
|
+ it('should prioritize scope options over common options in the keep unknown mode', function () {
|
|
|
const schema = {
|
|
|
foo: {select: false, scopes: {input: true}},
|
|
|
bar: {select: false, scopes: {input: {select: true}}},
|
|
|
};
|
|
|
const data = {foo: 10, bar: 20, baz: 30};
|
|
|
- const res = projectData(data, schema, {strict: true, scope: 'input'});
|
|
|
- expect(res).to.be.eql({foo: 10, bar: 20});
|
|
|
+ const res = projectData(data, schema, {scope: 'input', keepUnknown: true});
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: 20, baz: 30});
|
|
|
});
|
|
|
|
|
|
- it('should project the nested object by the given schema', function () {
|
|
|
- const schema = {
|
|
|
- foo: true,
|
|
|
- bar: {schema: {baz: true, qux: false}},
|
|
|
- };
|
|
|
+ it('should project a nested object by a given schema', function () {
|
|
|
+ const schema = {foo: true, bar: {schema: {baz: true, qux: false}}};
|
|
|
const data = {foo: 10, bar: {baz: 20, qux: 30, buz: 40}};
|
|
|
const res = projectData(data, schema, {scope: 'input'});
|
|
|
- expect(res).to.be.eql({foo: 10, bar: {baz: 20, buz: 40}});
|
|
|
+ expect(res).to.be.eql({foo: 10, bar: {baz: 20}});
|
|
|
});
|
|
|
});
|