validate-projection-schema.spec.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import {expect} from 'chai';
  2. import {format} from '@e22m4u/js-format';
  3. import {validateProjectionSchema} from './validate-projection-schema.js';
  4. describe('validateProjectionSchema', function () {
  5. it('should require the schema argument to be a object', function () {
  6. const throwable = v => () => validateProjectionSchema(v);
  7. const error = s =>
  8. format('Projection schema must be an Object, but %s was given.', s);
  9. expect(throwable('str')).to.throw(error('"str"'));
  10. expect(throwable('')).to.throw(error('""'));
  11. expect(throwable(10)).to.throw(error('10'));
  12. expect(throwable(0)).to.throw(error('0'));
  13. expect(throwable(true)).to.throw(error('true'));
  14. expect(throwable(false)).to.throw(error('false'));
  15. expect(throwable([])).to.throw(error('Array'));
  16. expect(throwable(null)).to.throw(error('null'));
  17. expect(throwable(undefined)).to.throw(error('undefined'));
  18. throwable({})();
  19. });
  20. it('should require the shallowMode parameter to be a boolean', function () {
  21. const throwable = v => () => validateProjectionSchema({}, v);
  22. const error = s =>
  23. format('Parameter "shallowMode" must be a Boolean, but %s was given.', s);
  24. expect(throwable('str')).to.throw(error('"str"'));
  25. expect(throwable('')).to.throw(error('""'));
  26. expect(throwable(10)).to.throw(error('10'));
  27. expect(throwable(0)).to.throw(error('0'));
  28. expect(throwable([])).to.throw(error('Array'));
  29. expect(throwable({})).to.throw(error('Object'));
  30. expect(throwable(null)).to.throw(error('null'));
  31. throwable(true)();
  32. throwable(false)();
  33. throwable(undefined)();
  34. });
  35. it('should require schema properties to be a boolean or an object', function () {
  36. const throwable = v => () => validateProjectionSchema({foo: v});
  37. const error = s =>
  38. format(
  39. 'Property options must be a Boolean or an Object, but %s was given.',
  40. s,
  41. );
  42. expect(throwable('str')).to.throw(error('"str"'));
  43. expect(throwable('')).to.throw(error('""'));
  44. expect(throwable(10)).to.throw(error('10'));
  45. expect(throwable(0)).to.throw(error('0'));
  46. expect(throwable([])).to.throw(error('Array'));
  47. expect(throwable(null)).to.throw(error('null'));
  48. throwable({})();
  49. throwable(true)();
  50. throwable(false)();
  51. throwable(undefined)();
  52. });
  53. it('should require the property option "select" to be a boolean', function () {
  54. const throwable = v => () => validateProjectionSchema({foo: {select: v}});
  55. const error = s =>
  56. format(
  57. 'Property option "select" must be a Boolean, but %s was given.',
  58. s,
  59. );
  60. expect(throwable('str')).to.throw(error('"str"'));
  61. expect(throwable('')).to.throw(error('""'));
  62. expect(throwable(10)).to.throw(error('10'));
  63. expect(throwable(0)).to.throw(error('0'));
  64. expect(throwable({})).to.throw(error('Object'));
  65. expect(throwable([])).to.throw(error('Array'));
  66. expect(throwable(null)).to.throw(error('null'));
  67. throwable(true)();
  68. throwable(false)();
  69. throwable(undefined)();
  70. });
  71. it('should require the property option "schema" to be an object or a non-empty string', function () {
  72. const throwable = v => () => validateProjectionSchema({foo: {schema: v}});
  73. const error = s =>
  74. format(
  75. 'Embedded schema must be an Object or a non-empty String ' +
  76. 'that represents a schema name, but %s was given.',
  77. s,
  78. );
  79. expect(throwable('')).to.throw(error('""'));
  80. expect(throwable(10)).to.throw(error('10'));
  81. expect(throwable(0)).to.throw(error('0'));
  82. expect(throwable(true)).to.throw(error('true'));
  83. expect(throwable(false)).to.throw(error('false'));
  84. expect(throwable([])).to.throw(error('Array'));
  85. expect(throwable(null)).to.throw(error('null'));
  86. throwable('str')();
  87. throwable({})();
  88. throwable(undefined)();
  89. });
  90. it('should require the property option "scopes" to be an object', function () {
  91. const throwable = v => () => validateProjectionSchema({foo: {scopes: v}});
  92. const error = s =>
  93. format(
  94. 'Property option "scopes" must be an Object, but %s was given.',
  95. s,
  96. );
  97. expect(throwable('str')).to.throw(error('"str"'));
  98. expect(throwable('')).to.throw(error('""'));
  99. expect(throwable(10)).to.throw(error('10'));
  100. expect(throwable(0)).to.throw(error('0'));
  101. expect(throwable(true)).to.throw(error('true'));
  102. expect(throwable(false)).to.throw(error('false'));
  103. expect(throwable([])).to.throw(error('Array'));
  104. expect(throwable(null)).to.throw(error('null'));
  105. throwable({})();
  106. throwable(undefined)();
  107. });
  108. it('should require scope options to be a boolean or an object', function () {
  109. const throwable = v => () =>
  110. validateProjectionSchema({foo: {scopes: {input: v}}});
  111. const error = s =>
  112. format(
  113. 'Scope options must be a Boolean or an Object, but %s was given.',
  114. s,
  115. );
  116. expect(throwable('str')).to.throw(error('"str"'));
  117. expect(throwable('')).to.throw(error('""'));
  118. expect(throwable(10)).to.throw(error('10'));
  119. expect(throwable(0)).to.throw(error('0'));
  120. expect(throwable([])).to.throw(error('Array'));
  121. expect(throwable(null)).to.throw(error('null'));
  122. throwable({})();
  123. throwable(true)();
  124. throwable(false)();
  125. throwable(undefined)();
  126. });
  127. it('should require the scope option "select" to be a boolean', function () {
  128. const throwable = v => () =>
  129. validateProjectionSchema({foo: {scopes: {input: {select: v}}}});
  130. const error = s =>
  131. format('Scope option "select" must be a Boolean, but %s was given.', s);
  132. expect(throwable('str')).to.throw(error('"str"'));
  133. expect(throwable('')).to.throw(error('""'));
  134. expect(throwable(10)).to.throw(error('10'));
  135. expect(throwable(0)).to.throw(error('0'));
  136. expect(throwable([])).to.throw(error('Array'));
  137. expect(throwable({})).to.throw(error('Object'));
  138. expect(throwable(null)).to.throw(error('null'));
  139. throwable(true)();
  140. throwable(false)();
  141. throwable(undefined)();
  142. });
  143. it('should allow nested schema', function () {
  144. validateProjectionSchema({
  145. foo: {
  146. select: true,
  147. schema: {
  148. bar: {
  149. select: true,
  150. },
  151. },
  152. },
  153. baz: {
  154. select: false,
  155. schema: {
  156. qux: {
  157. select: false,
  158. },
  159. },
  160. },
  161. });
  162. });
  163. it('should allow a projection name in the "schema" option', function () {
  164. validateProjectionSchema({foo: {schema: 'mySchema'}});
  165. validateProjectionSchema({foo: {schema: {bar: {schema: 'mySchema'}}}});
  166. });
  167. it('should validate root schema in shallow mode', function () {
  168. const throwable = () => validateProjectionSchema({foo: 10}, true);
  169. expect(throwable).to.throw(
  170. 'Property options must be a Boolean or an Object, but 10 was given.',
  171. );
  172. });
  173. it('should skip nested schema checking in shallow mode', function () {
  174. validateProjectionSchema({foo: {schema: {prop: 10}}}, true);
  175. });
  176. });