#1 2026-01-28 17:37:02

testgary
Member
Registered: 2025-02-06
Posts: 48

How to Elegantly Validate Input Length

TUserDTO = packed record      
  ID: TID;                    
  LogonName: RawUtf8;         
  DisplayName: RawUtf8;       
  InviteCode: RawUtf8;        
  ReferralCode: RawUtf8;      
  GroupName: RawUtf8;         
  Email: RawUtf8;             
  Phone: RawUtf8;             
  AvatarURL: RawUtf8;         
  Gender: TGenderTypeEnum;    
  Birthdate: TTimeLog;        
  Country: RawUtf8;           
  Province: RawUtf8;          
  City: RawUtf8;              
...       
end;                          

lazarus + mormot 2 + mongodb

How to Elegantly Validate Input Length?

Offline

#2 Yesterday 10:31:47

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 15,397
Website

Re: How to Elegantly Validate Input Length

The TSynFilter and TSynValidation classes are currently for ORM only...

You could be tempted to define a reusable function() returning an enumeration or a string to make it on the client/DTO side.
But since it may be very depending of each use case of this DTO, it may not be easy to reuse this function.

Perhaps, the cleanest is to put the logic on the server side, having the application layer check the DTO sent by the client, depending on its actual context call.
With an application layer, the API method/URI is tied to the application context, so it knows enough about the context to validate the input.
And only having basic validation, e.g. at the UI level, for most obvious issue.
This would ensure that the very same logic could be reuse if you use another kind of client (e.g. web app).

Of course, there will be final context validation at persistence level, because your persistence service would not trust the application layer, and could reject length.
Note that input length is likely to be depending on the backend database. It is better in 2026 to have no size limit on the database field length.

Offline

#3 Yesterday 13:31:15

testgary
Member
Registered: 2025-02-06
Posts: 48

Re: How to Elegantly Validate Input Length

class procedure TOrmUser.InternalDefineModel(Props: TOrmProperties);
begin
  inherited InternalDefineModel(Props);
  AddFilterOrValidate('Phone', TSynValidatePhone.Create);
  AddFilterOrValidate('Email', TSynValidateEmail.Create);
  ...
end; 

Is this how it's written?

Offline

Board footer

Powered by FluxBB