Composition API - Parameter Validation

When using the parameter validation functionality with the composition API we are supposed, according the example, to throw a ValidationError. The ValidationError is serialized as a JSON object with an ErrorCode and Message attribute. Regular errors (VIException) have a Message and Number attribute. Do we have an option to converge to one (1) representation? Having different error objects complicates the client development.

Thx,

Rodney 

Parents
  • At the moment (in version 8.2/9.0/9.1), you have the option to throw ViExceptions instead of returning ValidationErrors from your validation code.

    Thanks
    Hanno

  • Hi Hanno,

    The interface IRequestValidator is defined as:

      public interface IRequestValidator
      {
        string Name { get; }
    
        Task<IEnumerable<ValidationError>> ValidateAsync(IRequest request, CancellationToken ct = default (CancellationToken));
      }

     The extension methods on IMethod are defined as:

    public static T WithValidator<T>(
          this T method,
          string name,
          Func<IRequest, ValidationError> validator)
          where T : IMethod
    //--------------------------
        public static T WithValidator<T>(
          this T method,
          string name,
          Func<IRequest, CancellationToken, Task<ValidationError>> validator)
          where T : IMethod
    //--------------------------
    public static T WithValidator<T>(this T method, IRequestValidator validator) where T : IMethod
          
          

    I tried to throw a ViException but I am getting a compilation error, it seems that nowhere we have an extension method that allows Task<ViException>. Can you help?

  • Hi,

    Please post the code. It's hard to tell what the problem might be without knowing the code and the error.

  •             builder.AddMethod(Method.Define("roles/{roleId}/delete")
                                .WithDescription("Creates a request for the removal of the role. After approval the role will be deleted.")
                                .WithParameter(new RequestParameter()
                                {
                                    Name = "roleId",
                                    Type = typeof(string),
                                    IsInQuery = false,
                                    Description = "The id of the role"
                                })
                                .WithValidator("Check existence of role id", async (qr, ct) =>
                                {
                                    var f = qr.Session.SqlFormatter();
                                    var uidRole = qr.Parameters.Get<string>("roleId");
                                    if (await qr.Session.Source().ExistsAsync("Org", f.AndRelation(
                                                f.UidComparison("UID_OrgRoot", "CCC-A1CC7114316443DD9CCACCD00C2FC459"),
                                                f.UidComparison("UID_Org", uidRole)), ct).ConfigureAwait(false))
                                    {
                                        return null;
                                    }
                                    return new ValidationError("Role with id '{0}' can't be found", uidRole);
                                })
                                .Handle<Request>("DELETE", async (qr, ct) =>
                                {
                                // Not relevant
                                }));

    The above compiles, when replacing the thrown ValidationError with a VIException it will not compile.

Reply
  •             builder.AddMethod(Method.Define("roles/{roleId}/delete")
                                .WithDescription("Creates a request for the removal of the role. After approval the role will be deleted.")
                                .WithParameter(new RequestParameter()
                                {
                                    Name = "roleId",
                                    Type = typeof(string),
                                    IsInQuery = false,
                                    Description = "The id of the role"
                                })
                                .WithValidator("Check existence of role id", async (qr, ct) =>
                                {
                                    var f = qr.Session.SqlFormatter();
                                    var uidRole = qr.Parameters.Get<string>("roleId");
                                    if (await qr.Session.Source().ExistsAsync("Org", f.AndRelation(
                                                f.UidComparison("UID_OrgRoot", "CCC-A1CC7114316443DD9CCACCD00C2FC459"),
                                                f.UidComparison("UID_Org", uidRole)), ct).ConfigureAwait(false))
                                    {
                                        return null;
                                    }
                                    return new ValidationError("Role with id '{0}' can't be found", uidRole);
                                })
                                .Handle<Request>("DELETE", async (qr, ct) =>
                                {
                                // Not relevant
                                }));

    The above compiles, when replacing the thrown ValidationError with a VIException it will not compile.

Children