1 /* 2 * Copyright 2019, Andrew Lindesay <apl@lindesay.co.nz>. 3 * 4 * All rights reserved. Distributed under the terms of the MIT License. 5 */ 6 #ifndef VALIDATION_FAILURE_H 7 #define VALIDATION_FAILURE_H 8 9 10 #include <Archivable.h> 11 #include <ObjectList.h> 12 #include <String.h> 13 #include <StringList.h> 14 15 16 /*! This objects marries together a 'key' into some data together with an 17 error message. The error message is not intended to be human readable, but 18 is a key into a message. 19 */ 20 21 class ValidationFailure : public BArchivable { 22 public: 23 ValidationFailure(BMessage* from); 24 ValidationFailure(const BString& property); 25 virtual ~ValidationFailure(); 26 27 const BString& Property() const; 28 const BStringList& Messages() const; 29 bool IsEmpty() const; 30 bool Contains(const BString& message) const; 31 32 void AddMessage(const BString& value); 33 34 status_t Archive(BMessage* into, bool deep = true) const; 35 private: 36 BString fProperty; 37 BStringList fMessages; 38 }; 39 40 41 class ValidationFailures : public BArchivable { 42 public: 43 ValidationFailures(BMessage* from); 44 ValidationFailures(); 45 virtual ~ValidationFailures(); 46 47 void AddFailure(const BString& property, 48 const BString& message); 49 int32 CountFailures() const; 50 ValidationFailure* FailureAtIndex(int32 index) const; 51 bool IsEmpty() const; 52 bool Contains(const BString& property) const; 53 bool Contains(const BString& property, 54 const BString& message) const; 55 56 status_t Archive(BMessage* into, bool deep = true) const; 57 58 private: 59 void _AddFromMessage(const BMessage* from); 60 ValidationFailure* _GetFailure(const BString& property) const; 61 ValidationFailure* _GetOrCreateFailure(const BString& property); 62 63 private: 64 BObjectList<ValidationFailure> 65 fItems; 66 }; 67 68 69 #endif // VALIDATION_FAILURE_H 70