|
|
@@ -184,8 +184,11 @@ describe('projectData', function () {
|
|
|
const throwable = v => () =>
|
|
|
projectData({}, 'mySchema', {nameResolver: () => v});
|
|
|
const error = s =>
|
|
|
- format('Name resolver must return an Object, but %s was given.', s);
|
|
|
- expect(throwable('str')).to.throw(error('"str"'));
|
|
|
+ format(
|
|
|
+ 'Name resolver must return an Object, a Function ' +
|
|
|
+ 'or a non-empty String, but %s was given.',
|
|
|
+ s,
|
|
|
+ );
|
|
|
expect(throwable('')).to.throw(error('""'));
|
|
|
expect(throwable(10)).to.throw(error('10'));
|
|
|
expect(throwable(0)).to.throw(error('0'));
|
|
|
@@ -194,7 +197,6 @@ describe('projectData', function () {
|
|
|
expect(throwable([])).to.throw(error('Array'));
|
|
|
expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
expect(throwable(null)).to.throw(error('null'));
|
|
|
- expect(throwable(() => undefined)).to.throw(error('Function'));
|
|
|
throwable({})();
|
|
|
});
|
|
|
|
|
|
@@ -228,6 +230,102 @@ describe('projectData', function () {
|
|
|
expect(invoked).to.be.eq(2);
|
|
|
});
|
|
|
|
|
|
+ it('should resolve the schema object through the name chain', function () {
|
|
|
+ let invoked = 0;
|
|
|
+ const nameResolver = name => {
|
|
|
+ invoked++;
|
|
|
+ if (name === 'schema1') {
|
|
|
+ return 'schema2';
|
|
|
+ } else if (name === 'schema2') {
|
|
|
+ return {foo: true, bar: false};
|
|
|
+ }
|
|
|
+ throw new Error('Invalid argument.');
|
|
|
+ };
|
|
|
+ const res = projectData({foo: 10, bar: 20}, 'schema1', {nameResolver});
|
|
|
+ expect(res).to.be.eql({foo: 10});
|
|
|
+ expect(invoked).to.be.eq(2);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should validate a resolved value from the name chain', function () {
|
|
|
+ const nameResolver = v => name => {
|
|
|
+ if (name === 'schema1') {
|
|
|
+ return 'schema2';
|
|
|
+ } else if (name === 'schema2') {
|
|
|
+ return v;
|
|
|
+ } else if (name === 'schema3') {
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ throw new Error('Invalid argument.');
|
|
|
+ };
|
|
|
+ const throwable = v => () =>
|
|
|
+ projectData({foo: 10, bar: 20}, 'schema1', {
|
|
|
+ nameResolver: nameResolver(v),
|
|
|
+ });
|
|
|
+ const error = s =>
|
|
|
+ format(
|
|
|
+ 'Name resolver must return an Object, a Function ' +
|
|
|
+ 'or a non-empty String, but %s was given.',
|
|
|
+ s,
|
|
|
+ );
|
|
|
+ expect(throwable('')).to.throw(error('""'));
|
|
|
+ expect(throwable(10)).to.throw(error('10'));
|
|
|
+ expect(throwable(0)).to.throw(error('0'));
|
|
|
+ expect(throwable(true)).to.throw(error('true'));
|
|
|
+ expect(throwable(false)).to.throw(error('false'));
|
|
|
+ expect(throwable([])).to.throw(error('Array'));
|
|
|
+ expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
+ expect(throwable(null)).to.throw(error('null'));
|
|
|
+ throwable('schema3')();
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should resolve schema names through the factory function', function () {
|
|
|
+ let invoked = 0;
|
|
|
+ const nameResolver = name => {
|
|
|
+ invoked++;
|
|
|
+ if (name === 'schema1') {
|
|
|
+ return () => 'schema2';
|
|
|
+ } else if (name === 'schema2') {
|
|
|
+ return {foo: true, bar: false};
|
|
|
+ }
|
|
|
+ throw new Error('Invalid argument.');
|
|
|
+ };
|
|
|
+ const res = projectData({foo: 10, bar: 20}, 'schema1', {nameResolver});
|
|
|
+ expect(res).to.be.eql({foo: 10});
|
|
|
+ expect(invoked).to.be.eq(2);
|
|
|
+ });
|
|
|
+
|
|
|
+ it('should validate a return value of the factory resolved through the name chain', function () {
|
|
|
+ const nameResolver = v => name => {
|
|
|
+ if (name === 'schema1') {
|
|
|
+ return 'schema2';
|
|
|
+ } else if (name === 'schema2') {
|
|
|
+ return () => v;
|
|
|
+ } else if (name === 'schema3') {
|
|
|
+ return {};
|
|
|
+ }
|
|
|
+ throw new Error('Invalid argument.');
|
|
|
+ };
|
|
|
+ const throwable = v => () =>
|
|
|
+ projectData({foo: 10, bar: 20}, 'schema1', {
|
|
|
+ nameResolver: nameResolver(v),
|
|
|
+ });
|
|
|
+ const error = s =>
|
|
|
+ format(
|
|
|
+ 'Schema factory must return an Object ' +
|
|
|
+ 'or a non-empty String, but %s was given.',
|
|
|
+ s,
|
|
|
+ );
|
|
|
+ expect(throwable('')).to.throw(error('""'));
|
|
|
+ expect(throwable(10)).to.throw(error('10'));
|
|
|
+ expect(throwable(0)).to.throw(error('0'));
|
|
|
+ expect(throwable(true)).to.throw(error('true'));
|
|
|
+ expect(throwable(false)).to.throw(error('false'));
|
|
|
+ expect(throwable([])).to.throw(error('Array'));
|
|
|
+ expect(throwable(undefined)).to.throw(error('undefined'));
|
|
|
+ expect(throwable(null)).to.throw(error('null'));
|
|
|
+ throwable('schema3')();
|
|
|
+ });
|
|
|
+
|
|
|
it('should validate the given schema in the shallow mode', function () {
|
|
|
const schema1 = {foo: '?'};
|
|
|
const schema2 = {foo: true, bar: {schema: {baz: '?'}}};
|