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?

Reply
  • 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?

Children