예제 #1
0
    /// <summary>
    /// Adds the model state errors.
    /// </summary>
    /// <param name="modelState">State of the model.</param>
    /// <returns>The ApiResult from Model State.</returns>
    public static ApiResult FromModelStateErrors(ModelStateDictionary modelState)
    {
        var result = new ApiResult();

        foreach (var(key, value) in modelState.Where(x => x.Value.Errors.Any()))
        {
            var error = value.Errors.First();

            switch (error.ErrorMessage)
            {
            case nameof(ApiErrorCode.FieldRequired):
                result.AddFieldRequiredValidationError(key);
                break;

            case nameof(ApiErrorCode.FieldMismatch):
                result.AddFieldMismatchValidationError(key);
                break;

            default:
                result.AddGenericFieldValidationError(key, error.ErrorMessage);
                break;
            }
        }

        return(result);
    }