Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Added ValidateAndAdapt Methods for Property Validation

Open haritha99ch opened this issue 2 years ago • 4 comments

This PR introduces two new extension methods, ValidateAndAdapt, for the Mapster library. These methods ensure that all properties in the destination type either exist in the source type or are configured in Mapster. If a property does not exist and is not configured, an exception is thrown.

The PR includes:

  • Implementation of ValidateAndAdapt methods.
  • Test cases that cover different scenarios.

Usage

public static class ReportSelectors
{


    #region Selectors

    /// <summary>
    ///     Select WitnessId
    /// </summary>
    /// <returns>new <see cref="PermissionsToEditProps" />{ WitnessId = report.WitnessId }</returns>
    public static readonly Expression<Func<Report, PermissionsToEditProps>> PermissionsToEdit =
        e => e.ValidateAndAdapt<Report, PermissionsToEditProps>();

    /// <summary>
    ///     Select ModeratorId, Status
    /// </summary>
    /// <returns>new <see cref="PermissionsToModerateProps" />{ ModeratorId = report.ModeratorId, Status = report.Status }</returns>
    public static readonly Expression<Func<Report, PermissionsToModerateProps>> PermissionsToModerate =
        e => e.ValidateAndAdapt<Report, PermissionsToModerateProps>();

    /// <summary>
    ///     Select WitnessId, Status
    /// </summary>
    /// <returns>new <see cref="PermissionsToViewProps" />{ WitnessId = report.WitnessId, Status = report.Status }</returns>
    public static readonly Expression<Func<Report, PermissionsToViewProps>> PermissionsToView =
        e => e.ValidateAndAdapt<Report, PermissionsToViewProps>(PermissionsToViewPropsAdapter.Config);

    #endregion


    #region Selector Properties

    public record PermissionsToEditProps(WitnessId WitnessId);

    public record PermissionsToModerateProps(ModeratorId? ModeratorId, Status Status);

    public record PermissionsToViewProps(WitnessId WitnessId, WitnessDto Witness, Status Status)
    {
        /// <summary>
        ///     Adapter Settings for <see cref="Witness" />
        /// </summary>
        internal static readonly TypeAdapterSetter<Witness, WitnessDto> WitnessDtoAdapter =
            TypeAdapterConfig<Witness, WitnessDto>.NewConfig()
                .Map(s => s.WitnessId, e => e.Id);
    }

    #endregion


    #region Adapter Settings

    /// <summary>
    ///     Adapter Settings for <see cref="PermissionsToViewProps" />
    /// </summary>
    private static readonly TypeAdapterSetter<Report, PermissionsToViewProps> PermissionsToViewPropsAdapter =
        TypeAdapterConfig<Report, PermissionsToViewProps>.NewConfig()
            .Map(s => s.WitnessId, e => e.WitnessId)
            .Map(s => s.Witness,
                e => e.Witness!.ValidateAndAdapt<Witness, WitnessDto>(PermissionsToViewProps.WitnessDtoAdapter.Config));

    #endregion


}

haritha99ch avatar Oct 08 '23 13:10 haritha99ch

Hello @haritha99ch Is this a PR, about adding a new feature, or do you want to solve some existing problem? For example: Unexpected null or default value in TDisitination after adaptation?

DocSvartz avatar Oct 27 '23 10:10 DocSvartz

Hello @DocSvartz I’m not entirely sure if there are existing validations in the library. If there are, then this is just an alternative solution to a problem. However, if there aren’t any validations, this could be considered as a new feature. I just wanted to share a fix, and I hope this can certainly be improved.

haritha99ch avatar Oct 30 '23 14:10 haritha99ch

This is a great addition. You mentioned RecordType in the description. And I wanted to make sure that the starting point for this PR was not any mapping problems to RecordType.

DocSvartz avatar Oct 30 '23 16:10 DocSvartz

Thank you for your feedback @DocSvartz. and yes, this is not any mapping problems to RecordType.

haritha99ch avatar Oct 31 '23 10:10 haritha99ch