INSERT : Plugin Validation

BasicCRUD Insert Query with Bridge

Now let us look at another type of validation. That is the plug-in validation.

The plug-in validation happens in the ProcessServices. You see a ProcessCustomerServices, it creates a sequence with the data packet and the data packet contains the data that was passed as parameter from the controller. In the controller, we are passing the parameter that contains the ContextInfo and the InputAPIModel. Now the first validation that we will look at is the validation which are more complex in scenarios. We can have more database interactions or we have something we want to validate something like the aadhar number. So this is one validation that is not started with number, if you see it cause a packets method do not start with number.

public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
    {
        #region "Validations"
        //Validations goes here
        /// <summary>
        /// 
        /// </summary>
        public async Task DoesNotStartWithNumber()
        {

            //If any validation fails, uncomment and use the below line of code to add error to the packet

            if (Regex.IsMatch(this.InputParams.Model.Name, @"^\d+"))
                this.AddError("Name", $"Name {this.InputParams.Model.Name} cannot start with numbers");


            await Task.CompletedTask; //If you have any await in the validation, remove this line
        }

        #endregion

    }

Next validation, I am checking if the name that was provided by the model is unique, if any duplicate name is found. We are adding the message in the data packets error.

public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
    {
        #region "Validations"
        //Validations goes here
        /// <summary>
        /// 
        /// </summary>
        public async Task IsUnique()
        {

            //If any validation fails, uncomment and use the below line of code to add error to the packet
            //this.AddError("key", "ErrorMessage");

            _connectionProvider.ConfigureDbConnectionString(this.InputParams.HostContextInfo);
            _repo.InitializeConnection(_connectionProvider);

            var custId = _repo.FindAll<Customer>().Where(c => c.Name == this.InputParams.Model.Name).Select(s => s.Id).FirstOrDefault();
            if (custId != null)
                this.AddError("Name", $"Duplicate {this.InputParams.Model.Name} Customer found");

            await Task.CompletedTask; //If you have any await in the validation, remove this line
        }

        #endregion

    }

And refer to the repo instance in the constructor of your data packet as shown below:

public partial class AddCustomerDataPacket : FlexiFlowDataPacketBridge
    {

        readonly ILogger<AddCustomerDataPacket> _logger;
        IFlexRepositoryBridge _repo;
        IWriteDbConnectionProviderBridge _connectionProvider;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public AddCustomerDataPacket(ILogger<AddCustomerDataPacket> logger, IFlexRepositoryBridge repo, IWriteDbConnectionProviderBridge connectionProvider)
        {
            _logger = logger;
            _repo = repo;
            _connectionProvider = connectionProvider;
        }

        #region "Properties
        //Models and other properties goes here

        public AddCustomerParams InputParams { get; set; }

        #endregion
    }

Last updated