object-schema.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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 return separate objects when using subschema", () => {
  225. schema = new ObjectSchema({
  226. age: {
  227. merge: "replace",
  228. validate: "number"
  229. },
  230. address: {
  231. schema: {
  232. street: {
  233. schema: {
  234. number: {
  235. merge: "replace",
  236. validate: "number"
  237. },
  238. streetName: {
  239. merge: "replace",
  240. validate: "string"
  241. }
  242. }
  243. },
  244. state: {
  245. merge: "replace",
  246. validate: "string"
  247. }
  248. }
  249. }
  250. });
  251. const baseObject = {
  252. address: {
  253. street: {
  254. number: 100,
  255. streetName: "Foo St"
  256. },
  257. state: "HA"
  258. }
  259. };
  260. const result = schema.merge(baseObject, {
  261. age: 29
  262. });
  263. assert.notStrictEqual(result.address.street, baseObject.address.street);
  264. assert.deepStrictEqual(result.address, baseObject.address);
  265. });
  266. it("should not error when calling the merge strategy when there's a subschema and no matching key in second object", () => {
  267. schema = new ObjectSchema({
  268. name: {
  269. schema: {
  270. first: {
  271. merge: "replace",
  272. validate: "string"
  273. },
  274. last: {
  275. merge: "replace",
  276. validate: "string"
  277. }
  278. }
  279. }
  280. });
  281. const result = schema.merge({
  282. name: {
  283. first: "n",
  284. last: "z"
  285. }
  286. }, {
  287. });
  288. assert.strictEqual(result.name.first, "n");
  289. assert.strictEqual(result.name.last, "z");
  290. });
  291. it("should not error when calling the merge strategy when there's multiple subschemas and no matching key in second object", () => {
  292. schema = new ObjectSchema({
  293. user: {
  294. schema: {
  295. name: {
  296. schema: {
  297. first: {
  298. merge: "replace",
  299. validate: "string"
  300. },
  301. last: {
  302. merge: "replace",
  303. validate: "string"
  304. }
  305. }
  306. }
  307. }
  308. }
  309. });
  310. const result = schema.merge({
  311. user: {
  312. name: {
  313. first: "n",
  314. last: "z"
  315. }
  316. }
  317. }, {
  318. });
  319. assert.strictEqual(result.user.name.first, "n");
  320. assert.strictEqual(result.user.name.last, "z");
  321. });
  322. });
  323. describe("validate()", () => {
  324. it("should throw an error when an unexpected key is found", () => {
  325. let schema = new ObjectSchema({});
  326. assert.throws(() => {
  327. schema.validate({ foo: true });
  328. }, /Unexpected key "foo"/);
  329. });
  330. it("should not throw an error when an expected key is found", () => {
  331. schema = new ObjectSchema({
  332. foo: {
  333. merge() {
  334. return "bar";
  335. },
  336. validate() {}
  337. }
  338. });
  339. schema.validate({ foo: true });
  340. });
  341. it("should pass the property value into validate() when key is found", () => {
  342. schema = new ObjectSchema({
  343. foo: {
  344. merge() {
  345. return "bar";
  346. },
  347. validate(value) {
  348. assert.isTrue(value);
  349. }
  350. }
  351. });
  352. schema.validate({ foo: true });
  353. });
  354. it("should not throw an error when expected keys are found", () => {
  355. schema = new ObjectSchema({
  356. foo: {
  357. merge() {
  358. return "bar";
  359. },
  360. validate() {}
  361. },
  362. bar: {
  363. merge() {
  364. return "baz";
  365. },
  366. validate() {}
  367. }
  368. });
  369. schema.validate({ foo: true, bar: true });
  370. });
  371. it("should not throw an error when expected keys are found with required keys", () => {
  372. schema = new ObjectSchema({
  373. foo: {
  374. merge() {
  375. return "bar";
  376. },
  377. validate() { }
  378. },
  379. bar: {
  380. requires: ["foo"],
  381. merge() {
  382. return "baz";
  383. },
  384. validate() { }
  385. }
  386. });
  387. schema.validate({ foo: true, bar: true });
  388. });
  389. it("should throw an error when expected keys are found without required keys", () => {
  390. schema = new ObjectSchema({
  391. foo: {
  392. merge() {
  393. return "bar";
  394. },
  395. validate() { }
  396. },
  397. baz: {
  398. merge() {
  399. return "baz";
  400. },
  401. validate() { }
  402. },
  403. bar: {
  404. name: "bar",
  405. requires: ["foo", "baz"],
  406. merge() { },
  407. validate() { }
  408. }
  409. });
  410. assert.throws(() => {
  411. schema.validate({ bar: true });
  412. }, /Key "bar" requires keys "foo", "baz"./);
  413. });
  414. it("should throw an error when an expected key is found but is invalid", () => {
  415. schema = new ObjectSchema({
  416. foo: {
  417. merge() {
  418. return "bar";
  419. },
  420. validate() {
  421. throw new Error("Invalid key.");
  422. }
  423. }
  424. });
  425. assert.throws(() => {
  426. schema.validate({ foo: true });
  427. }, /Key "foo": Invalid key/);
  428. });
  429. it("should throw an error when an expected key is found but is invalid with a string validator", () => {
  430. schema = new ObjectSchema({
  431. foo: {
  432. merge() {
  433. return "bar";
  434. },
  435. validate: "string"
  436. }
  437. });
  438. assert.throws(() => {
  439. schema.validate({ foo: true });
  440. }, /Key "foo": Expected a string/);
  441. });
  442. it("should throw an error when an expected key is found but is invalid with a number validator", () => {
  443. schema = new ObjectSchema({
  444. foo: {
  445. merge() {
  446. return "bar";
  447. },
  448. validate: "number"
  449. }
  450. });
  451. assert.throws(() => {
  452. schema.validate({ foo: true });
  453. }, /Key "foo": Expected a number/);
  454. });
  455. it("should throw an error when a required key is missing", () => {
  456. schema = new ObjectSchema({
  457. foo: {
  458. required: true,
  459. merge() {
  460. return "bar";
  461. },
  462. validate() {}
  463. }
  464. });
  465. assert.throws(() => {
  466. schema.validate({});
  467. }, /Missing required key "foo"/);
  468. });
  469. it("should throw an error when a subschema is provided and the value doesn't validate", () => {
  470. schema = new ObjectSchema({
  471. name: {
  472. schema: {
  473. first: {
  474. merge: "replace",
  475. validate: "string"
  476. },
  477. last: {
  478. merge: "replace",
  479. validate: "string"
  480. }
  481. }
  482. }
  483. });
  484. assert.throws(() => {
  485. schema.validate({
  486. name: {
  487. first: 123,
  488. last: "z"
  489. }
  490. });
  491. }, /Key "name": Key "first": Expected a string/);
  492. });
  493. it("should not throw an error when a subschema is provided and the value validates", () => {
  494. schema = new ObjectSchema({
  495. name: {
  496. schema: {
  497. first: {
  498. merge: "replace",
  499. validate: "string"
  500. },
  501. last: {
  502. merge: "replace",
  503. validate: "string"
  504. }
  505. }
  506. }
  507. });
  508. schema.validate({
  509. name: {
  510. first: "n",
  511. last: "z"
  512. }
  513. });
  514. });
  515. });
  516. });