def validate_absolute_length(self, value): if self.length and len(value) != self.length: raise ValidationError(self.messages['absolute_length'] % self.length)
def validate_uppercase(self, value): if value.upper() != value: raise ValidationError("Value must be uppercase!")
def validate_lotID(self, data, lotID): if isinstance(data["__parent__"], Model): if not lotID and data["__parent__"].lots: raise ValidationError("This field is required.") if lotID and lotID not in [lot.id for lot in data["__parent__"].lots if lot]: raise ValidationError("lotID should be one of lots")
def validate_const(self, value): if value != self.const: raise ValidationError(f"value must be equal \"{self.const}\"")
def validate_cpv_group(items, *args): if items and len(set([i.classification.id[:3] for i in items])) != 1: raise ValidationError(u"CPV group of items be identical")
def validate_email(self, data, value): if not value and not data.get('telephone'): raise ValidationError(u"telephone or email should be present")
def validate_relatedParty(self, data, value): parent = data['__parent__'] if value and isinstance(parent, Model) and value not in [i.id for i in parent.parties]: raise ValidationError(u"relatedParty should be one of parties.")
def validate_publish(self, data, dt, context): if dt > datetime.datetime(2012, 1, 1, 0, 0) and not data['can_future']: raise ValidationError(future_error_msg)
def validate_should_raise(self, data, value, context): if value: raise ValidationError('message')
def validate_cancellationOf(self, data, cancellationOf): if (isinstance(data["__parent__"], Model) and cancellationOf == "lot" and not hasattr(data["__parent__"], "lots")): raise ValidationError( u'Lot cancellation can not be submitted, since "multiple lots" option is not available for this type of tender.' )
def validate_cause(self, data, value): required = get_first_revision_date( data, default=get_now()) >= QUICK_CAUSE_REQUIRED_FROM if required and value is None: raise ValidationError(BaseType.MESSAGES["required"])
def validate_relatedLot(self, data, relatedLot): if not relatedLot and data.get("cancellationOf") == "lot": raise ValidationError(u"This field is required.") if (relatedLot and isinstance(data["__parent__"], Model) and relatedLot not in [i.id for i in data["__parent__"].get("lots", [])]): raise ValidationError(u"relatedLot should be one of lots")
def validate_dateSigned(self, data, value): if value and value > get_now(): raise ValidationError( u"Contract signature date can't be in the future")
def validate_relatedLot(self, data, relatedLot): if relatedLot and isinstance(data["__parent__"], Model): raise ValidationError(u"This option is not available")
def validate_violationType(self, data, value): if data["violationOccurred"] and not value: raise ValidationError(u"This field is required.") if value and OTHER_VIOLATION not in value: # drop other type description data["otherViolationType"] = None
def validate_publish(self, data, dt, context): if data['should_raise'] is True: raise ValidationError(u'') return dt
def validate_otherViolationType(self, data, value): if OTHER_VIOLATION in data["violationType"] and not value: raise ValidationError(u"This field is required.")
def validate_call_me(self, data, value, context): if data['name'] == u'Brad' and value is True: raise ValidationError( u'I\'m sorry I never call people who\'s name is Brad') return value
def validate_startDate(self, data, value): if value and data.get('endDate') and data.get('endDate') < value: raise ValidationError(u"period should begin before its end")
def validate_call_me(self, data, value, context): if data['name'] == u'Joe': raise ValidationError(u"Don't try to decept me! You're Joe!") return value
def validate_date(self, data, value): if value >= datetime.datetime.now(): return value else: raise ValidationError(u'Date and time less than the current')
def test_clean_validation_messages(): error = ValidationError("A") assert error.messages == ["A"]
def validate_items_uniq(items, *args): if items: ids = [i.id for i in items] if [i for i in set(ids) if ids.count(i) > 1]: raise ValidationError(u"Item id should be uniq for all items")
def test_clean_validation_messages_list(): error = ValidationError(["A", "B", "C"]) assert error.messages, ["A", "B", "C"]
def validate_id(self, value): if not isinstance(value, bson.objectid.ObjectId): try: value = bson.objectid.ObjectId(unicode(value)) except Exception, e: raise ValidationError('Invalid ObjectId')
def test_builtin_validation_exception(): with pytest.raises(ValueError): raise ValidationError('message')
def validate_contains_m_chars(self, value): if value.count("M") != self.number_of_m_chars: raise ValidationError( "Value must contain {} 'm' characters".format( self.number_of_m_chars))
def is_not_future(dt, context=None): if dt > now: raise ValidationError(future_error_msg)
def validate_milestones(self, data, value): required = get_first_revision_date(data, default=get_now()) > MILESTONES_VALIDATION_FROM if required and (value is None or len(value) < 1): raise ValidationError("Tender should contain at least one milestone")
def validate_numeric(self, value): if re.compile(r'\D').match(value): raise ValidationError(self.messages['digits'])