Manual Reference Source

app/modules/pipeline/validator/valfunctions/checks.js

  1. // @flow
  2. const _ = require('lodash');
  3. const Utils = require('../../../utils/utils');
  4. const Errors = require('../../../exceptions/errors');
  5. const EntitiesUtils = require('../../../utils/entities');
  6. const Handlebars = require('../../../utils/templating');
  7.  
  8. function is_unique(path: string, type: string,
  9. filters: Array<Object> = [], error: ?Object): Function {
  10. return async function func(object: Object, method: string): Promise<boolean> {
  11. if (method === 'put') {
  12. return true;
  13. }
  14.  
  15. const p = path.split('.');
  16. const info = Utils.find_value_with_path(object, p);
  17.  
  18.  
  19. let where = { [path]: info };
  20. if (filters.length > 0) {
  21. where = {
  22. $and: [
  23. { [path]: info },
  24. ],
  25. };
  26.  
  27. filters.forEach((f) => {
  28. const nf = Object.keys(f).reduce((obj, k) => {
  29. obj[k] = Handlebars.compile(f[k])(object);
  30. return obj;
  31. }, {});
  32. where.$and.push(nf);
  33. });
  34. }
  35.  
  36. const result = await EntitiesUtils.search(type, { where });
  37. if (result && Object.keys(result).length > 0 && result.result.hits.length > 0) {
  38. error = error == null ? Errors.AlreadyExistingEntity : error;
  39. error.path = path;
  40. throw error;
  41. } else {
  42. return true;
  43. }
  44. };
  45. }
  46.  
  47. function if_exists(path: string, type: string, error_in_parent: boolean = false, error: ?Object): Function {
  48. return async function func(object: Object, method: string): Promise<boolean> {
  49. const p = path.split('.');
  50. const info = [...Utils.find_popvalue_with_path(object, p)];
  51.  
  52. if (info.length === 0) {
  53. return true;
  54. }
  55.  
  56. const sources = await EntitiesUtils.search_and_get_sources(type, { where: { _id: info } });
  57. if (sources.length === info.length) {
  58. return true;
  59. }
  60.  
  61. error = error == null ? Errors.InvalidEntity : error;
  62. if (error_in_parent) {
  63. error.path = p.slice(0, p.length - 1).join('.');
  64. } else {
  65. error.path = path;
  66. }
  67. throw error;
  68. };
  69. }
  70.  
  71. function is_valid_json(path: string, error: ?Object): Function {
  72. return async function func(object: Object, method: string): Promise<boolean> {
  73. const p = path.split('.');
  74. const info = [...Utils.find_popvalue_with_path(object, p)];
  75. if (info.length === 0) {
  76. return true;
  77. }
  78.  
  79. if (info[0] instanceof Object) {
  80. return true;
  81. }
  82.  
  83. try {
  84. JSON.parse(info[0]);
  85. return true;
  86. } catch (err) {
  87. error = error == null ? Errors.InvalidObject : error;
  88. error.path = path;
  89. throw error;
  90. }
  91. };
  92. }
  93.  
  94. function is_conditioned_on(in_path: string, check: Function, out_path: string,
  95. is: Function, error: ?Object): Function {
  96. return async function f(object): Promise<boolean> {
  97. // Semantic of the function
  98. // Check(in_path) when Is(out_path)
  99.  
  100. const in_segs = in_path.split('.');
  101. const out_segs = out_path.split('.');
  102.  
  103. const in_info = [...Utils.find_popvalue_with_path(object, in_segs)];
  104. const out_info = Utils.find_value_with_path(object, out_segs);
  105.  
  106. if (out_info == null) {
  107. return true;
  108. }
  109.  
  110. if (!is(out_info)) {
  111. return true;
  112. }
  113.  
  114. if (in_info.length === 0) {
  115. error = error == null ? Errors.InvalidObject : error;
  116. error.path = in_path;
  117. throw error;
  118. }
  119.  
  120. const verif = in_info.every(io => check(io));
  121.  
  122. if (!verif) {
  123. error = error == null ? Errors.InvalidObject : error;
  124. error.path = in_path;
  125. throw error;
  126. }
  127.  
  128. return true;
  129. };
  130. }
  131.  
  132. module.exports = {
  133. is_unique,
  134. if_exists,
  135. is_valid_json,
  136. is_conditioned_on,
  137. };