using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using xCommons.Extensions; using xExceptions.Constants; namespace xCommons.Providers { public partial class XValidationProvider { private ICollection validationGroup; // #region Simple Validators ... /// /// given objects must not null /// /// public void NotNull ( params object[] values) { // foreach (var o in values) { // if (o == null) { XException.InvalidArgs.Throw (); } } } /// /// given strings must not empty or null /// /// public void NotEmpty ( params string[] values) { // foreach (var s in values) { // if (s.IsNullOrEmpty ()) { XException.InvalidArgs.Throw (); } } } /// /// Check a numeric value is bigger than 0 /// /// public void NotZero ( params int[] values) { // foreach (var s in values) { // if (s == 0) { XException.InvalidArgs.Throw (); } } } /// /// Check a numeric value is bigger than 0 /// /// public void NotZero ( params long[] values) { // foreach (var s in values) { // if (s == 0) { XException.InvalidArgs.Throw (); } } } /// /// Check a numeric value is bigger than 0 /// /// public void NotZero ( params decimal[] values) { // foreach (var s in values) { // if (s == 0) { XException.InvalidArgs.Throw (); } } } /// /// Check a Value is a Valid Guid ... /// /// public void IsGuid (params string[] values) { // foreach (var s in values) { // if (!s.IsGuid () || s.IsDefaultGuid ()) { XException.InvalidArgs.Throw (); } } } /// /// Check a Value is a Valid Guid ... /// /// public void IsGuid (params Guid[] values) { // foreach (var s in values) { // if (s.IsNull () || s.IsDefaultGuid ()) { XException.InvalidArgs.Throw (); } } } /// /// Check a Value is an MD5 ... /// /// public void IsMD5 (params string[] values) { // foreach (var s in values) { // if (!s.IsMD5String ()) { XException.InvalidArgs.Throw (); } } } /// /// non zero childs /// /// public void NotZeroChilds ( params IEnumerable[] values) { // foreach (var s in values) { // if (s == null || s.Count () == 0) { XException.InvalidArgs.Throw (); } } } /// /// non zero childs /// /// public void NotZeroChilds ( params ICollection[] values) { // foreach (var s in values) { // if (s == null || s.Count == 0) { XException.InvalidArgs.Throw (); } } } /// /// given string must be a valid URL ... /// /// /// public void Url ( string url, Exception exception = null ) { // NotEmpty (url); // if (exception == null) { exception = XException.InavlidUrl.ToException (); } // if (!url.IsValidUrl ()) { throw exception; } } /// /// given string must be a valid Email Address ... /// /// /// public void EmailAddress ( string emailAdress, Exception exception = null ) { // NotEmpty (emailAdress); // if (exception == null) { exception = XException.InvalidEmailAddress.ToException (); } // if (!emailAdress.IsValidEmail ()) { throw exception; } } /// /// given string must be a valid Mobile Number ... /// /// /// public void MobileNumber ( string mobileNumber, Exception exception = null ) { // NotEmpty (mobileNumber); // if (exception == null) { exception = XException.InvalidMobileNumber.ToException (); } // if (!mobileNumber.IsValidMobileNumber ()) { throw exception; } } #endregion // #region Group Validatos ... public XValidationProvider GroupValidationBuilder () { // validationGroup = new List (); // return this; } public XValidationProvider AddNotEmpty (params string[] values) { // this.validationGroup.Add (() => this.NotEmpty (values) ); // return this; } public XValidationProvider AddNotNull (params object[] values) { // this.validationGroup.Add (() => this.NotNull (values) ); // return this; } public XValidationProvider AddNotZero (params int[] values) { // this.validationGroup.Add (() => this.NotZero (values) ); // return this; } public XValidationProvider AddNotZero (params long[] values) { // this.validationGroup.Add (() => this.NotZero (values) ); // return this; } public XValidationProvider AddNotZero (params decimal[] values) { // this.validationGroup.Add (() => this.NotZero (values) ); // return this; } public XValidationProvider AddIsGuid (params string[] values) { // this.validationGroup.Add (() => this.IsGuid (values) ); // return this; } public XValidationProvider AddIsGuid (params Guid[] values) { // this.validationGroup.Add (() => this.IsGuid (values) ); // return this; } public XValidationProvider AddIsMD5 (params string[] values) { // this.validationGroup.Add (() => this.IsMD5 (values) ); // return this; } public XValidationProvider AddNotZeroChilds (params IEnumerable[] values) { // this.validationGroup.Add (() => this.NotZeroChilds (values) ); // return this; } public XValidationProvider AddNotZeroChilds (params ICollection[] values) { // this.validationGroup.Add (() => this.NotZeroChilds (values) ); // return this; } public XValidationProvider AddEmailAddress ( string emailAdress, Exception exception = null) { // this.validationGroup.Add (() => this.EmailAddress (emailAdress, exception) ); // return this; } public XValidationProvider AddMobileNumber ( string mobileNumber, Exception exception = null) { // this.validationGroup.Add (() => this.MobileNumber (mobileNumber, exception) ); // return this; } public XValidationProvider AddUrl ( string url, Exception exception = null) { // this.validationGroup.Add (() => this.Url (url, exception) ); // return this; } public void ValidateGroup () { // // Run each Tasks ... foreach (var action in validationGroup) { action.Invoke (); } // // Clear Task List ... validationGroup.Clear (); } public async Task ValidateGroupAsync () { // // Run each Tasks ... foreach (var action in validationGroup) { await Task.Run (action); } // // Clear Task List ... validationGroup.Clear (); } #endregion } }