ari_model_validators.c.mustache 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "asterisk/logger.h"
  27. #include "asterisk/module.h"
  28. #include "ari_model_validators.h"
  29. {{#apis}}
  30. {{#api_declaration}}
  31. {{#models}}
  32. int ast_ari_validate_{{c_id}}(struct ast_json *json)
  33. {
  34. int res = 1;
  35. struct ast_json_iter *iter;
  36. {{#properties}}
  37. {{#required}}
  38. int has_{{name}} = 0;
  39. {{/required}}
  40. {{/properties}}
  41. {{#has_subtypes}}
  42. const char *discriminator;
  43. discriminator = ast_json_string_get(ast_json_object_get(json, "{{discriminator.name}}"));
  44. if (!discriminator) {
  45. ast_log(LOG_ERROR, "ARI {{id}} missing required field {{discriminator.name}}\n");
  46. return 0;
  47. }
  48. if (strcmp("{{id}}", discriminator) == 0) {
  49. /* Self type; fall through */
  50. } else
  51. {{#all_subtypes}}
  52. if (strcmp("{{id}}", discriminator) == 0) {
  53. return ast_ari_validate_{{c_id}}(json);
  54. } else
  55. {{/all_subtypes}}
  56. {
  57. ast_log(LOG_ERROR, "ARI {{id}} has undocumented subtype %s\n",
  58. discriminator);
  59. res = 0;
  60. }
  61. {{/has_subtypes}}
  62. for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
  63. {{#properties}}
  64. if (strcmp("{{name}}", ast_json_object_iter_key(iter)) == 0) {
  65. int prop_is_valid;
  66. {{#required}}
  67. has_{{name}} = 1;
  68. {{/required}}
  69. {{#type}}
  70. {{#is_list}}
  71. prop_is_valid = ast_ari_validate_list(
  72. ast_json_object_iter_value(iter),
  73. ast_ari_validate_{{c_singular_name}});
  74. {{/is_list}}
  75. {{^is_list}}
  76. prop_is_valid = ast_ari_validate_{{c_name}}(
  77. ast_json_object_iter_value(iter));
  78. {{/is_list}}
  79. {{/type}}
  80. if (!prop_is_valid) {
  81. ast_log(LOG_ERROR, "ARI {{id}} field {{name}} failed validation\n");
  82. res = 0;
  83. }
  84. } else
  85. {{/properties}}
  86. {
  87. ast_log(LOG_ERROR,
  88. "ARI {{id}} has undocumented field %s\n",
  89. ast_json_object_iter_key(iter));
  90. res = 0;
  91. }
  92. }
  93. {{#properties}}
  94. {{#required}}
  95. if (!has_{{name}}) {
  96. ast_log(LOG_ERROR, "ARI {{id}} missing required field {{name}}\n");
  97. res = 0;
  98. }
  99. {{/required}}
  100. {{/properties}}
  101. return res;
  102. }
  103. ari_validator ast_ari_validate_{{c_id}}_fn(void)
  104. {
  105. return ast_ari_validate_{{c_id}};
  106. }
  107. {{/models}}
  108. {{/api_declaration}}
  109. {{/apis}}