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 |
Greg Edelston | 53d34a1 | 2023-03-27 15:43:53 -0600 | [diff] [blame] | 16 | from typing import Any, 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: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 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: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 33 | The value of the field. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 57 | fields (str): The fields being checked. Can be . separated nested |
| 58 | fields. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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( |
Alex Klein | df8ee50 | 2022-10-18 09:48:15 -0600 | [diff] [blame] | 72 | "%s path does not exist: %s", field, value |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 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 | |
Greg Edelston | 53d34a1 | 2023-03-27 15:43:53 -0600 | [diff] [blame] | 82 | def eq(field: str, expected_value: Any): |
| 83 | """Validate |field| is set to |expected_value|. |
| 84 | |
| 85 | Args: |
| 86 | field: The field being checked. May be a `.` separated field. |
| 87 | expected_value: The value to which the field must be equal. |
| 88 | """ |
| 89 | assert field |
| 90 | |
| 91 | def decorator(func): |
| 92 | @functools.wraps(func) |
| 93 | def _eq(input_proto, output_proto, config, *args, **kwargs): |
| 94 | if config.do_validation: |
| 95 | logging.debug( |
| 96 | "Validating %s is equal to %r", field, expected_value |
| 97 | ) |
| 98 | actual_value = _value(field, input_proto) |
| 99 | |
| 100 | if actual_value != expected_value: |
| 101 | cros_build_lib.Die( |
| 102 | "%s (%r) must be equal to %r", |
| 103 | field, |
| 104 | actual_value, |
| 105 | expected_value, |
| 106 | ) |
| 107 | |
| 108 | return func(input_proto, output_proto, config, *args, **kwargs) |
| 109 | |
| 110 | return _eq |
| 111 | |
| 112 | return decorator |
| 113 | |
| 114 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 115 | def is_in(field: str, values: Iterable): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 116 | """Validate |field| is an element of |values|. |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 117 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 118 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 119 | field: The field being checked. May be . separated nested fields. |
| 120 | values: The possible values field may take. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 121 | """ |
| 122 | assert field |
| 123 | assert values |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 124 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 125 | def decorator(func): |
| 126 | @functools.wraps(func) |
| 127 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 128 | if config.do_validation: |
| 129 | logging.debug("Validating %s is in %r", field, values) |
| 130 | value = _value(field, input_proto) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 131 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 132 | if value not in values: |
| 133 | cros_build_lib.Die( |
| 134 | "%s (%r) must be in %r", field, value, values |
| 135 | ) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 136 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 137 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 138 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 139 | return _is_in |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 140 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 141 | return decorator |
Alex Klein | 231d2da | 2019-07-22 16:44:45 -0600 | [diff] [blame] | 142 | |
| 143 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 144 | def each_in( |
| 145 | field: str, |
| 146 | subfield: Optional[str], |
| 147 | values: Iterable, |
| 148 | optional: bool = False, |
| 149 | ): |
| 150 | """Validate each |subfield| of the repeated |field| is in |values|. |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 151 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 152 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 153 | field: The field being checked. May be . separated nested fields. |
| 154 | subfield: The field in the repeated |field| to validate, or None |
| 155 | when |field| is not a repeated message, e.g. enum, scalars. |
| 156 | values: The possible values field may take. |
| 157 | optional: Also allow the field to be empty when True. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 158 | """ |
| 159 | assert field |
| 160 | assert values |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 161 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 162 | def decorator(func): |
| 163 | @functools.wraps(func) |
| 164 | def _is_in(input_proto, output_proto, config, *args, **kwargs): |
| 165 | if config.do_validation: |
| 166 | members = _value(field, input_proto) or [] |
| 167 | if not optional and not members: |
| 168 | cros_build_lib.Die("The %s field is empty.", field) |
| 169 | for member in members: |
| 170 | logging.debug( |
| 171 | "Validating %s.[each].%s is in %r.", |
| 172 | field, |
| 173 | subfield, |
| 174 | values, |
| 175 | ) |
| 176 | value = _value(subfield, member) |
| 177 | if value not in values: |
| 178 | cros_build_lib.Die( |
| 179 | "%s.[each].%s (%r) must be in %r is required.", |
| 180 | field, |
| 181 | subfield, |
| 182 | value, |
| 183 | values, |
| 184 | ) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 185 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 186 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 187 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 188 | return _is_in |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 189 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 190 | return decorator |
Alex Klein | bdace30 | 2020-12-03 14:40:23 -0700 | [diff] [blame] | 191 | |
| 192 | |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 193 | def constraint(description): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 194 | """Define a function to be used as a constraint check. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 195 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 196 | A constraint is a function that checks the value of a field and either |
| 197 | does nothing (returns None) or returns a string indicating why the value |
| 198 | isn't valid. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 199 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 200 | We bind a human readable description to the constraint for error reporting |
| 201 | and logging. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 202 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 203 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 204 | description: Human readable description of the constraint |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 205 | """ |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 206 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 207 | def decorator(func): |
| 208 | @functools.wraps(func) |
| 209 | def _func(*args, **kwargs): |
| 210 | func(*args, **kwargs) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 211 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 212 | setattr(_func, "__constraint_description__", description) |
| 213 | return _func |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 214 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 215 | return decorator |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 216 | |
| 217 | |
| 218 | def check_constraint(field: str, checkfunc: Callable): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 219 | """Validate all values of |field| pass a constraint. |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 220 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 221 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 222 | field: The field being checked. May be . separated nested fields. |
| 223 | checkfunc: A constraint function to check on each value |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 224 | """ |
| 225 | assert field |
| 226 | assert constraint |
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 | # Get description for the constraint if it's set |
| 229 | constraint_description = getattr( |
| 230 | checkfunc, |
| 231 | "__constraint_description__", |
| 232 | checkfunc.__name__, |
| 233 | ) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 234 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 235 | def decorator(func): |
| 236 | @functools.wraps(func) |
| 237 | def _check_constraint( |
| 238 | input_proto, output_proto, config, *args, **kwargs |
| 239 | ): |
| 240 | if config.do_validation: |
| 241 | values = _value(field, input_proto) or [] |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 242 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 243 | failed = [] |
| 244 | for val in values: |
| 245 | msg = checkfunc(val) |
| 246 | if msg is not None: |
| 247 | failed.append((val, msg)) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 248 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 249 | if failed: |
| 250 | msg = ( |
| 251 | f"{field}.[all] one or more values failed check " |
| 252 | f'"{constraint_description}"\n' |
| 253 | ) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 254 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 255 | for value, msg in failed: |
| 256 | msg += " %s: %s\n" % (value, msg) |
| 257 | cros_build_lib.Die(msg) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 258 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 259 | return func(input_proto, output_proto, config, *args, **kwargs) |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 260 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 261 | return _check_constraint |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 262 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 263 | return decorator |
Sean McAllister | 17eed8d | 2021-09-21 10:41:16 -0600 | [diff] [blame] | 264 | |
| 265 | |
Mike Frysinger | 88e02c1 | 2019-10-01 15:05:36 -0400 | [diff] [blame] | 266 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 267 | def require(*fields: str): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 268 | """Verify |fields| have all been set to truthy values. |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 269 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 270 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 271 | fields: The fields being checked. May be . separated nested fields. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 272 | """ |
| 273 | assert fields |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 274 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 275 | def decorator(func): |
| 276 | @functools.wraps(func) |
| 277 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 278 | if config.do_validation: |
| 279 | for field in fields: |
| 280 | logging.debug("Validating %s is set.", field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 281 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 282 | value = _value(field, input_proto) |
| 283 | if not value: |
| 284 | cros_build_lib.Die("%s is required.", field) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 285 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 286 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 287 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 288 | return _require |
Alex Klein | 2b23672 | 2019-06-19 15:44:26 -0600 | [diff] [blame] | 289 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 290 | return decorator |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 291 | |
| 292 | |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 293 | # pylint: disable=docstring-misnamed-args |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 294 | def require_any(*fields: str): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 295 | """Verify at least one of |fields| have been set. |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 296 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 297 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 298 | fields: The fields being checked. May be . separated nested fields. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 299 | """ |
| 300 | assert fields |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 301 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 302 | def decorator(func): |
| 303 | @functools.wraps(func) |
| 304 | def _require(input_proto, output_proto, config, *args, **kwargs): |
| 305 | if config.do_validation: |
| 306 | for field in fields: |
| 307 | logging.debug("Validating %s is set.", field) |
| 308 | value = _value(field, input_proto) |
| 309 | if value: |
| 310 | break |
| 311 | else: |
| 312 | cros_build_lib.Die( |
| 313 | "At least one of the following must be set: %s", |
| 314 | ", ".join(fields), |
| 315 | ) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 316 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 317 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 318 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 319 | return _require |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 320 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 321 | return decorator |
Alex Klein | 60c8052 | 2020-10-13 18:05:38 -0600 | [diff] [blame] | 322 | |
| 323 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 324 | def require_each( |
| 325 | field: str, subfields: Iterable[str], allow_empty: bool = True |
| 326 | ): |
| 327 | """Verify |field| each have all of the |subfields| set. |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 328 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 329 | When |allow_empty| is True, |field| may be empty, and |subfields| are only |
| 330 | validated when it is not empty. When |allow_empty| is False, |field| must |
| 331 | also have at least one entry. |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 332 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 333 | Args: |
Alex Klein | a044268 | 2022-10-10 13:47:38 -0600 | [diff] [blame] | 334 | field: The repeated field being checked. May be . separated nested |
| 335 | fields. |
| 336 | subfields: The fields of the repeated message to validate. |
| 337 | allow_empty: Also require at least one entry in the repeated field. |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 338 | """ |
| 339 | assert field |
| 340 | assert subfields |
| 341 | assert not isinstance(subfields, str) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 342 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 343 | def decorator(func): |
| 344 | @functools.wraps(func) |
| 345 | def _require_each(input_proto, output_proto, config, *args, **kwargs): |
| 346 | if config.do_validation: |
| 347 | members = _value(field, input_proto) or [] |
| 348 | if not allow_empty and not members: |
| 349 | cros_build_lib.Die("The %s field is empty.", field) |
| 350 | for member in members: |
| 351 | for subfield in subfields: |
| 352 | logging.debug( |
| 353 | "Validating %s.[each].%s is set.", field, subfield |
| 354 | ) |
| 355 | value = _value(subfield, member) |
| 356 | if not value: |
| 357 | cros_build_lib.Die("%s is required.", field) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 358 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 359 | return func(input_proto, output_proto, config, *args, **kwargs) |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 360 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 361 | return _require_each |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 362 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 363 | return decorator |
Alex Klein | 86242bf | 2020-09-22 15:23:23 -0600 | [diff] [blame] | 364 | |
| 365 | |
Alex Klein | bebccd5 | 2021-01-22 13:37:35 -0700 | [diff] [blame] | 366 | def validation_complete(func: Callable): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 367 | """Automatically skip the endpoint when called after all other validators. |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 368 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 369 | This decorator MUST be applied after all other validate decorators. |
| 370 | The config can be checked manually if there is non-decorator validation, but |
| 371 | this is much cleaner if it is all done in decorators. |
| 372 | """ |
Alex Klein | 4de25e8 | 2019-08-05 15:58:39 -0600 | [diff] [blame] | 373 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 374 | @functools.wraps(func) |
| 375 | def _validate_only(request, response, configs, *args, **kwargs): |
| 376 | if configs.validate_only: |
| 377 | # Avoid calling the endpoint. |
| 378 | return 0 |
| 379 | else: |
| 380 | return func(request, response, configs, *args, **kwargs) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 381 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame] | 382 | return _validate_only |