Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ValidateAndAdapt Methods for Property Validation #641

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

haritha99ch
Copy link

@haritha99ch haritha99ch commented Oct 8, 2023

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


}

@DocSvartz
Copy link

DocSvartz commented Oct 27, 2023

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?

@haritha99ch
Copy link
Author

haritha99ch commented Oct 30, 2023

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.

@DocSvartz
Copy link

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.

@haritha99ch
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants