def is_valid(self, raise_exception=False): errors = [] try: self._validated_data = self.load(self.initial_data).data except (ValidationError, IncorrectTypeError) as err: errors.extend(err.messages.get('errors', [])) try: data, serializers = self.load_profile_updates() self._validated_profile_updates_data = data self._profile_updates_serializers = serializers except (ValidationError, IncorrectTypeError) as err: errors.extend(err.messages.get('errors', [])) if errors: self._validated_data = {} self._validated_profile_updates_data = [] self._errors = errors if raise_exception: err = ValidationError(u'Invalid data.') err.messages = { 'errors': errors } raise err return False self._errors = {} return True
def verify_result(self, value): if not value: logger.info('UserPatchSchema empty \'password\'') raise ValidationError('password must not be empty', 'password')
def _deserialize(self, value, attr, data, **kwargs): doi = idutils.is_doi(value) if not doi: raise ValidationError(f"It is not valid doi: \"{value}\"") return doi.string
def test_cli_invalid_config(self): with patch('aws_gate.cli.parse_arguments', return_value=MagicMock(subcommand='bootstrap')), \ patch('aws_gate.cli.load_config_from_files', side_effect=ValidationError(message='error')): with self.assertRaises(ValueError): main()
def _deserialize(self, value, attr, obj): try: return arrow.get(value).datetime except ParserError as ex: raise ValidationError(ex)
def validate_name(self, value): if User.query.filter_by(email=value).first(): raise ValidationError('Email already in use.')
def __post_init__(self): if self.managed and not self.name: raise ValidationError("name is required")
def handle_error(self, exc, data): raise ValidationError( 'An error occurred with input: {0} \n {1}'.format( data, exc.messages))
def validate_form(self, data, **kwargs): """Validates set task instance state form""" if not exactly_one(data.get("execution_date"), data.get("dag_run_id")): raise ValidationError( "Exactly one of execution_date or dag_run_id must be provided")
def validate_confirm_password(self, data, **_): if data.get("password") != data.get("confirm_password"): raise ValidationError("Strings don't match (password).")
def validate_passwords(self, data): if data["password"] != data["password_repeat"]: raise ValidationError( "The password confirmation does not match the password", "password_repeat")
def _validate_password(p): if not (8 <= len(p) <= 64): raise ValidationError("Length must be between 8 and 64 chars.")
def _validate_name(n): if len(n.strip()) == 0: raise ValidationError("Must be filled in.")
def validate_to_from_not_equal(self, data): # NOQA pylint:disable=no-self-use if 'msg_to' in data.keys() and 'msg_from' in data.keys() and data['msg_to'][0] == data['msg_from']: logger.info('Message to and message from cannot be the same', message_to=data['msg_to'][0], message_from=data['msg_from']) raise ValidationError("msg_to and msg_from fields can not be the same.")
def validate_field_length(field_name, length, max_field_len, data=None): if length > max_field_len: logger.info('Field is too large', field_name=field_name, length=length, max_field_len=max_field_len) raise ValidationError(f'{field_name} field length must not be greater than {max_field_len}', field_name, [], data)
def validate_name(self, name): if not re.match("^[A-Za-z0-9_-]{1,32}$", name): raise ValidationError( "Group should contain max 32 chars and include only " "letters, digits, underscores and dashes" )
def validate_pass(self, value): if len(value) < 6: raise ValidationError('Password must be at least 6 characters.')
def check_keyword(self, data, **kwargs): keywords = data.get("keywords", []) if isinstance(keywords, dict): if "error" in keywords: raise ValidationError(keywords["error"]) return data
def validate_password(self, obj): if len(obj['password']) < 6: raise ValidationError( 'Your password must be at least 6 characters.', [ 'password', ])
def check_language(self, data, **kwargs): language = data.get("language") if not language: raise ValidationError("Language is required field", field_name="language") return data
def validate(self, data): if not data['stdout'] and not data['exception']: raise ValidationError( "Must include either an exception or a stdout")
def rules_exceptions(self, data, **kwargs): if "rulesExceptions" in data: raise ValidationError(f"Some rules raises exception: {data['rulesExceptions']}") return data
def validate_type_interface(interface_object: dict): """ Takes a Kubeflow Component interface and returns True/False if valid. From here: https://www.kubeflow.org/docs/pipelines/reference/component-spec/#detailed-specification-componentspec # noqa """ # Kubeflow types manually copied from here - # https://github.com/kubeflow/pipelines/blob/master/sdk/python/kfp/dsl/types.py # TODO: Code gen this list: https://github.com/kubeflow/pipelines/blob/master/sdk/python/kfp/components/_structures.py # noqa kubeflow_types = { "Integer": int, "String": str, "Float": float, "Bool": bool, "List": list, "Dict": dict, "GcsPath": None, "GCPPath": None, "GCRPath": None, "GCPRegion": None, "GCPProjectID": None, "LocalPath": None, "Dataset": None, "AzureSku": None } # The schema gives us a list of dicts, each of which only has one entry. So we have to do # this - could theoretically support multiple entries per list item if it comes to that. if 'name' in interface_object: # This is for the following: # input: # - { name: foo, type: bar} # TODO: THIS IS SUPER BROKEN, IT WILL FAIL ON INTERFACES NAMED 'name' interface_dict = interface_object else: # This is for the following: # input: # - foo: { type: bar} key = next(iter(interface_object.keys())) interface_dict = interface_object[key] interface_dict['name'] = key if 'type' in interface_dict: # TODO: Let's raise this with KFP folks - I think type should be required. # if 'type' in interface_dict: # raise ValidationError(f"No type given for interface.") if isinstance(interface_dict['type'], dict) and len( interface_dict['type'].keys()) == 1: interface_type = next(iter(interface_dict['type'].keys())) elif isinstance(interface_dict['type'], str): interface_type = interface_dict['type'] else: raise ValidationError( f"{interface_dict['type']} is expected to be a string or a dict with one entry." ) # noqa if interface_type not in kubeflow_types.keys(): raise ValidationError( f"'{interface_type}' is not a known type for an interface. Types are case sensistive. Please see this link for known types: https://aka.ms/kfptypes." ) # noqa elif 'default' in interface_dict \ and kubeflow_types[interface_type] is not None: try: kubeflow_types[interface_type](interface_dict['default']) except (ValueError, TypeError): raise ValidationError( f"'{interface_dict['default']}' is not a valid default type for this field, nor is it castable using the provided type. If you were expecting it to be a string, make sure it's quoted." ) # noqa # print(f"dict: f'{interface_dict}'") # print(f"key: '{key}'\ndict: f'{interface_dict}'") if 'name' in interface_dict and not isinstance(interface_dict['name'], str): raise ValidationError( f"{interface_dict['name']} is not a valid string (if you were expecting it to be cast as a string make sure it's quoted." ) # noqa if 'description' in interface_dict and not isinstance( interface_dict['description'], str): raise ValidationError( f"{interface_dict['description']} is not a valid string (if you were expecting it to be cast as a string make sure it's quoted." ) # noqa return True
def validate_timeslots(self, v): if len(set(v)) != len(v): raise ValidationError('Time slots are not unique')
def verify_result(self, value): if value not in ('pending', 'approved', 'suspended'): logger.info('UserPostSchema incorrect \'status\' value \'%s\'' % value) raise ValidationError('status must be one of \'pending\', \'approved\', \'suspended\'', 'status')
def validate_signature(self, data, **kwargs): data = dict(data) signature = data.pop('signature') auth_uid = data.pop('auth_uid') if not check_user_signature(dict(data, uid=auth_uid), signature): raise ValidationError("Participant's user signature is invalid!")
def validate_id(self, value): """Validate Id.""" raise ValidationError('Não envie pelo amor de Deus o ID.')
def validate_parameters(self, param1): if param1 == None: raise ValidationError("Parameters is None")
def _deserialize(self, value, attr, data, **kwargs): pattern = r"RIV/\w{8}:\w{5}/\d{2}:\d{8}$" match = re.match(pattern, value.strip()) if not match: raise ValidationError(f"It is not valid RIV id: \"{value.strip()}\"") return match.string
def _handle_not_supported_config(config, not_supported_keys: Iterable[str]): for key in not_supported_keys: value = _get_nested_dict(config, key) if value is not None: raise ValidationError({not_supported_keys: "Not supported anymore"})
def load_profile_updates(self): if UPDATES_PROFILE not in self.initial_data.get( 'links', {}).get('profile', []): return [], [] for alias, profile in iteritems(self.initial_data.get('aliases', {})): if profile == UPDATES_PROFILE: break else: return [], [] errors = [] validated_data = [] profile_serializers = [] for i, update in enumerate( self.initial_data.get('meta', {}).get(alias, [])): if 'type' not in update: errors.append({ 'detail': '`data` object must include `type` key.', 'source': { 'pointer': '/meta/{}/{}/data'.format(alias, i) } }) continue type_ = update['type'] data = { 'data': update, } try: serializer_class = get_schema(type_) except ImproperlyConfigured: errors.append({ 'detail': 'Invalid type: {}.'.format(type_), 'source': { 'pointer': '/meta/{}/{}/data/type'.format(alias, i), }, }) continue serializer = serializer_class( data=data, partial=set( serializer_class.opts.model._ordered_fields).difference( {'id'})) try: validated_data.append(serializer.validated_data) profile_serializers.append(serializer) except (ValidationError, IncorrectTypeError) as err: errors.extend({ 'detail': error['detail'], 'source': { 'pointer': '/meta/{}/{}{}'.format( alias, i, error['source']['pointer']) }, } for error in err.messages.get('errors', [])) continue if errors: err = ValidationError(u'Invalid data for updates.') err.messages = { 'errors': errors } raise err return validated_data, profile_serializers
def validate_not_present(data, field_name): if field_name in data.keys(): logger.info('Field cannot be set', field_name=field_name) raise ValidationError(f"{field_name} can not be set")