Mike Frysinger | f1ba7ad | 2022-09-12 05:42:57 -0400 | [diff] [blame^] | 1 | # Copyright 2019 The ChromiumOS Authors |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 5 | """Validation helpers for simple input validation in the API. |
| 6 | |
| 7 | Note: Every validator MUST respect config.do_validation. This is an internally |
| 8 | set config option that allows the mock call decorators to be placed before or |
| 9 | after the validation decorators, rather than forcing an ordering that could then |
| 10 | produce incorrect outputs if missed. |
| 11 | """ |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 12 | |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 13 | import functools |
Chris McDonald | 1672ddb | 2021-07-21 11:48:23 -0600 | [diff] [blame] | 14 | import logging |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 15 | import os |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 16 | from typing import Callable, Iterable, List, Optional, Union |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 17 | |
Mike Frysinger | 2c02406 | 2021-05-22 15:43:22 -0400 | [diff] [blame] | 18 | from chromite.third_party.google.protobuf import message as protobuf_message |
Mike Frysinger | 849d640 | 2019-10-17 00:14:16 -0400 | [diff] [blame] | 19 | |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 20 | from chromite.lib import cros_build_lib |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 21 | |
Mike Frysinger | ef94e4c | 2020-02-10 23:59:54 -0500 | [diff] [blame] | 22 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 23 | def _value( |
| 24 | field: str, message: protobuf_message.Message |
| 25 | ) -> Union[bool, int, str, None, List, protobuf_message.Message]: |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 26 | """Helper function to fetch the value of the field. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 27 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 28 | Args: |
| 29 | field: The field name. Can be nested via . separation. |
| 30 | message: The protobuf message it is being fetched from. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 31 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 32 | Returns: |
| 33 | The value of the field. |
| 34 | """ |
| 35 | if not field: |
| 36 | return message |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 37 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 38 | value = message |
| 39 | for part in field.split("."): |
| 40 | if not isinstance(value, protobuf_message.Message): |
| 41 | value = None |
| 42 | break |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 43 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 44 | try: |
| 45 | value = getattr(value, part) |
| 46 | except AttributeError as e: |
| 47 | cros_build_lib.Die("Invalid field: %s", e) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 48 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 49 | return value |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 50 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 51 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 52 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 53 | def exists(*fields: str): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 54 | """Validate that the paths in |fields| exist. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 55 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 56 | Args: |
| 57 | fields (str): The fields being checked. Can be . separated nested |
| 58 | fields. |
| 59 | """ |
| 60 | assert fields |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 61 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 62 | def decorator(func): |
| 63 | @functools.wraps(func) |
| 64 | def _exists(input_proto, output_proto, config, *args, **kwargs): |
| 65 | if config.do_validation: |
| 66 | for field in fields: |
| 67 | logging.debug("Validating %s exists.", field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 68 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 69 | value = _value(field, input_proto) |
| 70 | if not value or not os.path.exists(value): |
| 71 | cros_build_lib.Die( |
| 72 | "%s path does not exist: %s" % (field, value) |
| 73 | ) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 74 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 75 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 76 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 77 | return _exists |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 78 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 79 | return decorator |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 80 | |
| 81 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 82 | def is_in(field: str, values: Iterable): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 83 | """Validate |field| is an element of |values|. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 84 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 85 | Args: |
| 86 | field: The field being checked. May be . separated nested fields. |
| 87 | values: The possible values field may take. |
| 88 | """ |
| 89 | assert field |
| 90 | assert values |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 91 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 92 | def decorator(func): |
| 93 | @functools.wraps(func) |
| 94 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 95 | if config.do_validation: |
| 96 | logging.debug("Validating %s is in %r", field, values) |
| 97 | value = _value(field, input_proto) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 98 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 99 | if value not in values: |
| 100 | cros_build_lib.Die( |
| 101 | "%s (%r) must be in %r", field, value, values |
| 102 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 103 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 104 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 105 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 106 | return _is_in |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 107 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 108 | return decorator |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 109 | |
| 110 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 111 | def each_in( |
| 112 | field: str, |
| 113 | subfield: Optional[str], |
| 114 | values: Iterable, |
| 115 | optional: bool = False, |
| 116 | ): |
| 117 | """Validate each |subfield| of the repeated |field| is in |values|. |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 118 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 119 | Args: |
| 120 | field: The field being checked. May be . separated nested fields. |
| 121 | subfield: The field in the repeated |field| to validate, or None |
| 122 | when |field| is not a repeated message, e.g. enum, scalars. |
| 123 | values: The possible values field may take. |
| 124 | optional: Also allow the field to be empty when True. |
| 125 | """ |
| 126 | assert field |
| 127 | assert values |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 128 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 129 | def decorator(func): |
| 130 | @functools.wraps(func) |
| 131 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 132 | if config.do_validation: |
| 133 | members = _value(field, input_proto) or [] |
| 134 | if not optional and not members: |
| 135 | cros_build_lib.Die("The %s field is empty.", field) |
| 136 | for member in members: |
| 137 | logging.debug( |
| 138 | "Validating %s.[each].%s is in %r.", |
| 139 | field, |
| 140 | subfield, |
| 141 | values, |
| 142 | ) |
| 143 | value = _value(subfield, member) |
| 144 | if value not in values: |
| 145 | cros_build_lib.Die( |
| 146 | "%s.[each].%s (%r) must be in %r is required.", |
| 147 | field, |
| 148 | subfield, |
| 149 | value, |
| 150 | values, |
| 151 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 152 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 153 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 154 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 155 | return _is_in |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 156 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 157 | return decorator |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 158 | |
| 159 | |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 160 | def constraint(description): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 161 | """Define a function to be used as a constraint check. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 162 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 163 | A constraint is a function that checks the value of a field and either |
| 164 | does nothing (returns None) or returns a string indicating why the value |
| 165 | isn't valid. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 166 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 167 | We bind a human readable description to the constraint for error reporting |
| 168 | and logging. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 169 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 170 | Args: |
| 171 | description: Human readable description of the constraint |
| 172 | """ |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 173 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 174 | def decorator(func): |
| 175 | @functools.wraps(func) |
| 176 | def _func(*args, **kwargs): |
| 177 | func(*args, **kwargs) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 178 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 179 | setattr(_func, "__constraint_description__", description) |
| 180 | return _func |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 181 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 182 | return decorator |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 183 | |
| 184 | |
| 185 | def check_constraint(field: str, checkfunc: Callable): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | """Validate all values of |field| pass a constraint. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | Args: |
| 189 | field: The field being checked. May be . separated nested fields. |
| 190 | checkfunc: A constraint function to check on each value |
| 191 | """ |
| 192 | assert field |
| 193 | assert constraint |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 194 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 195 | # Get description for the constraint if it's set |
| 196 | constraint_description = getattr( |
| 197 | checkfunc, |
| 198 | "__constraint_description__", |
| 199 | checkfunc.__name__, |
| 200 | ) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 201 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 202 | def decorator(func): |
| 203 | @functools.wraps(func) |
| 204 | def _check_constraint( |
| 205 | input_proto, output_proto, config, *args, **kwargs |
| 206 | ): |
| 207 | if config.do_validation: |
| 208 | values = _value(field, input_proto) or [] |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 209 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 210 | failed = [] |
| 211 | for val in values: |
| 212 | msg = checkfunc(val) |
| 213 | if msg is not None: |
| 214 | failed.append((val, msg)) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 215 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 216 | if failed: |
| 217 | msg = ( |
| 218 | f"{field}.[all] one or more values failed check " |
| 219 | f'"{constraint_description}"\n' |
| 220 | ) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 221 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 222 | for value, msg in failed: |
| 223 | msg += " %s: %s\n" % (value, msg) |
| 224 | cros_build_lib.Die(msg) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 225 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 226 | return func(input_proto, output_proto, config, *args, **kwargs) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 227 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 228 | return _check_constraint |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 229 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 230 | return decorator |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 231 | |
| 232 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 233 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 234 | def require(*fields: str): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | """Verify |fields| have all been set to truthy values. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 236 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 237 | Args: |
| 238 | fields: The fields being checked. May be . separated nested fields. |
| 239 | """ |
| 240 | assert fields |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 241 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 242 | def decorator(func): |
| 243 | @functools.wraps(func) |
| 244 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 245 | if config.do_validation: |
| 246 | for field in fields: |
| 247 | logging.debug("Validating %s is set.", field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 248 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 249 | value = _value(field, input_proto) |
| 250 | if not value: |
| 251 | cros_build_lib.Die("%s is required.", field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 252 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 253 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | return _require |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 256 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 257 | return decorator |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 258 | |
| 259 | |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 260 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 261 | def require_any(*fields: str): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 262 | """Verify at least one of |fields| have been set. |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 263 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 264 | Args: |
| 265 | fields: The fields being checked. May be . separated nested fields. |
| 266 | """ |
| 267 | assert fields |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 268 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 269 | def decorator(func): |
| 270 | @functools.wraps(func) |
| 271 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 272 | if config.do_validation: |
| 273 | for field in fields: |
| 274 | logging.debug("Validating %s is set.", field) |
| 275 | value = _value(field, input_proto) |
| 276 | if value: |
| 277 | break |
| 278 | else: |
| 279 | cros_build_lib.Die( |
| 280 | "At least one of the following must be set: %s", |
| 281 | ", ".join(fields), |
| 282 | ) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 283 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 284 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 285 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 286 | return _require |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 287 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | return decorator |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 289 | |
| 290 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 291 | def require_each( |
| 292 | field: str, subfields: Iterable[str], allow_empty: bool = True |
| 293 | ): |
| 294 | """Verify |field| each have all of the |subfields| set. |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 295 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 296 | When |allow_empty| is True, |field| may be empty, and |subfields| are only |
| 297 | validated when it is not empty. When |allow_empty| is False, |field| must |
| 298 | also have at least one entry. |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 299 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 300 | Args: |
| 301 | field: The repeated field being checked. May be . separated nested |
| 302 | fields. |
| 303 | subfields: The fields of the repeated message to validate. |
| 304 | allow_empty: Also require at least one entry in the repeated field. |
| 305 | """ |
| 306 | assert field |
| 307 | assert subfields |
| 308 | assert not isinstance(subfields, str) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 309 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 310 | def decorator(func): |
| 311 | @functools.wraps(func) |
| 312 | def _require_each(input_proto, output_proto, config, *args, **kwargs): |
| 313 | if config.do_validation: |
| 314 | members = _value(field, input_proto) or [] |
| 315 | if not allow_empty and not members: |
| 316 | cros_build_lib.Die("The %s field is empty.", field) |
| 317 | for member in members: |
| 318 | for subfield in subfields: |
| 319 | logging.debug( |
| 320 | "Validating %s.[each].%s is set.", field, subfield |
| 321 | ) |
| 322 | value = _value(subfield, member) |
| 323 | if not value: |
| 324 | cros_build_lib.Die("%s is required.", field) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 325 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 326 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 327 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 328 | return _require_each |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 329 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 330 | return decorator |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 331 | |
| 332 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 333 | def validation_complete(func: Callable): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 334 | """Automatically skip the endpoint when called after all other validators. |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 335 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 336 | This decorator MUST be applied after all other validate decorators. |
| 337 | The config can be checked manually if there is non-decorator validation, but |
| 338 | this is much cleaner if it is all done in decorators. |
| 339 | """ |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 340 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 341 | @functools.wraps(func) |
| 342 | def _validate_only(request, response, configs, *args, **kwargs): |
| 343 | if configs.validate_only: |
| 344 | # Avoid calling the endpoint. |
| 345 | return 0 |
| 346 | else: |
| 347 | return func(request, response, configs, *args, **kwargs) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 348 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 349 | return _validate_only |