ari_model_validators.c.mustache 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2013, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief Generated file - Build validators for ARI model objects.
  19. */
  20. /*
  21. {{> do-not-edit}}
  22. * This file is generated by a mustache template. Please see the original
  23. * template in rest-api-templates/ari_model_validators.c.mustache
  24. */
  25. #include "asterisk.h"
  26. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  27. #include "asterisk/logger.h"
  28. #include "asterisk/module.h"
  29. #include "ari_model_validators.h"
  30. {{#apis}}
  31. {{#api_declaration}}
  32. {{#models}}
  33. int ast_ari_validate_{{c_id}}(struct ast_json *json)
  34. {
  35. int res = 1;
  36. struct ast_json_iter *iter;
  37. {{#properties}}
  38. {{#required}}
  39. int has_{{name}} = 0;
  40. {{/required}}
  41. {{/properties}}
  42. {{#has_subtypes}}
  43. const char *discriminator;
  44. discriminator = ast_json_string_get(ast_json_object_get(json, "{{discriminator.name}}"));
  45. if (!discriminator) {
  46. ast_log(LOG_ERROR, "ARI {{id}} missing required field {{discriminator.name}}\n");
  47. return 0;
  48. }
  49. if (strcmp("{{id}}", discriminator) == 0) {
  50. /* Self type; fall through */
  51. } else
  52. {{#all_subtypes}}
  53. if (strcmp("{{id}}", discriminator) == 0) {
  54. return ast_ari_validate_{{c_id}}(json);
  55. } else
  56. {{/all_subtypes}}
  57. {
  58. ast_log(LOG_ERROR, "ARI {{id}} has undocumented subtype %s\n",
  59. discriminator);
  60. res = 0;
  61. }
  62. {{/has_subtypes}}
  63. for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
  64. {{#properties}}
  65. if (strcmp("{{name}}", ast_json_object_iter_key(iter)) == 0) {
  66. int prop_is_valid;
  67. {{#required}}
  68. has_{{name}} = 1;
  69. {{/required}}
  70. {{#type}}
  71. {{#is_list}}
  72. prop_is_valid = ast_ari_validate_list(
  73. ast_json_object_iter_value(iter),
  74. ast_ari_validate_{{c_singular_name}});
  75. {{/is_list}}
  76. {{^is_list}}
  77. prop_is_valid = ast_ari_validate_{{c_name}}(
  78. ast_json_object_iter_value(iter));
  79. {{/is_list}}
  80. {{/type}}
  81. if (!prop_is_valid) {
  82. ast_log(LOG_ERROR, "ARI {{id}} field {{name}} failed validation\n");
  83. res = 0;
  84. }
  85. } else
  86. {{/properties}}
  87. {
  88. ast_log(LOG_ERROR,
  89. "ARI {{id}} has undocumented field %s\n",
  90. ast_json_object_iter_key(iter));
  91. res = 0;
  92. }
  93. }
  94. {{#properties}}
  95. {{#required}}
  96. if (!has_{{name}}) {
  97. ast_log(LOG_ERROR, "ARI {{id}} missing required field {{name}}\n");
  98. res = 0;
  99. }
  100. {{/required}}
  101. {{/properties}}
  102. return res;
  103. }
  104. ari_validator ast_ari_validate_{{c_id}}_fn(void)
  105. {
  106. return ast_ari_validate_{{c_id}};
  107. }
  108. {{/models}}
  109. {{/api_declaration}}
  110. {{/apis}}