Guest User

Untitled

a guest
Jan 23rd, 2018
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. var Helpers = {
  2.  
  3. /* Schema keys are defined as either
  4. key: Type
  5. or
  6. key: { type: Type }
  7. This helper just converts the shorthand version to key: {type: Type}
  8. and converts the Type to a string
  9. */
  10. schema_parse(obj) {
  11. if (typeof obj == 'function') {
  12. obj = {
  13. type: typeof obj()
  14. };
  15. } else {
  16. obj.type = typeof obj.type();
  17. }
  18.  
  19. return obj;
  20. }
  21.  
  22. };
  23.  
  24. function Schema(schema) {
  25. // Definition of types, stored as key: typeof __
  26. const _types = {};
  27. // List
  28. const _required = [];
  29.  
  30. // Parse the schema
  31. for (let key in schema) {
  32. let definition = Helpers.schema_parse(schema[key]);
  33.  
  34. _types[key] = definition.type;
  35.  
  36. if (definition.required) {
  37. _required.push(key);
  38. }
  39. }
  40.  
  41. return function(json) {
  42.  
  43. function _ValidateTypes() {
  44. let json_type,
  45. target_type,
  46. result = true;
  47. for (var key in json) {
  48. if (_types[key]) {
  49. json_type = typeof json[key];
  50. target_type = _types[key];
  51. if (json_type !== target_type) {
  52. console.log(`'${key}' is not the right type; Got '${json_type}', was expecting '${target_type}'`);
  53. result = false;
  54. }
  55. }
  56. }
  57.  
  58. return result;
  59. }
  60.  
  61. function _ValidateRequired() {
  62. for (var i = 0; i < _required.length; i++) {
  63. const required_key = _required[i];
  64. if (!(required_key in json)) {
  65. console.log(required_key + " is required");
  66. return false;
  67. }
  68. }
  69.  
  70. return true;
  71. }
  72.  
  73. return {
  74.  
  75. get valid() {
  76. let isValid = _ValidateRequired() && _ValidateTypes();
  77. if (!isValid) {
  78. console.warn("Unable to validate JSON", json);
  79. }
  80. return isValid;
  81. },
  82.  
  83. get json() {
  84. return json;
  85. }
  86.  
  87. };
  88. };
  89. }
  90.  
  91. /***
  92.  
  93. Below are tests used just for quick development, ignore this.
  94. Just leaving it in the gist to show what the point of the class eventually is
  95.  
  96. */
  97.  
  98. const SubscribtionModel = Schema({
  99. name: String,
  100. id: Number,
  101. email: {
  102. type: String,
  103. required: true
  104. }
  105. });
  106.  
  107. // This should pass, valid JSON
  108. const valid1 = {
  109. name: "hello",
  110. id: 1824,
  111. email: "foo@bar.com"
  112. };
  113.  
  114. console.log("##Valid ", SubscribtionModel(valid1).valid);
  115.  
  116. // This should pass, name isn't required
  117. const valid2 = {
  118. id: 9892,
  119. email: "test@gmawil.com"
  120. };
  121.  
  122. console.log("##Valid 2", SubscribtionModel(valid2).valid);
  123.  
  124. // This should fail due to bad types
  125. const typeBreak = {
  126. name: 1234,
  127. id: "hello",
  128. email: "foo@bar.com"
  129. };
  130.  
  131. console.log("##Type Break 1", !SubscribtionModel(typeBreak).valid);
  132.  
  133. // This should fail due to bad type
  134. const typeBreak2 = {
  135. name: "hello",
  136. email: 12923
  137. };
  138.  
  139. console.log("##Type Break 2", !SubscribtionModel(typeBreak2).valid);
  140.  
  141. // This should fail due to required email field
  142. const required = {
  143. name: "hello"
  144. };
  145.  
  146. console.log("##Required Break", !SubscribtionModel(required).valid);
  147.  
  148. export default Schema;
Add Comment
Please, Sign In to add comment