Parameters validation

Hi, I have a class as a parameter in a service, but the validation is not performed.

The class:

TJsonRPC = class
  strict private
   [Required]  Fjsonrpc: String;
   [MinLenght(1)] Fmethod: string;
   [Required]   Fparams: TObject;
   [Required]   Fid: string;

  public
  property jsonrpc: String read Fjsonrpc write Fjsonrpc;
  property method:   String read Fmethod write Fmethod;
  property params:   TObject read Fparams write Fparams;
  property id:           String read Fid write Fid;
  end;

 [ValidateParams]
 [HttpPost] function JSONRPC2([Required] AJsonRPC:TJsonRPC): TStream;

function TSerGenService.JSONRPC2(AJsonRPC: TJsonRPC): TStream;
var
User: IUserIdentity;
begin
//User Authorization;
if AJsonRPC.Jsonrpc <> '2.0' then
    raise EXDataHttpForbidden.Create('JsonRPC non conforme');
result:=TstringStream.Create('test');
end;

AJsonRpc is not validated on receieve request if one property is empty.
I expect an exception first. Am I doing something wrong?
Thanks

Xdata documentation:
When a parameter is class, then the object itself will also be validated, i.e., all the mapped
members will also have the validation attributes applied:
In the above example, when AcceptFoo is invoked, XData will apply the Required validation to
it. If Foo is nil , the request will be rejected. But also, even if Foo object is provided, the
request will only be accepted if the Id property is positive, and Name property is not longer
than 10 characters.
For example, if the following JSON is sent to the endpoint:
The endpoint will answer with a 400 Bad Request response including the following detailed
content:
TFoo = class
strict private
[Range(1, MaxInt)] FId: Integer;
[MaxLength(10)] FName: string;
public
property Id: Integer read FId write FId;
property Name: string read FName write FName;
end;
{...}
[ValidateParams] procedure AcceptFoo([Required] Foo: TFoo);

Hi,
it seems that I have missing Aurelius.Valitation.Attributes in uses.
(And write wrong MinLength)
Now It works,

But I Have some questions:
Can I pass a string and a Tlist as parameters in a method with 2 parameters? If Yes, how should the json be formed in the body (an little example, thanks).
In this case, the TList is validated? Or is validates only if the parameters is a single object?
Thanks

Yes. Each parameter should be passed as a property in a JSON object, as specified here: Service Operations | TMS XData documentation

If an object being validated has a TList property, then each object in such list will be also validated.

Ok, Thanks!

1 Like