Debian Codegraph @

rest-api-templates/ari_model_validators.c.mustache

raw
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
17 /*! \file
18 *
19 * \brief Generated file - Build validators for ARI model objects.
20 */
21
22 /*
23 {{> do-not-edit}}
24 * This file is generated by a mustache template. Please see the original
25 * template in rest-api-templates/ari_model_validators.c.mustache
26 */
27
28 #include "asterisk.h"
29
30 #include "asterisk/logger.h"
31 #include "asterisk/module.h"
32 #include "ari_model_validators.h"
33 {{#apis}}
34 {{#api_declaration}}
35 {{#models}}
36
37 int ast_ari_validate_{{c_id}}(struct ast_json *json)
38 {
39 int res = 1;
40 struct ast_json_iter *iter;
41 {{#properties}}
42 {{#required}}
43 int has_{{name}} = 0;
44 {{/required}}
45 {{/properties}}
46 {{#has_subtypes}}
47 const char *discriminator;
48
49 discriminator = ast_json_string_get(ast_json_object_get(json, "{{discriminator.name}}"));
50 if (!discriminator) {
51 ast_log(LOG_ERROR, "ARI {{id}} missing required field {{discriminator.name}}\n");
52 return 0;
53 }
54
55 if (strcmp("{{id}}", discriminator) == 0) {
56 /* Self type; fall through */
57 } else
58 {{#all_subtypes}}
59 if (strcmp("{{id}}", discriminator) == 0) {
60 return ast_ari_validate_{{c_id}}(json);
61 } else
62 {{/all_subtypes}}
63 {
64 ast_log(LOG_ERROR, "ARI {{id}} has undocumented subtype %s\n",
65 discriminator);
66 res = 0;
67 }
68 {{/has_subtypes}}
69
70 for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) {
71 {{#properties}}
72 if (strcmp("{{name}}", ast_json_object_iter_key(iter)) == 0) {
73 int prop_is_valid;
74 {{#required}}
75 has_{{name}} = 1;
76 {{/required}}
77 {{#type}}
78 {{#is_list}}
79 prop_is_valid = ast_ari_validate_list(
80 ast_json_object_iter_value(iter),
81 ast_ari_validate_{{c_singular_name}});
82 {{/is_list}}
83 {{^is_list}}
84 prop_is_valid = ast_ari_validate_{{c_name}}(
85 ast_json_object_iter_value(iter));
86 {{/is_list}}
87 {{/type}}
88 if (!prop_is_valid) {
89 ast_log(LOG_ERROR, "ARI {{id}} field {{name}} failed validation\n");
90 res = 0;
91 }
92 } else
93 {{/properties}}
94 {
95 ast_log(LOG_ERROR,
96 "ARI {{id}} has undocumented field %s\n",
97 ast_json_object_iter_key(iter));
98 res = 0;
99 }
100 }
101
102 {{#properties}}
103 {{#required}}
104 if (!has_{{name}}) {
105 ast_log(LOG_ERROR, "ARI {{id}} missing required field {{name}}\n");
106 res = 0;
107 }
108
109 {{/required}}
110 {{/properties}}
111 return res;
112 }
113
114 ari_validator ast_ari_validate_{{c_id}}_fn(void)
115 {
116 return ast_ari_validate_{{c_id}};
117 }
118 {{/models}}
119 {{/api_declaration}}
120 {{/apis}}