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.

README.md 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # JavaScript ObjectSchema Package
  2. by [Nicholas C. Zakas](https://humanwhocodes.com)
  3. If you find this useful, please consider supporting my work with a [donation](https://humanwhocodes.com/donate).
  4. ## Overview
  5. A JavaScript object merge/validation utility where you can define a different merge and validation strategy for each key. This is helpful when you need to validate complex data structures and then merge them in a way that is more complex than `Object.assign()`.
  6. ## Installation
  7. You can install using either npm:
  8. ```
  9. npm install @humanwhocodes/object-schema
  10. ```
  11. Or Yarn:
  12. ```
  13. yarn add @humanwhocodes/object-schema
  14. ```
  15. ## Usage
  16. Use CommonJS to get access to the `ObjectSchema` constructor:
  17. ```js
  18. const { ObjectSchema } = require("@humanwhocodes/object-schema");
  19. const schema = new ObjectSchema({
  20. // define a definition for the "downloads" key
  21. downloads: {
  22. required: true,
  23. merge(value1, value2) {
  24. return value1 + value2;
  25. },
  26. validate(value) {
  27. if (typeof value !== "number") {
  28. throw new Error("Expected downloads to be a number.");
  29. }
  30. }
  31. },
  32. // define a strategy for the "versions" key
  33. version: {
  34. required: true,
  35. merge(value1, value2) {
  36. return value1.concat(value2);
  37. },
  38. validate(value) {
  39. if (!Array.isArray(value)) {
  40. throw new Error("Expected versions to be an array.");
  41. }
  42. }
  43. }
  44. });
  45. const record1 = {
  46. downloads: 25,
  47. versions: [
  48. "v1.0.0",
  49. "v1.1.0",
  50. "v1.2.0"
  51. ]
  52. };
  53. const record2 = {
  54. downloads: 125,
  55. versions: [
  56. "v2.0.0",
  57. "v2.1.0",
  58. "v3.0.0"
  59. ]
  60. };
  61. // make sure the records are valid
  62. schema.validate(record1);
  63. schema.validate(record2);
  64. // merge together (schema.merge() accepts any number of objects)
  65. const result = schema.merge(record1, record2);
  66. // result looks like this:
  67. const result = {
  68. downloads: 75,
  69. versions: [
  70. "v1.0.0",
  71. "v1.1.0",
  72. "v1.2.0",
  73. "v2.0.0",
  74. "v2.1.0",
  75. "v3.0.0"
  76. ]
  77. };
  78. ```
  79. ## Tips and Tricks
  80. ### Named merge strategies
  81. Instead of specifying a `merge()` method, you can specify one of the following strings to use a default merge strategy:
  82. * `"assign"` - use `Object.assign()` to merge the two values into one object.
  83. * `"overwrite"` - the second value always replaces the first.
  84. * `"replace"` - the second value replaces the first if the second is not `undefined`.
  85. For example:
  86. ```js
  87. const schema = new ObjectSchema({
  88. name: {
  89. merge: "replace",
  90. validate() {}
  91. }
  92. });
  93. ```
  94. ### Named validation strategies
  95. Instead of specifying a `validate()` method, you can specify one of the following strings to use a default validation strategy:
  96. * `"array"` - value must be an array.
  97. * `"boolean"` - value must be a boolean.
  98. * `"number"` - value must be a number.
  99. * `"object"` - value must be an object.
  100. * `"object?"` - value must be an object or null.
  101. * `"string"` - value must be a string.
  102. * `"string!"` - value must be a non-empty string.
  103. For example:
  104. ```js
  105. const schema = new ObjectSchema({
  106. name: {
  107. merge: "replace",
  108. validate: "string"
  109. }
  110. });
  111. ```
  112. ### Subschemas
  113. If you are defining a key that is, itself, an object, you can simplify the process by using a subschema. Instead of defining `merge()` and `validate()`, assign a `schema` key that contains a schema definition, like this:
  114. ```js
  115. const schema = new ObjectSchema({
  116. name: {
  117. schema: {
  118. first: {
  119. merge: "replace",
  120. validate: "string"
  121. },
  122. last: {
  123. merge: "replace",
  124. validate: "string"
  125. }
  126. }
  127. }
  128. });
  129. schema.validate({
  130. name: {
  131. first: "n",
  132. last: "z"
  133. }
  134. });
  135. ```
  136. ### Remove Keys During Merge
  137. If the merge strategy for a key returns `undefined`, then the key will not appear in the final object. For example:
  138. ```js
  139. const schema = new ObjectSchema({
  140. date: {
  141. merge() {
  142. return undefined;
  143. },
  144. validate(value) {
  145. Date.parse(value); // throws an error when invalid
  146. }
  147. }
  148. });
  149. const object1 = { date: "5/5/2005" };
  150. const object2 = { date: "6/6/2006" };
  151. const result = schema.merge(object1, object2);
  152. console.log("date" in result); // false
  153. ```
  154. ### Requiring Another Key Be Present
  155. If you'd like the presence of one key to require the presence of another key, you can use the `requires` property to specify an array of other properties that any key requires. For example:
  156. ```js
  157. const schema = new ObjectSchema();
  158. const schema = new ObjectSchema({
  159. date: {
  160. merge() {
  161. return undefined;
  162. },
  163. validate(value) {
  164. Date.parse(value); // throws an error when invalid
  165. }
  166. },
  167. time: {
  168. requires: ["date"],
  169. merge(first, second) {
  170. return second;
  171. },
  172. validate(value) {
  173. // ...
  174. }
  175. }
  176. });
  177. // throws error: Key "time" requires keys "date"
  178. schema.validate({
  179. time: "13:45"
  180. });
  181. ```
  182. In this example, even though `date` is an optional key, it is required to be present whenever `time` is present.
  183. ## License
  184. BSD 3-Clause