Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

object-schema.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /**
  2. * @filedescription Object Schema Tests
  3. */
  4. /* global it, describe, beforeEach */
  5. "use strict";
  6. //-----------------------------------------------------------------------------
  7. // Requirements
  8. //-----------------------------------------------------------------------------
  9. const assert = require("chai").assert;
  10. const { ObjectSchema } = require("../src/");
  11. //-----------------------------------------------------------------------------
  12. // Class
  13. //-----------------------------------------------------------------------------
  14. describe("ObjectSchema", () => {
  15. let schema;
  16. describe("new ObjectSchema()", () => {
  17. it("should add a new key when a strategy is passed", () => {
  18. schema = new ObjectSchema({
  19. foo: {
  20. merge() {},
  21. validate() {}
  22. }
  23. });
  24. assert.isTrue(schema.hasKey("foo"));
  25. });
  26. it("should throw an error when a strategy is missing a merge() method", () => {
  27. assert.throws(() => {
  28. schema = new ObjectSchema({
  29. foo: {
  30. validate() { }
  31. }
  32. });
  33. }, /Definition for key "foo" must have a merge property/);
  34. });
  35. it("should throw an error when a strategy is missing a merge() method", () => {
  36. assert.throws(() => {
  37. schema = new ObjectSchema();
  38. }, /Schema definitions missing/);
  39. });
  40. it("should throw an error when a strategy is missing a validate() method", () => {
  41. assert.throws(() => {
  42. schema = new ObjectSchema({
  43. foo: {
  44. merge() { },
  45. }
  46. });
  47. }, /Definition for key "foo" must have a validate\(\) method/);
  48. });
  49. it("should throw an error when merge is an invalid string", () => {
  50. assert.throws(() => {
  51. new ObjectSchema({
  52. foo: {
  53. merge: "bar",
  54. validate() { }
  55. }
  56. });
  57. }, /key "foo" missing valid merge strategy/);
  58. });
  59. it("should throw an error when validate is an invalid string", () => {
  60. assert.throws(() => {
  61. new ObjectSchema({
  62. foo: {
  63. merge: "assign",
  64. validate: "s"
  65. }
  66. });
  67. }, /key "foo" missing valid validation strategy/);
  68. });
  69. });
  70. describe("merge()", () => {
  71. it("should throw an error when an unexpected key is found", () => {
  72. let schema = new ObjectSchema({});
  73. assert.throws(() => {
  74. schema.merge({ foo: true }, { foo: true });
  75. }, /Unexpected key "foo"/);
  76. });
  77. it("should throw an error when merge() throws an error", () => {
  78. let schema = new ObjectSchema({
  79. foo: {
  80. merge() {
  81. throw new Error("Boom!");
  82. },
  83. validate() {}
  84. }
  85. });
  86. assert.throws(() => {
  87. schema.merge({ foo: true }, { foo: true });
  88. }, /Key "foo": Boom!/);
  89. });
  90. it("should call the merge() strategy for one key when called", () => {
  91. schema = new ObjectSchema({
  92. foo: {
  93. merge() {
  94. return "bar";
  95. },
  96. validate() {}
  97. }
  98. });
  99. const result = schema.merge({ foo: true }, { foo: false });
  100. assert.propertyVal(result, "foo", "bar");
  101. });
  102. it("should not call the merge() strategy when both objects don't contain the key", () => {
  103. let called = false;
  104. schema = new ObjectSchema({
  105. foo: {
  106. merge() {
  107. called = true;
  108. },
  109. validate() {}
  110. }
  111. });
  112. schema.merge({}, {});
  113. assert.isFalse(called, "The merge() strategy should not have been called.");
  114. });
  115. it("should omit returning the key when the merge() strategy returns undefined", () => {
  116. schema = new ObjectSchema({
  117. foo: {
  118. merge() {
  119. return undefined;
  120. },
  121. validate() { }
  122. }
  123. });
  124. const result = schema.merge({ foo: true }, { foo: false });
  125. assert.notProperty(result, "foo");
  126. });
  127. it("should call the merge() strategy for two keys when called", () => {
  128. schema = new ObjectSchema({
  129. foo: {
  130. merge() {
  131. return "bar";
  132. },
  133. validate() { }
  134. },
  135. bar: {
  136. merge() {
  137. return "baz";
  138. },
  139. validate() {}
  140. }
  141. });
  142. const result = schema.merge({ foo: true, bar: 1 }, { foo: true, bar: 2 });
  143. assert.propertyVal(result, "foo", "bar");
  144. assert.propertyVal(result, "bar", "baz");
  145. });
  146. it("should call the merge() strategy for two keys when called on three objects", () => {
  147. schema = new ObjectSchema({
  148. foo: {
  149. merge() {
  150. return "bar";
  151. },
  152. validate() { }
  153. },
  154. bar: {
  155. merge() {
  156. return "baz";
  157. },
  158. validate() { }
  159. }
  160. });
  161. const result = schema.merge(
  162. { foo: true, bar: 1 },
  163. { foo: true, bar: 3 },
  164. { foo: false, bar: 2 }
  165. );
  166. assert.propertyVal(result, "foo", "bar");
  167. assert.propertyVal(result, "bar", "baz");
  168. });
  169. it("should call the merge() strategy when defined as 'overwrite'", () => {
  170. schema = new ObjectSchema({
  171. foo: {
  172. merge: "overwrite",
  173. validate() { }
  174. }
  175. });
  176. const result = schema.merge(
  177. { foo: true },
  178. { foo: false }
  179. );
  180. assert.propertyVal(result, "foo", false);
  181. });
  182. it("should call the merge() strategy when defined as 'assign'", () => {
  183. schema = new ObjectSchema({
  184. foo: {
  185. merge: "assign",
  186. validate() { }
  187. }
  188. });
  189. const result = schema.merge(
  190. { foo: { bar: true } },
  191. { foo: { baz: false } }
  192. );
  193. assert.strictEqual(result.foo.bar, true);
  194. assert.strictEqual(result.foo.baz, false);
  195. });
  196. it("should call the merge strategy when there's a subschema", () => {
  197. schema = new ObjectSchema({
  198. name: {
  199. schema: {
  200. first: {
  201. merge: "replace",
  202. validate: "string"
  203. },
  204. last: {
  205. merge: "replace",
  206. validate: "string"
  207. }
  208. }
  209. }
  210. });
  211. const result = schema.merge({
  212. name: {
  213. first: "n",
  214. last: "z"
  215. }
  216. }, {
  217. name: {
  218. first: "g"
  219. }
  220. });
  221. assert.strictEqual(result.name.first, "g");
  222. assert.strictEqual(result.name.last, "z");
  223. });
  224. it("should not error when calling the merge strategy when there's a subschema and no matching key in second object", () => {
  225. schema = new ObjectSchema({
  226. name: {
  227. schema: {
  228. first: {
  229. merge: "replace",
  230. validate: "string"
  231. },
  232. last: {
  233. merge: "replace",
  234. validate: "string"
  235. }
  236. }
  237. }
  238. });
  239. const result = schema.merge({
  240. name: {
  241. first: "n",
  242. last: "z"
  243. }
  244. }, {
  245. });
  246. assert.strictEqual(result.name.first, "n");
  247. assert.strictEqual(result.name.last, "z");
  248. });
  249. });
  250. describe("validate()", () => {
  251. it("should throw an error when an unexpected key is found", () => {
  252. let schema = new ObjectSchema({});
  253. assert.throws(() => {
  254. schema.validate({ foo: true });
  255. }, /Unexpected key "foo"/);
  256. });
  257. it("should not throw an error when an expected key is found", () => {
  258. schema = new ObjectSchema({
  259. foo: {
  260. merge() {
  261. return "bar";
  262. },
  263. validate() {}
  264. }
  265. });
  266. schema.validate({ foo: true });
  267. });
  268. it("should pass the property value into validate() when key is found", () => {
  269. schema = new ObjectSchema({
  270. foo: {
  271. merge() {
  272. return "bar";
  273. },
  274. validate(value) {
  275. assert.isTrue(value);
  276. }
  277. }
  278. });
  279. schema.validate({ foo: true });
  280. });
  281. it("should not throw an error when expected keys are found", () => {
  282. schema = new ObjectSchema({
  283. foo: {
  284. merge() {
  285. return "bar";
  286. },
  287. validate() {}
  288. },
  289. bar: {
  290. merge() {
  291. return "baz";
  292. },
  293. validate() {}
  294. }
  295. });
  296. schema.validate({ foo: true, bar: true });
  297. });
  298. it("should not throw an error when expected keys are found with required keys", () => {
  299. schema = new ObjectSchema({
  300. foo: {
  301. merge() {
  302. return "bar";
  303. },
  304. validate() { }
  305. },
  306. bar: {
  307. requires: ["foo"],
  308. merge() {
  309. return "baz";
  310. },
  311. validate() { }
  312. }
  313. });
  314. schema.validate({ foo: true, bar: true });
  315. });
  316. it("should throw an error when expected keys are found without required keys", () => {
  317. schema = new ObjectSchema({
  318. foo: {
  319. merge() {
  320. return "bar";
  321. },
  322. validate() { }
  323. },
  324. baz: {
  325. merge() {
  326. return "baz";
  327. },
  328. validate() { }
  329. },
  330. bar: {
  331. name: "bar",
  332. requires: ["foo", "baz"],
  333. merge() { },
  334. validate() { }
  335. }
  336. });
  337. assert.throws(() => {
  338. schema.validate({ bar: true });
  339. }, /Key "bar" requires keys "foo", "baz"./);
  340. });
  341. it("should throw an error when an expected key is found but is invalid", () => {
  342. schema = new ObjectSchema({
  343. foo: {
  344. merge() {
  345. return "bar";
  346. },
  347. validate() {
  348. throw new Error("Invalid key.");
  349. }
  350. }
  351. });
  352. assert.throws(() => {
  353. schema.validate({ foo: true });
  354. }, /Key "foo": Invalid key/);
  355. });
  356. it("should throw an error when an expected key is found but is invalid with a string validator", () => {
  357. schema = new ObjectSchema({
  358. foo: {
  359. merge() {
  360. return "bar";
  361. },
  362. validate: "string"
  363. }
  364. });
  365. assert.throws(() => {
  366. schema.validate({ foo: true });
  367. }, /Key "foo": Expected a string/);
  368. });
  369. it("should throw an error when an expected key is found but is invalid with a number validator", () => {
  370. schema = new ObjectSchema({
  371. foo: {
  372. merge() {
  373. return "bar";
  374. },
  375. validate: "number"
  376. }
  377. });
  378. assert.throws(() => {
  379. schema.validate({ foo: true });
  380. }, /Key "foo": Expected a number/);
  381. });
  382. it("should throw an error when a required key is missing", () => {
  383. schema = new ObjectSchema({
  384. foo: {
  385. required: true,
  386. merge() {
  387. return "bar";
  388. },
  389. validate() {}
  390. }
  391. });
  392. assert.throws(() => {
  393. schema.validate({});
  394. }, /Missing required key "foo"/);
  395. });
  396. it("should throw an error when a subschema is provided and the value doesn't validate", () => {
  397. schema = new ObjectSchema({
  398. name: {
  399. schema: {
  400. first: {
  401. merge: "replace",
  402. validate: "string"
  403. },
  404. last: {
  405. merge: "replace",
  406. validate: "string"
  407. }
  408. }
  409. }
  410. });
  411. assert.throws(() => {
  412. schema.validate({
  413. name: {
  414. first: 123,
  415. last: "z"
  416. }
  417. });
  418. }, /Key "name": Key "first": Expected a string/);
  419. });
  420. it("should not throw an error when a subschema is provided and the value validates", () => {
  421. schema = new ObjectSchema({
  422. name: {
  423. schema: {
  424. first: {
  425. merge: "replace",
  426. validate: "string"
  427. },
  428. last: {
  429. merge: "replace",
  430. validate: "string"
  431. }
  432. }
  433. }
  434. });
  435. schema.validate({
  436. name: {
  437. first: "n",
  438. last: "z"
  439. }
  440. });
  441. });
  442. });
  443. });