2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 2013, Digium, Inc.
6 * David M. Lee, II <dlee@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief Implementation Swagger validators.
23 * \author David M. Lee, II <dlee@digium.com>
27 <support_level>core</support_level>
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include "ari/ari_model_validators.h"
35 #include "asterisk/logger.h"
36 #include "asterisk/module.h"
37 #include "asterisk/utils.h"
41 /* Regex to match date strings */
42 static regex_t date_regex;
44 /* Regex for YYYY-MM-DD */
45 #define REGEX_YMD "[0-9]{4}-[01][0-9]-[0-3][0-9]"
47 /* Regex for hh:mm(:ss(.s)); seconds and subseconds optional
48 * Handles the probably impossible case of a leap second, too */
49 #define REGEX_HMS "[0-2][0-9]:[0-5][0-9](:[0-6][0-9](.[0-9]+)?)?"
51 /* Regex for timezone: (+|-)hh(:mm), with optional colon. */
52 #define REGEX_TZ "(Z|[-+][0-2][0-9](:?[0-5][0-9])?)"
54 /* REGEX for ISO 8601, the time specifier optional */
55 #define ISO8601_PATTERN "^" REGEX_YMD "(T" REGEX_HMS REGEX_TZ ")?$"
57 static int check_type(struct ast_json *json, enum ast_json_type expected)
59 enum ast_json_type actual;
62 ast_log(LOG_ERROR, "Expected type %s, was NULL\n",
63 ast_json_typename(expected));
67 actual = ast_json_typeof(json);
68 if (expected != actual) {
69 ast_log(LOG_ERROR, "Expected type %s, was %s\n",
70 ast_json_typename(expected), ast_json_typename(actual));
76 static int check_range(intmax_t minval, intmax_t maxval, struct ast_json *json)
80 if (!check_type(json, AST_JSON_INTEGER)) {
84 v = ast_json_integer_get(json);
86 if (v < minval || maxval < v) {
87 ast_log(LOG_ERROR, "Value out of range. Expected %jd <= %jd <= %jd\n", minval, v, maxval);
93 int ast_ari_validate_void(struct ast_json *json)
95 return check_type(json, AST_JSON_NULL);
98 int ast_ari_validate_object(struct ast_json *json)
100 return check_type(json, AST_JSON_OBJECT);
103 int ast_ari_validate_byte(struct ast_json *json)
105 /* Java bytes are signed, which accounts for great fun for all */
106 return check_range(-128, 255, json);
109 int ast_ari_validate_boolean(struct ast_json *json)
111 enum ast_json_type actual = ast_json_typeof(json);
117 ast_log(LOG_ERROR, "Expected type boolean, was %s\n",
118 ast_json_typename(actual));
123 int ast_ari_validate_int(struct ast_json *json)
125 /* Swagger int's are 32-bit */
126 return check_range(-2147483648LL, 2147483647LL, json);
129 int ast_ari_validate_long(struct ast_json *json)
131 /* All integral values are valid longs. No need for range check. */
132 return check_type(json, AST_JSON_INTEGER);
135 int ast_ari_validate_float(struct ast_json *json)
137 return check_type(json, AST_JSON_REAL);
140 int ast_ari_validate_double(struct ast_json *json)
142 return check_type(json, AST_JSON_REAL);
145 int ast_ari_validate_string(struct ast_json *json)
147 return check_type(json, AST_JSON_STRING);
150 int ast_ari_validate_date(struct ast_json *json)
152 /* Dates are ISO-8601 strings */
154 if (!check_type(json, AST_JSON_STRING)) {
157 str = ast_json_string_get(json);
158 ast_assert(str != NULL);
159 if (regexec(&date_regex, str, 0, NULL, 0) != 0) {
160 ast_log(LOG_ERROR, "Date field is malformed: '%s'\n", str);
166 int ast_ari_validate_list(struct ast_json *json, int (*fn)(struct ast_json *))
171 if (!check_type(json, AST_JSON_ARRAY)) {
175 for (i = 0; i < ast_json_array_size(json); ++i) {
177 member_res = fn(ast_json_array_get(json, i));
180 "Array member %zd failed validation\n", i);
188 static int load_module(void)
191 res = regcomp(&date_regex, ISO8601_PATTERN,
192 REG_EXTENDED | REG_ICASE | REG_NOSUB);
195 return AST_MODULE_LOAD_FAILURE;
197 return AST_MODULE_LOAD_SUCCESS;
200 static int unload_module(void)
202 regfree(&date_regex);
206 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER | AST_MODFLAG_GLOBAL_SYMBOLS, "ARI Model validators",
208 .unload = unload_module,
209 .load_pri = AST_MODPRI_APP_DEPEND,