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