Custom Cerberus validator for TF tag spec. Note: Each _validate_foo function's docstring must end with a segment describing its own validation schema, e.g. "The rule's arguments are...". If you add a new validator, you can copy/paste that section.
| 226 | |
| 227 | |
| 228 | class TfDockerTagValidator(cerberus.Validator): |
| 229 | """Custom Cerberus validator for TF tag spec. |
| 230 | |
| 231 | Note: Each _validate_foo function's docstring must end with a segment |
| 232 | describing its own validation schema, e.g. "The rule's arguments are...". If |
| 233 | you add a new validator, you can copy/paste that section. |
| 234 | """ |
| 235 | |
| 236 | def __init__(self, *args, **kwargs): |
| 237 | # See http://docs.python-cerberus.org/en/stable/customize.html |
| 238 | if 'partials' in kwargs: |
| 239 | self.partials = kwargs['partials'] |
| 240 | super(cerberus.Validator, self).__init__(*args, **kwargs) |
| 241 | |
| 242 | def _validate_ispartial(self, ispartial, field, value): |
| 243 | """Validate that a partial references an existing partial spec. |
| 244 | |
| 245 | Args: |
| 246 | ispartial: Value of the rule, a bool |
| 247 | field: The field being validated |
| 248 | value: The field's value |
| 249 | The rule's arguments are validated against this schema: |
| 250 | {'type': 'boolean'} |
| 251 | """ |
| 252 | if ispartial and value not in self.partials: |
| 253 | self._error(field, |
| 254 | '{} is not present in the partials directory.'.format(value)) |
| 255 | |
| 256 | def _validate_isfullarg(self, isfullarg, field, value): |
| 257 | """Validate that a string is either a FULL=arg or NOT. |
| 258 | |
| 259 | Args: |
| 260 | isfullarg: Value of the rule, a bool |
| 261 | field: The field being validated |
| 262 | value: The field's value |
| 263 | The rule's arguments are validated against this schema: |
| 264 | {'type': 'boolean'} |
| 265 | """ |
| 266 | if isfullarg and '=' not in value: |
| 267 | self._error(field, '{} should be of the form ARG=VALUE.'.format(value)) |
| 268 | if not isfullarg and '=' in value: |
| 269 | self._error(field, '{} should be of the form ARG (no =).'.format(value)) |
| 270 | |
| 271 | |
| 272 | def eprint(*args, **kwargs): |